Kat*_*ate 4 pdf itextsharp richtext
我有一个名为"DocumentContent"的富文本框,我将使用以下代码将其内容添加到pdf:
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);
Run Code Online (Sandbox Code Playgroud)
问题是当我打开PDF文件时,内容显示为HTML而不是文本,如下所示:
<p>Overview  line1 </p><p>Overview  line2
</p><p>Overview  line3 </p><p>Overview 
line4</p><p>Overview  line4</p><p>Overview 
line5 </p>
Run Code Online (Sandbox Code Playgroud)
但它应该如下所示
Overview line1
Overview line2
Overview line3
Overview line4
Overview line4
Overview line5
Run Code Online (Sandbox Code Playgroud)
我要做的是保留用户应用于富文本的所有样式,只需将字体系列更改为Arial.
我可以更改字体系列,但我需要将此内容从HTML解码为文本.
您能否提一些建议?谢谢
请看一下HtmlContentForCell示例.
在这个例子中,我们有你提到的HTML:
public static final String HTML = "<p>Overview line1</p>"
+ "<p>Overview line2</p><p>Overview line3</p>"
+ "<p>Overview line4</p><p>Overview line4</p>"
+ "<p>Overview line5 </p>";
Run Code Online (Sandbox Code Playgroud)
我们还为<p>
标签创建了一种字体:
public static final String CSS = "p { font-family: Cardo; }";
Run Code Online (Sandbox Code Playgroud)
在您的情况下,您可能想要替换Cardo
为Arial
.
请注意,我们注册了该Cardo
字体的常规版本:
FontFactory.register("resources/fonts/Cardo-Regular.ttf");
Run Code Online (Sandbox Code Playgroud)
如果您需要粗体,斜体和粗体,则还需要注册同一Cardo
系列的字体.(如果是arial,你可以注册arial.ttf,arialbd.ttf,ariali.ttf和arialbi.ttf).
现在我们可以Element
使用该parseToElementList()
方法将此HTML和CSS解析为对象列表.我们可以在单元格中使用这些对象:
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);
Run Code Online (Sandbox Code Playgroud)
有关生成的PDF,请参阅html_in_cell.pdf.
我没有时间/技能在iTextSharp中提供这个例子,但将它移植到C#应该很容易.
归档时间: |
|
查看次数: |
3515 次 |
最近记录: |