Tuesday, May 3, 2011

Combine two (or more) PDF's

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

Problem: I would like to allow a user to run all reports and also just run a single report. I was thinking I could do this by creating the reports and then doing:

List<ReportClass> reports = new List<ReportClass>();
reports.Add(new WeeklyReport1());
reports.Add(new WeeklyReport2());
reports.Add(new WeeklyReport3());
<snip>

foreach (ReportClass report in reports)
{
    report.ExportToDisk(ExportFormatType.PortableDocFormat, @"c:\reports\" + report.ResourceName + ".pdf");
}

This would provide me a folder full of the reports, but I would like to email everyone a single PDF with all the weekly reports. So I need to combine them.

Is there an easy way to do this without install any more third party controls? I already have DevExpress & CrystalReports and I'd prefer not to add too many more.

Would it be best to combine them in the foreach loop or in a seperate loop? (or an alternate way)

Thanks

From stackoverflow
  • PDFsharp seems to allow merging multiple PDF documents into one.

    And the same is also possible with ITextSharp.

  • 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]);
        }
    }
    
    Andrew Burns : Ah looks like Martin beat me to it, I am saying it was because I was diging up my code sample :)
  • Here is a link to an example using PDFSharp and ConcatenateDocuments

  • Here the solution http://www.wacdesigns.com/2008/10/03/merge-pdf-files-using-c It use free open source iTextSharp library http://sourceforge.net/projects/itextsharp

  • I've done this with PDFBox. I suppose it works similarly to iTextSharp.

  • You could try pdf-shuffler gtk-apps.org

  • I know a lot of people have recommended PDF Sharp, however it doesn't look like that project has been updated since june of 2008. Further, source isn't available.

    Personally, I've been playing with iTextSharp which has been pretty easy to work with.

  • There's some good answers here already, but I thought I might mention that pdftk might be useful for this task. Instead of producing one PDF directly, you could produce each PDF you need and then combine them together as a post-process with pdftk. This could even be done from within your program using a system() or ShellExecute() call.

0 comments:

Post a Comment