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

查看:2314
本文介绍了如何将富文本框(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:\Windows\Fonts\arial.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.

您能告诉我吗?
谢谢

Could you please advise? Thanks

推荐答案

请查看 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> tag:

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

在您的情况下,您可能想要替换 Cardo 使用 Arial

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

请注意,我们注册了 Cardo <的常规版本/ code> font:

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).

现在我们可以解析这个HTML和CSS使用 parseToElementList()方法进入元素对象的列表。我们可以在单元格中使用这些对象:

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);

参见 html_in_cell.pdf 获得的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天全站免登陆