Combining multiple PDFs using PDFSharp
Asked Answered
G

3

25

I am trying to combine multiple PDFs into a single PDF. The PDFs come from SSRS, from some LocalReports that I processed. I am using PDFSharp, because it is already used through out the project. However, the outputDocument.addPage(page) methods throws an InvalidOperationException("Cannot change document.") exception. I have tried many different way of doing this, but I can't get it to work...

Here my method, where all the inputs have already been checked:

private static void saveFile(string fileName, params byte[][] bytes)
{
    try
    {
        PdfDocument outputDocument = new PdfDocument();
        for (int i = 0; i < bytes.Length; i++)
        {
            using (MemoryStream stream = new MemoryStream(bytes[i]))
            {
                PdfDocument inputDocument = PdfReader.Open(stream, PdfDocumentOpenMode.Import);
                foreach (PdfPage page in inputDocument.Pages)
                {
                    outputDocument.AddPage(page); //throws the exception !!!
                }
            }
        }
        outputDocument.Save(fileName);  
    }
    catch (Exception ex)
    {
        throw new Exception("Erreur lors de l'enregistrement du fichier", ex);
    }
}

From the examples I saw on the web, this seems to be the right way of doing this... I am opened to other suggestions for merging my PDFs, but I would rather not use another 3rd party lib, like ITextSharp, because PDFSharp is already used in the project.

If it matters, I am using VS2010 Pro on a Win7 machine.

EDIT : Call stack from the exception :

at PdfSharp.Pdf.PdfObject.set_Document(PdfDocument value)  
at PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObjectTable, PdfDocument owner, PdfObject externalObject)  
at PdfSharp.Pdf.PdfPages.CloneElement(PdfPage page, PdfPage importPage, String key, Boolean deepcopy)  
at PdfSharp.Pdf.PdfPages.ImportExternalPage(PdfPage importPage)  
at PdfSharp.Pdf.PdfPages.Insert(Int32 index, PdfPage page)  
at PdfSharp.Pdf.PdfPages.Add(PdfPage page)  
at PdfSharp.Pdf.PdfDocument.AddPage(PdfPage page)  
at Something.saveFile(String fileName, Byte[][] bytes)  

Is the problem me? Isn't this the way this is supposed to be done? Or is there any other way of combining multiple LocalReport into a single PDF?

Gregory answered 14/2, 2011 at 17:35 Comment(3)
Could you please provide a callstack from the Exception?Vice
I have just tried your code with the newest build of PDFSharp and it works for me. It generates many pages and doesn't throw. Could you try with another set of input PDFs?Vice
I really can't make it throw. I have tried newest pre-built version both WPF and GDI+ and compiled from source. Your code works on my computer :)Vice
V
8

I have come to believe that it might be the input PDFs that are corrupt or unreadable to PDFSharp. There are several examples of SSRS PDFs not being readable to PDF-libraries or even Adobe's Reader. For example here:

http://www.sqldev.org/sql-server-reporting-services/export-pdf-in-ssrs-2008-vs-ssrs-2005--pdf-is-different-wont-work-with-itextsharp-possibly-other-13968.shtml

... and here:

https://stackoverflow.com/questions/2393175/ssrs-2008-pdf-files-cannot-be-opened

... AND most importantly on the PDFSharp forum:

http://forum.pdfsharp.net/viewtopic.php?f=2&t=674

I don't know if this is the bug you're encountering - the message is strange - but it seems likely to have something to do with that, when you take in to consideration that your code sample works flawlessly with any PDF I tried (I don't have any SQL Server Reports to try out, though)

Vice answered 14/2, 2011 at 21:38 Comment(2)
Compressed PDF files are a good example of PDF files not readable by PDFSharp... that is why it cannot read SSRS 2008 PDF files and reads SSRS 2005 files just fine... However, wouldn't it generate the exception in the line above the foreach loop if that was the problem here?Simonasimonds
I would think so too yes. It's a peculiar message and location if it's only compression. I think that during the Open() something gets setup wrong, so that when the cloning of the page happens, it fails. But that leads to that it can be something else than compression that is "corrupt".Vice
E
11

I don't sure about my answer. Please read your self.

http://www.go4coding.com/post/2011/05/26/Merging-PDF-files-into-single-PDF-in-CSharp-using-PDFSharp.aspx

private static void MergeMultiplePDFIntoSinglePDF(string outputFilePath, string[] pdfFiles)
{
    Console.WriteLine("Merging started.....");
    PdfDocument outputPDFDocument = new PdfDocument(); 
    foreach (string pdfFile in pdfFiles)
    {
        PdfDocument inputPDFDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
        outputPDFDocument.Version = inputPDFDocument.Version; 
        foreach (PdfPage page in inputPDFDocument.Pages)
        {
            outputPDFDocument.AddPage(page);
        }
    }
    outputPDFDocument.Save(outputFilePath); 
    Console.WriteLine("Merging Completed");
}
Energy answered 14/7, 2013 at 14:35 Comment(2)
the link is deadGillman
use this link instead. pdfsharp.net/wiki/ConcatenateDocuments-sample.ashxAnthocyanin
V
8

I have come to believe that it might be the input PDFs that are corrupt or unreadable to PDFSharp. There are several examples of SSRS PDFs not being readable to PDF-libraries or even Adobe's Reader. For example here:

http://www.sqldev.org/sql-server-reporting-services/export-pdf-in-ssrs-2008-vs-ssrs-2005--pdf-is-different-wont-work-with-itextsharp-possibly-other-13968.shtml

... and here:

https://stackoverflow.com/questions/2393175/ssrs-2008-pdf-files-cannot-be-opened

... AND most importantly on the PDFSharp forum:

http://forum.pdfsharp.net/viewtopic.php?f=2&t=674

I don't know if this is the bug you're encountering - the message is strange - but it seems likely to have something to do with that, when you take in to consideration that your code sample works flawlessly with any PDF I tried (I don't have any SQL Server Reports to try out, though)

Vice answered 14/2, 2011 at 21:38 Comment(2)
Compressed PDF files are a good example of PDF files not readable by PDFSharp... that is why it cannot read SSRS 2008 PDF files and reads SSRS 2005 files just fine... However, wouldn't it generate the exception in the line above the foreach loop if that was the problem here?Simonasimonds
I would think so too yes. It's a peculiar message and location if it's only compression. I think that during the Open() something gets setup wrong, so that when the cloning of the page happens, it fails. But that leads to that it can be something else than compression that is "corrupt".Vice
G
1

First of all, thank you for your feedback. The problem doesn't come from the compression because I have <humanreadalble>true</humanreadable> in my device info string, otherwise PDFSharp just can't see anything in the PDF.

I tried recompiling PDFSharp from the latest source code, and it worked... It doesn't throw the exception anymore. The weird thing is that I checked the version of my dll and it was the same as the latest build. Maybe they fixed something without incrementing the version ?

Anyway, thanks for your assistance. I accepted your post as the answer to show my appreciation.

Gregory answered 15/2, 2011 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.