【问题标题】:Add multiple Checkboxes in ITextsharp html to pdf将 ITextsharp html 中的多个复选框添加到 pdf
【发布时间】:2022-01-05 12:14:26
【问题描述】:

我正在使用 iTextSharp 库将我的 html 转换为 pdf。问题是我正在尝试使用以下代码添加复选框外观:

string HTML,public static String FONT = "c:/windows/fonts/WINGDING.TTF";
 
public static String TEXT = "o";
        
public void HTMLToPdf( string FileName)
{
    string HTML="<!DOCTYPE html>
                 <html>
                   <head><title></title><meta charset='UTF-8'></head>
                   <body><div class='mystyle'>Here i want to print many checkbox lik appearances</div></body>
                 <html>";
    Document pdfDoc = new Document(PageSize.A4, 30f, 30f, 10f, 10f);
    pdfDoc.Add(p);
    BaseFont bf = BaseFont.CreateFont(FONT, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font f = new Font(bf, 12);
    Paragraph p = new Paragraph(TEXT, f);
    pdfDoc.Add(p);
}

问题是这种方法在pdf开头添加了复选框,请帮我将包含复选框值的段落附加到我的html中。 简而言之,我在 pdfDoc.Add(p) 处获取值,但我希望它在一个变量中以在 html 中多次打印它。

【问题讨论】:

  • 嗨,你试过 iText 7 吗?

标签: c# itext


【解决方案1】:

其实问题的意思不是很清楚:

请帮我将包含复选框值的段落附加到我的 html 中

我可以假设您想将 HTML 转换为 PDF,并在下一行添加段落。 通常,为此使用 iTextSharp 是个坏主意,因为该库已过时且不再受支持。我可以建议我自己用 pdfHTML 解决问题的方法,这是一个 iText7 插件。我的代码是Java的,但是和sharp没有太大区别。主要思想是html转换后不要关闭文档。因为如果你试图在一个封闭的文档中写一个段落,它会在开头,就像你的例子一样。

 String FONT = "c:/windows/fonts/WINGDING.TTF";
    String TEXT = "o";
    File htmlSource = new File("checkBoxHtml.html");
    File pdfDest = new File("output.pdf");

    ConverterProperties converterProperties = new ConverterProperties();

    Document document = HtmlConverter.convertToDocument(new FileInputStream(htmlSource),
            new PdfDocument(new PdfWriter(pdfDest)), converterProperties);
    PdfFont font = PdfFontFactory.createFont(FONT);
    Text text = new Text(TEXT);
    text.setFont(font);
    Paragraph paragraph = new Paragraph();

    // Adding text to the paragraph
    paragraph.add(text);

    // Adding paragraph to the document
    document.add(paragraph);
    document.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-02
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-18
    相关资源
    最近更新 更多