使用PDFSharp组合多个PDF

问题描述 投票:17回答:3

我正在尝试将多个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的其他建议持开放态度,但我不希望使用另一个IrdSharp之类的第三方库,因为该项目中已经使用了PDFSharp。

如果有问题,我正在Win7机器上使用VS2010 Pro。

EDIT:从异常中调用堆栈:

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?

c# ssrs-2008 pdfsharp
3个回答
8
投票

我已经相信输入的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报告可尝试)


3
投票

我不确定我的回答。请阅读您的自我。

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

1
投票

首先,感谢您的反馈。问题不来自压缩,因为我的设备信息字符串中有<humanreadalble> true </humanreadable>,否则PDFSharp根本看不到PDF中的任何内容。

我尝试从最新的源代码重新编译PDFSharp,并且它起作用了...它不再抛出异常。奇怪的是,我检查了dll的版本,它与最新版本相同。也许他们在不增加版本的情况下解决了某些问题?

无论如何,谢谢您的帮助。我接受了您的帖子作为对我表示感谢的答案。

© www.soinside.com 2019 - 2024. All rights reserved.