Pdf 如何向表格单元格添加富文本框(HTML)?
我有一个名为“DocumentContent”的富文本框,我将使用以下代码将其内容添加到pdf:Pdf 如何向表格单元格添加富文本框(HTML)?,pdf,itextsharp,richtext,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.HttpU
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而不是文本,如下所示:
<p>Overview  line1 </p><p>Overview  line2
</p><p>Overview  line3 </p><p>Overview 
line4</p><p>Overview  line4</p><p>Overview 
line5 </p>
public static 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>";
我要做的是保留用户应用于富文本的所有样式,并将字体族更改为Arial
我可以更改字体系列,但我需要将此内容从HTML解码为文本
你能给我个建议吗?
谢谢请看一看这个例子
在本例中,我们有您提到的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>";
在您的情况下,您可能需要将Cardo
替换为Arial
请注意,我们注册了常规版本的Cardo
font:
FontFactory.register("resources/fonts/Cardo-Regular.ttf");
如果需要加粗、斜体和加粗斜体,还需要注册相同Cardo
系列的字体。(如果是arial,您需要注册arial.ttf、arialbd.ttf、ariali.ttf和arialbi.ttf)
现在,我们可以使用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);
有关生成的PDF,请参见
我没有时间/技能在iTextSharp中提供这个示例,但将其移植到C#应该非常容易。最后,我用C#编写了这段代码,这段代码运行得非常好,感谢Bruno帮助我理解了XMLWorker 下面是一个在C#中使用XMLWorker的示例 我使用了一个示例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>
public static 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>";
它创建Test.pdf文件,添加字体为Arial的my HTML。所以所有的威尔士字符都可以保存在PDF文件中
注意:我已将iTextSharp.dll v:5.5.3和XMLworker.dll v:5.5.3添加到我的项目中
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.css;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline;
希望这能有用
凯特谢谢布鲁诺,这很有帮助。我感谢你的帮助。现在我正在尝试将其更改为C。好的,如果您能够在移植示例后添加我的代码的C版本作为额外答案,那就太好了。
using iTextSharp.text;
using iTextSharp.text.html;
using iTextSharp.text.pdf;
using iTextSharp.tool.xml;
using iTextSharp.tool.xml.css;
using iTextSharp.tool.xml.html;
using iTextSharp.tool.xml.parser;
using iTextSharp.tool.xml.pipeline;