4

现在我使用 iText 自动生成 pdf。而我的问题是,当内容真的很大时,我需要计算内容的高度和宽度,然后添加新页面......这真的很不方便。

所以我想知道是否有这样的方法: Document.add("a very very large article"); 之后,它会自动生成一个pdf文件????

提前致谢 !

4

2 回答 2

5

以下创建一个 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();
        }
}
于 2009-11-09T15:05:43.100 回答
0

当当前页面的内容已满时,将自动生成一个新页面。

于 2009-10-23T14:06:24.543 回答