Combine two (or more) PDF's

后端 未结 17 1469
梦毁少年i
梦毁少年i 2020-12-04 07:26

Background: I need to provide a weekly report package for my sales staff. This package contains several (5-10) crystal reports.

Problem:

17条回答
  •  攒了一身酷
    2020-12-04 07:49

    Here is a single function that will merge X amount of PDFs using PDFSharp

    using PdfSharp;
    using PdfSharp.Pdf;
    using PdfSharp.Pdf.IO;
    
    public static void MergePDFs(string targetPath, params string[] pdfs) {
        using(PdfDocument targetDoc = new PdfDocument()){
            foreach (string pdf in pdfs) {
                using (PdfDocument pdfDoc = PdfReader.Open(pdf, PdfDocumentOpenMode.Import)) {
                    for (int i = 0; i < pdfDoc.PageCount; i++) {
                        targetDoc.AddPage(pdfDoc.Pages[i]);
                    }
                }
            }
            targetDoc.Save(targetPath);
        }
    }
    

提交回复
热议问题