本文介绍了试图从pdfstamper获取内存流到pdfreader,但得到:“PDF startxref not found”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#应用程序,填补了一堆PDF表单,连接这些话在某些页码放。我在与来自pdfstamper将MemoryStream结果困难。如果我改变的MemoryStream到FILESTREAM它工作正常,但我不希望使用的文件系统。我创建了下面的代码片段重现我的错误:

I'm writing an app in c# that fills a bunch of pdf forms, concatenates them then puts in some page numbers. I'm having difficulty with the memorystream result from the pdfstamper. If I change the memorystream to a filestream it works fine but I don't want to use the filesystem. I've created the following code snippet that reproduces my error:

    public static void TestStreams(string filepath)
    {
        PdfReader reader = new PdfReader(filepath);
        MemoryStream ms = new MemoryStream();
        PdfReader.unethicalreading = true;
        PdfStamper stamper = new PdfStamper(reader, ms);
        byte[] result = ms.ToArray();
        //The error is in the following line
        PdfReader reader2 = new PdfReader(result);
    }



该错误是:

The error is:

iTextSharp.text.exceptions.InvalidPdfException was unhandled
  HResult=-2146232800
  Message=Rebuild failed: trailer not found.; Original message: PDF startxref not found.
  Source=itextsharp



我怎样才能解决这个问题?

How can I fix it?

推荐答案

您忘了一行:

public static void TestStreams(string filepath) {
    PdfReader reader = new PdfReader(filepath);
    MemoryStream ms = new MemoryStream();
    PdfReader.unethicalreading = true;
    PdfStamper stamper = new PdfStamper(reader, ms);
    stamper.Close();
    byte[] result = ms.ToArray();
    //The error is in the following line
    PdfReader reader2 = new PdfReader(result);
}

当你做 ms.ToArray()不首先关闭了模子,你有一个不完整的PDF文件。该PDF始于%PDF - ,但没有 %% EOF ,没有拖车,没有目录。一个不完整的PDF不能用 PdfReader 阅读。

When you do ms.ToArray() without first closing the stamper, you have an incomplete PDF. The PDF starts with %PDF-, but there's no %%EOF, no trailer, no catalog. An incomplete PDF can't be read by PdfReader.

这篇关于试图从pdfstamper获取内存流到pdfreader,但得到:“PDF startxref not found”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 08:34