How to write content in pdf using iText?

Right now I am using iText to automatically create a PDF file. And my problem is that when the content is really very large, I need to calculate the height and width of the content and then add a new page ... this is really very inconvenient.

so I wonder if there is such a way: Document.add ("a very large article"); and after that it will automatically generate a pdf file.

Thanks in advance!

+4
source share
2 answers

Next, a 9-page PDF file is created without calculating the 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(); } } 
+5
source

a new page will be generated automatically when the contents of the current page is full.

0
source

All Articles