How to write content into pdf use iText?

与世无争的帅哥 提交于 2019-12-22 08:24:24

问题


Right now i use iText to generate a pdf automatically. And my problem is that when the content is really very large, i need to calculate the content's height and width, and then add new page... this is really very inconvinent.

so I wonder whether or not there is a method like: Document.add("a very very large article"); and after this , it will auto generate a pdf file ????

Thanks in advance !


回答1:


The following creates a 9 page pdf without having to calculate height and width.

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();
        }
}



回答2:


a new page will be generated automaticly, when the content of the current page is full.



来源:https://stackoverflow.com/questions/1231133/how-to-write-content-into-pdf-use-itext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!