Home > Backend Development > C++ > How to Merge Multiple Runtime-Generated PDFs Using ITextSharp?

How to Merge Multiple Runtime-Generated PDFs Using ITextSharp?

Linda Hamilton
Release: 2024-12-27 08:18:11
Original
631 people have browsed it

How to Merge Multiple Runtime-Generated PDFs Using ITextSharp?

How to Merge and Print Multiple PDFs Generated at Runtime Using ITextSharp?

To merge multiple PDFs generated at runtime using ITextSharp, you have two primary options: using the PdfCopy class family or the PdfWriter class.

PdfCopy Family

The Pdf*Copy* classes allow you to merge PDFs while preserving their original format and interactive annotations. For this approach, follow these steps:

  1. Initialize an empty PDF document.
  2. Create a PdfCopy object.
  3. Loop through the generated PDF bytes.
  4. For each byte array, create a PdfReader and import its pages into the PdfCopy object.

Example Using PdfCopy:

byte[] mergedPdf = null;
using (MemoryStream ms = new MemoryStream())
{
    using (Document document = new Document())
    {
        using (PdfCopy copy = new PdfCopy(document, ms))
        {
            document.Open();

            for (int i = 0; i < pdfBytes.Count; ++i)
            {
                PdfReader reader = new PdfReader(pdfBytes[i]);

                int n = reader.NumberOfPages;
                for (int page = 0; page < n; )
                {
                    copy.AddPage(copy.GetImportedPage(reader, ++page));
                }
            }
        }
    }
    mergedPdf = ms.ToArray();
}
Copy after login

PdfWriter Class

The PdfWriter class allows you to integrate pages from source PDFs into a new document without preserving their original interactivity. Use this approach if you are not concerned about annotations or other features in the source PDFs.

using (MemoryStream ms = new MemoryStream())
{
    using (Document document = new Document())
    {
        using (PdfWriter writer = PdfWriter.GetInstance(document, ms))
        {
            document.Open();

            for (int i = 0; i < pdfBytes.Count; ++i)
            {
                PdfReader reader = new PdfReader(pdfBytes[i]);

                int n = reader.NumberOfPages;
                for (int page = 0; page < n; )
                {
                    PdfImportedPage importedPage = writer.GetImportedPage(reader, ++page);
                    writer.Add(importedPage);
                }

                reader.Close();
            }
        }
    }
    mergedPdf = ms.ToArray();
}
Copy after login

For more details, refer to the iTextSharp documentation or the code examples provided in the linked answer.

The above is the detailed content of How to Merge Multiple Runtime-Generated PDFs Using ITextSharp?. 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