【问题标题】:iText7 Create PDF in memory instead of physical fileiText7 在内存中创建 PDF 而不是物理文件
【发布时间】:2017-03-25 06:09:47
【问题描述】:

如何使用 itext7 在内存流中创建 PDF 而不是物理文件? 我不知道如何在最新版本中做到这一点,有什么帮助吗?

我尝试了以下代码,但没有正确填充 pdfSM:

string filePath = "./abc.pdf";

MemoryStream pdfSM = new ByteArrayOutputStream();

PdfDocument doc = new PdfDocument(new PdfReader(filePath), new PdfWriter(pdfSM));
.......

doc.close();

完整的测试代码如下供您参考,它在将 filePath 传递到 PdfWriter 时有效,但不适用于内存流:

    public static readonly String sourceFolder = "../../FormTest/";

    public static readonly String destinationFolder = "../../Output/";

    static void Main(string[] args)
    {

        String srcFilePattern = "I-983";
        String destPattern = "I-129_2014_";

        String src = sourceFolder + srcFilePattern + ".pdf";
        String dest = destinationFolder + destPattern + "_flattened.pdf";
        MemoryStream returnSM = new MemoryStream();

        PdfDocument doc = new PdfDocument(new PdfReader(src), new PdfWriter(returnSM));

        PdfAcroForm form = PdfAcroForm.GetAcroForm(doc, false);

        foreach (PdfFormField field in form.GetFormFields().Values) 
        {
            var fieldName = field.GetFieldName();
            var type = field.GetType();
            if (fieldName != null)
            {
                if (type.Name.Equals("PdfTextFormField"))
                {
                        field.SetValue("T");
                }
            }               
        }
        form.FlattenFields();         
        doc.Close();

    }

【问题讨论】:

  • “pdfSM 未正确填充”,您能否定义一下您的实际意思?您是否收到任何编译错误?您是否遇到任何运行时错误?
  • PdfWriter 类支持 filePath 作为构造函数和流,当我传入路径时,它与创建的 PDF 一起工作,但是,传递 Stream 没有返回任何内容,没有编译或运行时错误,因为我是 iText 的新手,也许我只是不知道如何使用这个类,这就是我需要帮助的原因
  • 我没有看到你关闭doc,也没有看到你在流中检索数据gem。
  • 是否可以在不创建物理文件的情况下将文件保存到内存流?
  • @ZachJinUS-Advisory 是的,但我没有看到您在任何地方关闭doc,因此很可能完整的PDF 永远不会写入MemoryStream。 (这可以解释您的问题。)或者您使用错误的方法从MemoryStream 获取byte[]。很难说,因为您发布的问题不到一半。

标签: c# pdf memory itext


【解决方案1】:

这对我有用。

    public byte[] CreatePdf()
    {
        var stream = new MemoryStream();
        var writer = new PdfWriter(stream);
        var pdf = new PdfDocument(writer);
        var document = new Document(pdf);

        document.Add(new Paragraph("Hello world!"));
        document.Close();

        return stream.ToArray();
    }

【讨论】:

  • 嗨,弗雷德,这是不对的。当您关闭文档时,PdfWriter 中的 MemoryStream 也会关闭,因为它是 IDisposable 并且在调用 document.Close() 时会立即调用 stream.dispose()。尝试访问流时抛出异常:System.ObjectDisposedException: 'Cannot access a closed Stream.'
  • 但是我昨天使用了这个代码,它对我有用。
  • 您确定您使用的是 iText 7 吗?因为我现在正在使用它,但它不起作用。
  • 是的,我刚刚尝试了这个确切的代码(在控制台应用程序中)并且可以确认它适用于 .NET Core 3.1 上的 iText 7 版本 7.1.9。
  • 我在 .NET Standard 2.0 中也遇到了 ObjectDisposedException。添加这一行解决了这个问题: writer.SetCloseStream(false);
【解决方案2】:

iText7、C#控制器

错误:

public ActionResult Report()
{
    //...
    doc1.Close();
    return File(memoryStream1, "application/pdf", "pdf_file_name.pdf");
}

工作:

public ActionResult Report()
{
    //...
    doc1.Close();
    byte[] byte1 = memoryStream1.ToArray();
    return File(byte1, "application/pdf", "pdf_file_name.pdf");
}

我不知道为什么...但是,它正在工作!

另一个:link

【讨论】:

    【解决方案3】:

    我需要同样的东西。让它像这样工作: (我包括了一些提高性能的设置)

     string HtmlString = "<html><head></head><body>some content</body></html>";
     byte[] buffer;
     PdfDocument pdfDoc = null;
     using (MemoryStream memStream = new MemoryStream())
     {
         using(PdfWriter pdfWriter = new PdfWriter(memStream, wp))
         {
            pdfWriter.SetCloseStream(true);
            using (pdfDoc = new PdfDocument(pdfWriter))
            {
               ConverterProperties props = new ConverterProperties();
    
               pdfDoc.SetDefaultPageSize(PageSize.LETTER);
               pdfDoc.SetCloseWriter(true);
               pdfDoc.SetCloseReader(true);
               pdfDoc.SetFlushUnusedObjects(true);
               HtmlConverter.ConvertToPdf(reader, pdfDoc, props));
               pdfDoc.Close();
            }
         }
         buffer = memStream.ToArray();
     }
     return buffer;
    

    【讨论】:

    • 您在pdfDoc.Close() 之前检索memStream.ToArray()。因此,您检索的 pdf 很可能是不完整的。
    • 谢谢@mkl,我会在几个小时内阅读并进行相应的编辑。
    猜你喜欢
    • 2011-02-18
    • 2020-11-11
    • 2015-03-04
    • 2014-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    相关资源
    最近更新 更多