Combine two (or more) PDF's

后端 未结 17 1478
梦毁少年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:45

    I had to solve a similar problem and what I ended up doing was creating a small pdfmerge utility that uses the PDFSharp project which is essentially MIT licensed.

    The code is dead simple, I needed a cmdline utility so I have more code dedicated to parsing the arguments than I do for the PDF merging:

    using (PdfDocument one = PdfReader.Open("file1.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument two = PdfReader.Open("file2.pdf", PdfDocumentOpenMode.Import))
    using (PdfDocument outPdf = new PdfDocument())
    {                
        CopyPages(one, outPdf);
        CopyPages(two, outPdf);
    
        outPdf.Save("file1and2.pdf");
    }
    
    void CopyPages(PdfDocument from, PdfDocument to)
    {
        for (int i = 0; i < from.PageCount; i++)
        {
            to.AddPage(from.Pages[i]);
        }
    }
    

提交回复
热议问题