如何写的内容转换成PDF使用iText的?
-
22-07-2019 - |
题
现在,我使用的iText自动生成一个pdf。 我的问题是,当内容真的是非常大的,我需要计算内容的高度和宽度,然后添加新的一页...... 这真的是很inconvinent。
所以我不知道是否存在等的方法: Document.add(“一个非常非常大的文章”); 并在此之后,它会自动产生一个pdf文件????
提前感谢!
解决方案
以下,而不必计算的高度和宽度创建9页PDF。
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class HelloWorld {
public static void main(String[] args) {
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("HelloWorld.pdf"));
document.open();
String text = "";
for (int i = 0; i < 10000; i++) {
text += "test";
}
document.add(new Paragraph(text));
} catch (DocumentException e) {
System.err.println(e.getMessage());
} catch (IOException ex) {
System.err.println(ex.getMessage());
}
document.close();
}
}
其他提示
一个新的页面将被自动产生的,当所述当前页的内容是满的。
不隶属于 StackOverflow