2011-02-14 38 views
16

多个PDF我尝试多个PDF合并成单一的PDF。 PDFs来自SSRS,来自我处理的一些LocalReports。我正在使用PDFSharp,因为它已在整个项目中使用。但是,outputDocument.addPage(page)方法会引发InvalidOperationException(“无法更改文档”)异常。我试图做的许多不同的方式,但我不能得到它的工作...结合使用PDFSharp

这里我的方法,所有的投入都已经被检查:

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); 
    } 
} 

从我的例子看到在网络上,这似乎是这样做的正确的方式... 我打开其他建议合并我的PDF文件,但我宁可不使用其他第三方的lib,就像iTextSharp的,因为PDFSharp在已经使用该项目。

如果它的事项,我使用的是Win7的机器上VS2010专业版。

编辑:
在PdfSharp.Pdf.PdfObject.set_Document(PdfDocument值)
在PdfSharp.Pdf.PdfObject.ImportClosure(PdfImportedObjectTable importedObjectTable,PdfDocument所有者,PdfObject externalObject)
:从异常调用堆栈在PdfSharp.Pdf.PdfPages.CloneElement(PdfPage页,PdfPage importPage,串键,布尔deepcopy的)
在PdfSharp.Pdf.PdfPages.ImportExternalPage(PdfPage importPage)
在PdfSharp.Pdf.PdfPages.Insert(的Int32指数,PdfPage页)
a吨PdfSharp.Pdf.PdfPages.Add(PdfPage页)
在PdfSharp.Pdf.PdfDocument.AddPage(PdfPage页)
在Something.saveFile(字符串文件名,字节[] []字节)

问题是可我 ?这是不是应该这样做? 或者是否有其他方式将多个LocalReport合并到一个PDF中?

+0

出现InvalidOperationException – 2011-02-14 18:44:35

+0

能否请您提供从Exception调用堆栈? – asgerhallas 2011-02-14 18:58:47

回答

6

我终于相信,这可能是输入PDF文件是损坏或无法读取PDFSharp。有几个SSRS PDF文件不能被PDF库或Adobe Reader读取。例如这里:

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

...这里:

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

...和最重要的PDFSharp论坛:

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

我不不知道这是否是你遇到的错误 - 这个消息很奇怪 - 但它似乎有可能东西做的是,当你在考虑到您的代码示例与我试过任何PDF完美的作品(我没有任何SQL Server报表尝试,虽然)

1

首先,感谢你您的反馈。这个问题并非来自压缩,因为我在我的设备信息字符串<humanreadalble>真正</humanreadable>,否则PDFSharp只是不能看到任何PDF。

我尝试从最新的源代码重新编译PDFSharp,它的工作...它不会抛出异常了。奇怪的是我检查了我的dll版本,它和最新版本一样。也许他们修正了一些东西而不增加版本?

无论如何,感谢您的协助。我接受你的文章作为答复,以表示我的赞赏。

2

我不确定我的答案。请阅读你的自我。

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"); 
     }