【问题标题】:How do i use HTML tags in iTextSharp string如何在 iTextSharp 字符串中使用 HTML 标签
【发布时间】:2012-10-18 05:01:20
【问题描述】:

我有一个这样的 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], widthoftext, v, 0); 方法的数据类型是 footer.ShowTextAligned(int, string, float, float, float); 你能帮我在上面的代码中如何使用CreateSimpleHtmlParagraph 方法吗? 亲切的问候。

【问题讨论】:

  • v0 转换为float 是否有效? (即footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v as float, 0 as float);
  • 我看不到 ShowTextAligned 方法的内容。因为它在 itextsharp.ddl 中。有什么区别?你说的地方没有问题。 ShowTextAligned 方法需要第二个元素中的字符串。但是 CreateSimpleHtmlParagraph 方法返回段落。我没有管理此转换。
  • 对不起,我想我当时没有正确理解你的问题。

标签: c# html-parsing itextsharp


【解决方案1】:

我不完全知道您是如何使用 PdfTemplate 的,但您将 iTextSharp 的文本抽象(例如 ParagraphChunk)与原始 PDF 命令(例如 ShowText())混合在一起。抽象最终使用原始命令,但可以方便地帮助您处理通常必须手动计算的换行符和当前坐标。

好消息是,只要您愿意接受有一个固定的矩形可以在其中绘制文本,就有一种称为 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#。所以我不明白你下面的代码。对不起我糟糕的学生时代。如何将下面给定的代码集成到问题中的 PdfTemplate 方法中。亲切的问候。
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 2012-11-07
  • 1970-01-01
  • 1970-01-01
  • 2018-07-28
  • 1970-01-01
  • 2021-06-14
  • 1970-01-01
相关资源
最近更新 更多