Java 在 Itextpdf 中设置字体大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21154578/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting the font size in Itextpdf
提问by Techidiot
I am working on a program where I am taking an ASCII
file as input and converting it to PDF
using Itext
library.
我正在开发一个程序,我将ASCII
文件作为输入并将其转换为PDF
使用Itext
库。
I am able to convert and print it, but the font size appears too small. Currently I have set the font size to 6
but, if I change it to 7, It doesn't work, it doesn't fit on the PDF
properly.
我能够转换和打印它,但字体大小显得太小。目前我已将字体大小设置为,6
但是,如果我将其更改为 7,则它不起作用,它无法PDF
正确显示。
Here is a part of my code snippet:
这是我的代码片段的一部分:
Document doc= new Document();
Rectangle test = new Rectangle(531,666);
doc = new Document(test);
doc.setMargins(0,0,0,0);
p = new Paragraph(new Phrase(lineSpacing,line,
FontFactory.getFont(FontFactory.COURIER, fntSize)));
doc.add(p);
I am not able to use double with this method. Is there any other way?
我无法通过这种方法使用 double 。有没有其他办法?
采纳答案by Techidiot
So, my final snippet looks like this:
所以,我的最终片段如下所示:
Document doc = new Document(PageSize.A4, 0f, 0f, 0f, 0f);
float fntSize, lineSpacing;
fntSize = 6.7f;
lineSpacing = 10f;
Paragraph p = new Paragraph(new Phrase(lineSpacing,line,
FontFactory.getFont(FontFactory.COURIER, fntSize)));
doc.add(p);
It gives a perfect format for an A4size paper with no margins and a good font size.
它为无边距和良好字体大小的A4尺寸纸张提供了完美的格式。
I hope it helps someone!
我希望它可以帮助某人!