简体   繁体   English

iText7 在内存中创建 PDF 而不是物理文件

[英]iText7 Create PDF in memory instead of physical file

How do one create PDF in memorystream instead of physical file using itext7?如何使用 itext7 在内存流中创建 PDF 而不是物理文件? I have no idea how to do it in the latest version, any help?我不知道如何在最新版本中做到这一点,有什么帮助吗?

I tried the following code, but pdfSM is not properly populated:我尝试了以下代码,但 pdfSM 未正确填充:

string filePath = "./abc.pdf";

MemoryStream pdfSM = new ByteArrayOutputStream();

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

doc.close();

The full testing code as below for your reference, it worked when past filePath into PdfWriter but not for the memory stream:完整的测试代码如下供您参考,它在将 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();

    }

This works for me.这对我有用。

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

I needed the same thing.我需要同样的东西。 Got it working like this: (I included some settings which improve performance)让它像这样工作:(我包括了一些提高性能的设置)

 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(HtmlString, pdfDoc, props));
           pdfDoc.Close();
        }
     }
     buffer = memStream.ToArray();
 }
 return buffer;

iText7、C# Controller iText7、C#控制器

Error:错误:

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

Work:工作:

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

I don't know why... but, it's working!我不知道为什么......但是,它的工作!

another: link另一个: 链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM