Home > Backend Development > C++ > How to Merge Runtime-Generated PDF Files in iTextSharp for Printing?

How to Merge Runtime-Generated PDF Files in iTextSharp for Printing?

Susan Sarandon
Release: 2024-12-25 20:26:10
Original
655 people have browsed it

How to Merge Runtime-Generated PDF Files in iTextSharp for Printing?

Merging Multiple PDF Files Generated at Runtime

Question:

How do I merge multiple PDF files generated at runtime using iTextSharp for printing purposes?

Answer:

For merging source documents in iText(Sharp), two distinct situations arise:

  1. Preserving Original Layout:
    To merge documents with unaltered pages, annotations, and content, employ a solution based on the Pdf*Copy* class family. This ensures the integrity of the original documents' interactive features.
  2. Creating a New Document:
    For integrating pages while controlling the overall format and discarding interactive features, consider using the PdfWriter class. It allows importing pages from source documents and governing their presentation.

Implementation Using PdfCopy:

byte[] mergedPdf = null;
using (MemoryStream ms = new MemoryStream())
{
    using (Document document = new Document())
    {
        using (PdfCopy copy = new PdfCopy(document, ms))
        {
            document.Open();
            // Iterate over the PDF byte arrays and add pages to the merged document
            for (int i = 0; i < pdf.Count; ++i)
            {
                PdfReader reader = new PdfReader(pdf[i]);
                // Extract pages from the reader and add them to the merged document
                int n = reader.NumberOfPages;
                for (int page = 0; page < n; )
                {
                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                }
            }
        }
    }
    mergedPdf = ms.ToArray();
}
Copy after login

In the provided code, pdf is an array of byte arrays, each representing a generated PDF document. The resulting mergedPdf byte array contains the combined PDF content prepared for printing.

Class Summary:

  • PdfCopy: Copies pages without redundancy detection, but can struggle with forms.
  • PdfCopyFields: Merges fields and forms but may consume excessive memory.
  • PdfSmartCopy: Detects redundancies efficiently but requires more resources.
  • PdfWriter: Imports pages but sacrifices interactive features of the imported pages.

The above is the detailed content of How to Merge Runtime-Generated PDF Files in iTextSharp for Printing?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template