使用 iText java 将字体设置为 pdf 中的段落
我试图在java中使用iText创建pdf。当我尝试将字体设置为段落时失败。确切的问题只是字体大小没有得到应用。我使用了以下代码。
StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);
Font font = new Font(BaseFont.createFont("c:\\ARIALUN0.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED), 6, Font.BOLD, new Color(0, 0, 0));
Paragraph para = new Paragraph();
para.setFont(font);
for (int k = 0; k < arrList.size(); ++k) {
para.add((com.lowagie.text.Element)arrList.get(k));
}
谁能帮我找到解决方案?
I was trying to create pdf using iText in java. And am failed when I tried to set font to paragraph. The exact problem is only the font size is not getting applied. I used the following code.
StringReader strReader = new StringReader(content);
arrList = HTMLWorker.parseToList(strReader, null);
Font font = new Font(BaseFont.createFont("c:\\ARIALUN0.ttf", BaseFont.IDENTITY_H,
BaseFont.EMBEDDED), 6, Font.BOLD, new Color(0, 0, 0));
Paragraph para = new Paragraph();
para.setFont(font);
for (int k = 0; k < arrList.size(); ++k) {
para.add((com.lowagie.text.Element)arrList.get(k));
}
Can anyone help me to find a solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
//使用此代码。有时 setfont() 不适用于 Paragraph
//use this code.Sometimes setfont() willnot work with Paragraph
我很困惑,几乎发布了错误的答案。
您的段落的字体设置正确。只需尝试插入一个字符串即可查看。
你的问题出在你的for循环上。您将向该段落添加 Element 对象。 Element 由 Chunk 对象组成,每个 Chunk 对象都有自己的 Font 数据。
尝试在实例化元素时设置元素中块的字体。那应该可以解决你的问题。
I was pretty confused and almost posted the wrong answer to this.
Your paragraph is having its font set correctly. Just try inserting a String to see.
Your problem lies in your for loop. To the paragraph, you're adding a Element objects. An Element is composed of Chunk objects, which each have their own Font data.
Try setting the Font of the Chunks in your Elements when they are instantiated. That should solve your problem.
试试这个,它将保存文本的样式:
Try this ,It will save the style of the text:
要将
Font
添加到itextpdfParagraph
,您只需使用Chunk
,然后您就可以设置Font 到
Chunk
然后将该Chunk
添加到Paragraph
中。示例:
For adding
Font
to itextpdfParagraph
you can simply use aChunk
then you can setFont
toChunk
add thatChunk
toParagraph
afterwards.Example: