itext7 中的 htmlConverter 在我希望在 C# 中关闭 pdf 文档之前关闭它

问题描述 投票:0回答:2

我正在使用 iText7 将 HTML 转换为 PDF,运行完美

但是这个电话:

   HtmlConverter.ConvertToPdf(htmlStream, document);

将在调用后关闭文档,但由于以下原因我还不想关闭文档

我编写了这个函数来编写页面,我将在循环中调用它

   public static void WritePage(string htmlbody)
    {
        document.AddNewPage();
        byte[] htmlByteArray = Encoding.UTF8.GetBytes(htmlbody); 
        MemoryStream htmlStream = new MemoryStream(htmlByteArray);       
        HtmlConverter.ConvertToPdf(htmlStream, document);
    }

循环结束后我会自己关闭文档

 public static void CloseDocument()
    {   
       document.Close();
    }

这在我使用 iText5 时有效,但现在 ConvertToPdf 将关闭文档。 我可以读取该文档并将其与新页面一起添加到新文档中,但我不想这样做 我想利用内置的 document.AddNewPage ,在文档完全构建后我会自己关闭它

提前谢谢您

c# itext7 html-to-pdf
2个回答
2
投票

answer#1 正在使用 PdfMerger,这是我的首选答案

public void createPdf(String baseUri, String[] src, String dest) throws IOException { 
ConverterProperties properties = new ConverterProperties();
properties.setBaseUri(baseUri);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
PdfMerger merger = new PdfMerger(pdf);
for (String html : src) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PdfDocument temp = new PdfDocument(new PdfWriter(baos));
    HtmlConverter.convertToPdf(new FileInputStream(html), temp, properties);
    temp = new PdfDocument(
        new PdfReader(new ByteArrayInputStream(baos.toByteArray())));
    merger.merge(temp, 1, temp.getNumberOfPages());
    temp.close();
}
pdf.close();}

answer#2 正在将 html 转换为 IElement List 并将其添加到文档中,如以下代码所示:

public void createPdf(String baseUri, String[] src, String dest) throws IOException { 
ConverterProperties properties = new ConverterProperties();
properties.setBaseUri(baseUri);
PdfWriter writer = new PdfWriter(dest);
PdfDocument pdf = new PdfDocument(writer);
Document document = new Document(pdf);
for (String html : src) {
    List<IElement> elements =
        HtmlConverter.convertToElements(new FileInputStream(html), properties);
    for (IElement element : elements) {
        document.add((IBlockElement)element);
    }
}
document.close();}

参考:https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about-pdfhtml/how-parse-multiple-html-文件一个 pdf


0
投票

我知道这是一个老问题,但是我找不到任何对我有用的东西。 这是我的工作样本

    string FileName = "Receipts for  " + DateTime.Now.ToString("yyyy MM dd");

// PnlLTOReceipt 是套用信函的模板 //GVTagReceipt 是一个包含信件的姓名和地址的网格视图

    using (MemoryStream memoryStream = new MemoryStream())
    {
        PdfWriter writer = new PdfWriter(memoryStream);

        PdfDocument pdfDoc = new PdfDocument(writer);
   ConverterProperties converterProperties = new ConverterProperties();

// PageCount 是 PDF 的页数

        int PageCount= GVTagReceipt.Rows.Count;

        // to convert images for inclusion in PDF

converterProperties.SetBaseUri(Request.Url.GetLeftPart(UriPartial.Authority)); 字符串 htmlContent = string.Empty;

        // Example loop to create multiple pages

        foreach (GridViewRow row in GVTagReceipt.Rows)
        {

            L1.Text = Server.HtmlDecode(row.Cells[0].Text.Trim());
            L2.text= Server.HtmlDecode(row.Cells[1].Text.Trim());
            L3.Text= Server.HtmlDecode(row.Cells[2].Text.Trim())+ ",   " + Server.HtmlDecode(row.Cells[3].Text.Trim())+ "   " + Server.HtmlDecode(row.Cells[4].Text.Trim())      ;

            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            PnlLTOReceipt.RenderControl(htmlTextWriter);
            htmlContent = htmlContent + stringWriter.ToString();

    

        }

        // Convert HTML to PDF for each page
        HtmlConverter.ConvertToPdf(htmlContent, pdfDoc, converterProperties);


        // write memorystream to PDF and Send the PDF as a response
        Response.ContentType = "application/pdf";
        Response.AddHeader("content-disposition", "attachment;filename=" + FileName + ".pdf");
        Response.BinaryWrite(memoryStream.ToArray());
        Response.End();
    }
© www.soinside.com 2019 - 2024. All rights reserved.