2011-02-14 39 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"); 
     }