iTextSharp 给我错误:“未找到 PDF 标头签名" [英] iTextSharp is giving me the error: "PDF header signature not found"

查看:79
本文介绍了iTextSharp 给我错误:“未找到 PDF 标头签名"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所读到的关于此错误的所有内容都表明该文件必须在顶部缺少%PDF-1.4"或类似内容;但是,我的文件包含它.我不是 PDF 格式方面的专家,但我确实仔细检查过我没有多个 %%EOF 或拖车标签,所以现在我不知道是什么导致我的 PDF 标头签名不好.如果您想查看该文件,请访问该文件的链接:格式不佳的 PDF

Everything I have read about this error says the file must be missing a "%PDF-1.4" or something similar at the top; however, my file includes it. I am not an expert in PDF formatting, but I did double check that I don't have multiple %%EOF or trailer tags, so now I'm at a loss as to what is causing my PDF header signature to be bad. Here is a link to the file if you would like to look at it: Poorly formatted PDF

这就是我正在做的事情.我以 MemoryStream 的形式获取 PDF 的每一页,因此我必须将每一页附加到前几页的末尾.为了做到这一点,我使用了 iTextSharp 的 PdfCopy 类.这是我正在使用的代码:

Here is what I'm doing. I am getting each page of the PDF in the form of a MemoryStream, so I have to append each page to the end of the previous pages. In order to do this, I am using iTextSharp's PdfCopy class. Here is the code I am using:

    /// <summary>
    /// Takes two PDF streams and appends the second onto the first.
    /// </summary>
    /// <param name="firstPdf">The PDF to which the other document will be appended.</param>
    /// <param name="secondPdf">The PDF to append.</param>
    /// <returns>A new stream with the second PDF appended to the first.</returns>
    public Stream ConcatenatePdfs(Stream firstPdf, Stream secondPdf)
    {
        // If either PDF is null, then return the other one
        if (firstPdf == null) return secondPdf;
        if (secondPdf == null) return firstPdf;
        var destStream = new MemoryStream();

        // Set the PDF copier up.
        using (var document = new Document())
        {
            using (var copy = new PdfCopy(document, destStream))
            {
                document.Open();
                copy.CloseStream = false;

                // Copy the first document
                using (var reader = new PdfReader(firstPdf))
                {
                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        copy.AddPage(copy.GetImportedPage(reader, i));
                    }
                }

                // Copy the second document
                using (var reader = new PdfReader(secondPdf))
                {
                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        copy.AddPage(copy.GetImportedPage(reader, i));
                    }
                }
            }
        }
        return destStream;
    }

每次我收到一个新的 PDF 页面时,我都会将先前连接的页面 (firstPdf) 与新页面 (secondPdf) 一起传递给此函数.对于第一页,我没有任何先前连接的页面,因此 firstPdf 为空,从而导致 secondPdf 作为结果返回.我第二次通过时,第一页作为 firstPdf 传入,新的第二页作为 secondPdf 传入.连接工作得很好,结果实际上在上面链接的 First.pdf 文件中.

Every time I receive a new PDF page, I pass the previously concatenated pages (firstPdf) along with the new page (secondPdf) to this function. For the first page, I don't have any previously concatenated pages, so firstPdf is null, thereby resulting in secondPdf being returned as the result. The second time I go through, the first page is passed in as firstPdf and the new second page is passed in as secondPdf. The concatenation works just fine and the results are actually in the First.pdf file linked above.

问题是当我去添加第三页时.我使用前一阶段(前两页)的输出作为第三阶段的输入,以及一个新的 PDF 流.当我尝试使用之前连接的 PDF 页面初始化 PdfReader 时出现异常.

The problem is when I go to add a third page. I am using the output of the previous pass (the first two pages) as the input for the third pass, along with a new PDF stream. The exception occus when I try to initialize the PdfReader with the PDF previously concatenated pages.

我觉得特别有趣的是它无法读取自己的输出.我觉得我一定是做错了什么,但我既不知道如何避免这个问题,也不知道为什么标题会出现问题;对我来说看起来很正常.如果有人能告诉我我的代码有什么问题,或者至少 PDF 文件有什么问题,我将不胜感激.

What I find particularly interesting is that it fails to read its own output. I feel like I must be doing something wrong, but I can neither figure out how to avoid the problem, nor why there is a problem with the header; it looks perfectly normal to me. If someone could show me either what I'm doing wrong with the my code or at least what is wrong with the PDF file, I would really appreciate it.

推荐答案

(评论回答)

我强烈建议不要传递原始流本身,而是通过在您的 MemoryStream 上调用 .ToArray() 来传递字节数组.iTextSharp 假定它具有用于写入的专用空流,因为它无法就地"编辑现有文件.尽管流本质上映射到字节,但它们也具有固有的属性,例如 OpenClosedPosition,这些属性可能会把事情搞砸.

I strongly recommend not passing the raw streams themselves around and instead pass around a byte array by calling .ToArray() on your MemoryStream. iTextSharp assumes that is has a dedicated empty stream for writing to since it can't edit existing files "in-place". Although streams essentially map to bytes, they also have inherent properties like Open and Closed and Position that can mess things up.

这篇关于iTextSharp 给我错误:“未找到 PDF 标头签名"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆