c# - 我如何在 iTextSharp 字符串中使用 HTML 标签

标签 c# html-parsing itextsharp

我有一个像这样的 iTextSharp 页脚模板方法:

public PdfTemplate footerTemplate(){
    PdfTemplate footer = cb.CreateTemplate(500, 50);
    footer.BeginText();
    BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, "windows-1254", BaseFont.NOT_EMBEDDED);
    footer.SetFontAndSize(bf2, 11);
    footer.SetColorStroke(BaseColor.DARK_GRAY);
    footer.SetColorFill(BaseColor.GRAY);
    int al = -200;
    int v = 45 - 15;
        float widthoftext = 500.0f - bf2.GetWidthPoint(footerOneLine[0], 11);
        footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v, 0);
    footer.EndText();
    return footer;
}

footerTemplate() 正在获取这样的字符串:

footerOneLine.Add("<b>All this line is bold, and <u>this is bold and underlined</u></b>");

我有另一种方法可以将字符串转换为 HTML。方法是:

private Paragraph CreateSimpleHtmlParagraph(String text) {
    //Our return object
    Paragraph p = new Paragraph();

    //ParseToList requires a StreamReader instead of just text
    using (StringReader sr = new StringReader(text)) {
        //Parse and get a collection of elements
        List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null);
        foreach (IElement e in elements) {
            //Add those elements to the paragraph
            p.Add(e);
        }
    }
    //Return the paragraph
    return p;
}

问题:我没有设法在上面的代码中使用 CreateSimpleHtmlParagraph 方法。 footer.ShowTextAligned(al, footerOneLine[0], widtoftext, v, 0);方法的数据类型是footer.ShowTextAligned(int, string, float, float, float); 你能帮我如何在上面的代码中使用 CreateSimpleHtmlParagraph 方法吗? 亲切的问候。

最佳答案

我不完全知道你是如何使用 PdfTemplate 但你混合了 iTextSharp 的文本抽象,例如 ParagraphChunk使用像 ShowText() 这样的原始 PDF 命令。抽象最终使用原始命令,但可以方便地帮助您解决通常需要手动计算的换行符和当前坐标等问题。

好消息是,有一个名为 ColumnText 的抽象可以直接与 PdfWriter.PdfContentByte 对象一起使用,只要你愿意接受你有一个用于绘制文本的固定矩形。

//Create a ColumnText from the current writer
var ct = new ColumnText(writer.DirectContent);
//Set the dimensions of the ColumnText
ct.SetSimpleColumn(0, 0, 500, 0 + 20);
//Create two fonts
var helv_n = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
var helv_b = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED);
//Create a paragraph
var p = new Paragraph();
//Add two chunks to the paragraph with different fonts 
p.Add(new Chunk("Hello ", new iTextSharp.text.Font(helv_n, 12)));
p.Add(new Chunk("World", new iTextSharp.text.Font(helv_b, 12)));
//Add the paragraph to the ColumnText
ct.AddElement(p);
//Tell the ColumnText to draw itself
ct.Go();

关于c# - 我如何在 iTextSharp 字符串中使用 HTML 标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13112171/

相关文章:

c# - 如何使用 itextsharp 在 PdfContentByte 矩形中添加文本?

c# - Linq 快速相交查询 - 增强?

c# - 使用 C# 从 Excel 单元格中检索数据

c# - 使用 .NET Framework 4.6.1 构建 .NET 标准库 1.4,无需 Visual Studio

java - 如何从类内同名但数据不同的类中获取数据?

r - R中xpathSApply的正确语法

asp.net - 如何使用 webmatrix 将图像添加到 iTextSharp 中的表格单元格

c# - 如何访问 RunSettings 文件中的 TestRunParameters

Xpath 找到一个没有特定文本的子元素的 div

c# - 如何使用 ITextSharp.dll 在 C# 中屏蔽图像?