Java 如何在写入pdf文件时设置字体大小
我正在从Java 如何在写入pdf文件时设置字体大小,java,itext,Java,Itext,我正在从.txt文件中读取一些文本并将其写入pdf文件。但在我的pdf中,文本显示为一些较大的字体。即使我已将.txt文件的字体大小更改为较大或过小,pdf中的字体也将相同 我的代码的一部分,用于在文本文件中读写 Document document = new Document(PageSize.A4.rotate(), 36, 36, 80, 160); ........ ......... document.newPage(); //Wri
.txt
文件中读取一些文本并将其写入pdf文件。但在我的pdf中,文本显示为一些较大的字体。即使我已将.txt
文件的字体大小更改为较大或过小,pdf中的字体也将相同
我的代码的一部分,用于在文本文件中读写
Document document = new Document(PageSize.A4.rotate(), 36, 36, 80, 160);
........
.........
document.newPage();
//Writing in a default page from the txt file
document.add(new com.lowagie.text.Paragraph("\n"));
iStream = new FileInputStream(path1); //path1 is location of .txt file
in = new DataInputStream(iStream);
is = new InputStreamReader(in);
br = new BufferedReader(is);
String strLine;
while ((strLine = br.readLine()) != null)
{
Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n");
para.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(new com.lowagie.text.Paragraph(strLine + "\n"));
}
br.close();
is.close();
in.close();
iStream.close();
document.close();
writer.close();
很抱歉,我正在使用较低版本的itext,因为现在我无法将任何第三方库添加到我的windchill中。此版本的itext已包含在我的windchill中
如何在pdf文件中设置字体大小?任何帮助都会非常有用。非常简单:D
首先,我要改变一件事:
while ((strLine = br.readLine()) != null) {
Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n");
para.setAlignment(Element.ALIGN_JUSTIFIED);
document.add(para);
}
这样,该段在文件中是合理的。按照您的方式,您更改了一个Pargaph对象的文本对齐方式,但随后在PDF中添加了一个完全不同的新对象
现在谈谈你的问题:您可以在每个段落“constructor”中指定
字体,如下所示:
Paragraph para = new com.lowagie.text.Paragraph(strLine + "\n", FontFactory.getFont("Arial", 14f /*This is the size, but as a float!*/);
可以找到LowagieAPI的完整文档
致以最诚挚的问候我在哪里可以将字体大小传递给上述人员?请稍候,让我更正一下。根据lowagie实施的FontFactory文件,这应该是可行的。