我正在尝试将多个PDF合并到一个PDF中。PDF来自SSRS,来自我处理过的一些LocalReports。我正在使用PDFSharp,因为它已经在整个项目中使用了。但是,outputDocument.addPage(page)方法会引发InvalidOperationException("Cannot change document.")异常。我试过很多不同的方法,但我不能让它起作用.
在这里,我的方法已经检查了所有输入:
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的建议,但我不想再使用第三方库,比如ITextSharp,因为PDFSharp已经在项目中使用了。
如果有关系的话,我正在Win7机器上使用Win7 Pro。
编辑:从异常调用堆栈:
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) 是我的问题吗?这不是我们应该做的吗?或者有没有其他方法将多个LocalReport合并到一个PDF中?
发布于 2011-02-14 21:38:43
我已经开始相信,可能是输入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完美地工作时,这似乎可能与此有关(不过,我没有任何Server报告可供试用)。
发布于 2013-07-14 14:35:14
我不确定我的答案。请读你的自我。
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");
}发布于 2011-02-15 14:09:04
首先,感谢您的反馈。问题不是来自压缩,因为我的设备信息字符串中有<humanreadalble>真</humanreadable>,否则PDFSharp就看不到PDF中的任何内容。
我试着从最新的源代码中重新编译PDFSharp,它成功了.它不再抛出异常。奇怪的是,我检查了dll的版本,它与最新的版本相同。也许他们在不增加版本的情况下修复了什么东西?
不管怎样,谢谢你的帮助。我接受你的职位来表达我的感激之情。
https://stackoverflow.com/questions/4995263
复制相似问题