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:
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(); }
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:
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!