尝试使用 iTextSharp v4 合并 PDF 会引发异常

发布于 2024-11-17 23:05:48 字数 1796 浏览 4 评论 0原文

我尝试了几个不同的代码示例,它们都抛出相同的异常:

System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'iTextSharp.text.pdf.PdfArray' to type 'iTextSharp.text.pdf.PRIndirectReference'.
  Source=itextsharp
  StackTrace:
       at iTextSharp.text.pdf.PdfCopy.CopyObject(PdfObject inp)
       at iTextSharp.text.pdf.PdfCopy.CopyDictionary(PdfDictionary inp)
       at iTextSharp.text.pdf.PdfCopy.AddPage(PdfImportedPage iPage)

此示例使用 PdfCopy。我也用 PdfWriter 尝试过:

    public MemoryStream Merge(MemoryStream outputStream,List<PdfReader> documents)
    {
        if (outputStream == null || !outputStream.CanWrite)
            throw new Exception("OutputStream is null or you can't write to it.");

        Document newDocument = null;
        try
        {
            newDocument = new Document(documents[0].GetPageSizeWithRotation(1));
            PdfCopy pdfWriter = new PdfCopy(newDocument, outputStream);

            newDocument.Open();
            //PdfContentByte pdfContentByte = pdfWriter.DirectContent;

            foreach (PdfReader pdfReader in documents)
            {
                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    //newDocument.NewPage();
                    PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, page);
                    pdfWriter.AddPage(importedPage);
                }
            }
        }
        finally
        {
            outputStream.Flush();
            if (newDocument != null)
                newDocument.Close();
            outputStream.Close();
        }
        return outputStream;
    }

使用此代码,异常发生在 AddPage 处。在 PdfWriter 上,它发生在文档关闭时。我真的不太了解 iTextSharp 的内部结构......

I have tried a couple of different code samples and they all throw the same exception:

System.InvalidCastException was unhandled by user code
  Message=Unable to cast object of type 'iTextSharp.text.pdf.PdfArray' to type 'iTextSharp.text.pdf.PRIndirectReference'.
  Source=itextsharp
  StackTrace:
       at iTextSharp.text.pdf.PdfCopy.CopyObject(PdfObject inp)
       at iTextSharp.text.pdf.PdfCopy.CopyDictionary(PdfDictionary inp)
       at iTextSharp.text.pdf.PdfCopy.AddPage(PdfImportedPage iPage)

This example uses PdfCopy. I have also tried it with PdfWriter:

    public MemoryStream Merge(MemoryStream outputStream,List<PdfReader> documents)
    {
        if (outputStream == null || !outputStream.CanWrite)
            throw new Exception("OutputStream is null or you can't write to it.");

        Document newDocument = null;
        try
        {
            newDocument = new Document(documents[0].GetPageSizeWithRotation(1));
            PdfCopy pdfWriter = new PdfCopy(newDocument, outputStream);

            newDocument.Open();
            //PdfContentByte pdfContentByte = pdfWriter.DirectContent;

            foreach (PdfReader pdfReader in documents)
            {
                for (int page = 1; page <= pdfReader.NumberOfPages; page++)
                {
                    //newDocument.NewPage();
                    PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, page);
                    pdfWriter.AddPage(importedPage);
                }
            }
        }
        finally
        {
            outputStream.Flush();
            if (newDocument != null)
                newDocument.Close();
            outputStream.Close();
        }
        return outputStream;
    }

With this code, the exception happens at AddPage. On a PdfWriter, it happens at document close. I really don't know the iTextSharp internals all that well...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文