如何将富文本框 (HTML) 添加到表格单元格? [英] How to add a rich Textbox (HTML) to a table cell?

查看:60
本文介绍了如何将富文本框 (HTML) 添加到表格单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为DocumentContent"的富文本框,我将使用以下代码将其内容添加到 pdf:

I have a rich text box named:"DocumentContent" which I’m going to add its content to pdf using the below code:

iTextSharp.text.Font font = FontFactory.GetFont(@"C:WindowsFontsarial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 12f, Font.NORMAL, BaseColor.BLACK);
            DocumentContent = System.Web.HttpUtility.HtmlDecode(DocumentContent);
            Chunk chunkContent = new Chunk(DocumentContent);
            chunkContent.Font = font;            

            Phrase PhraseContent = new Phrase(chunkContent);
            PhraseContent.Font = font;


            PdfPTable table = new PdfPTable(2);
            table.WidthPercentage = 100;
            PdfPCell cell;

            cell = new PdfPCell(new Phrase(PhraseContent));
            cell.Border = Rectangle.NO_BORDER;
            table.AddCell(cell);

问题是当我打开 PDF 文件时,内容显示为 HTML,而不是如下所示的文本:

The problem is when I open PDF file the content appears as HTML not a text as below:

<p>Overview&#160; line1 </p><p>Overview&#160; line2
</p><p>Overview&#160; line3 </p><p>Overview&#160;
line4</p><p>Overview&#160; line4</p><p>Overview&#160;
line5&#160;</p>

但它应该看起来像下面

Overview line1
Overview line2
Overview line3
Overview line4
Overview line4
Overview line5

我要做的是保留用户应用于富文本的所有样式,并将字体系列更改为 Arial.

What I'm going to do is to keep all the styling which user apply to the rich text and just change font family to Arial.

我可以更改字体系列,但我需要将此内容从 HTML 解码为文本.

I can change Font Family but I need to Decode this content from HTML to Text.

你能指点一下吗?谢谢

推荐答案

请看HtmlContentForCell 示例.

在这个例子中,我们有你提到的 HTML:

In this example, we have the HTML you mention:

public static final String HTML = "<p>Overview&#160;line1</p>"
        + "<p>Overview&#160;line2</p><p>Overview&#160;line3</p>"
        + "<p>Overview&#160;line4</p><p>Overview&#160;line4</p>"
        + "<p>Overview&#160;line5&#160;</p>";

我们还为 <p> 标签创建了一个字体:

We also create a font for the <p> tag:

public static final String CSS = "p { font-family: Cardo; }";

在您的情况下,您可能希望将 Cardo 替换为 Arial.

In your case, you may want to replace Cardo with Arial.

请注意,我们注册了 Cardo 字体的常规版本:

Note that we registered the regular version of the Cardo font:

FontFactory.register("resources/fonts/Cardo-Regular.ttf");

如果您需要粗体、斜体和粗斜体,您还需要注册相同 Cardo 家族的那些字体.(如果是 arial,则需要注册 arial.ttf、arialbd.ttf、ariali.ttf 和 arialbi.ttf).

If you need bold, italic and bold-italic, you also need to register those fonts of the same Cardo family. (In case of arial, you'd register arial.ttf, arialbd.ttf, ariali.ttf and arialbi.ttf).

现在我们可以使用 parseToElementList() 方法将此 HTML 和 CSS 解析为一个 Element 对象列表.我们可以在单元格内使用这些对象:

Now we can parse this HTML and CSS into a list of Element objects with the parseToElementList() method. We can use these objects inside a cell:

PdfPTable table = new PdfPTable(2);
table.addCell("Some rich text:");
PdfPCell cell = new PdfPCell();
for (Element e : XMLWorkerHelper.parseToElementList(HTML, CSS)) {
    cell.addElement(e);
}
table.addCell(cell);
document.add(table);

有关生成的 PDF,请参阅 html_in_cell.pdf.

See html_in_cell.pdf for the resulting PDF.

我没有时间/技能在 iTextSharp 中提供这个示例,但是将它移植到 C# 应该很容易.

I do not have the time/skills to provide this example in iTextSharp, but it should be very easy to port this to C#.

这篇关于如何将富文本框 (HTML) 添加到表格单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆