How to write content to pdf using iText?


Memory leak:

Now, I use iText to automatically generate pdf. My problem is that when the content is very large, I need to calculate the height and width of the content and then add a new page... which is really inconvenient.

So I wonder if there is such a method: Document.add("A very large article"); Then, it will automatically generate a pdf file?

Thanks in advance!

mark:

The following creates a 9 page pdf without calculating 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();
        }
}

Related


How to write content to pdf using iText?

Memory leak: Now, I use iText to automatically generate pdf. My problem is that when the content is very large, I need to calculate the height and width of the content and then add a new page... which is really inconvenient. So I wonder if there is such a meth

How to write content to pdf using iText?

Memory leak: Now, I use iText to automatically generate pdf. My problem is that when the content is very large, I need to calculate the height and width of the content and then add a new page... which is really inconvenient. So I wonder if there is such a meth

How to write PDF to HttpResponseMessage using iText 7

joy 8 I am trying to use iText 7 and iText7.pdfHtml library to generate PDF and write it to HTTP response. The HTML content of the PDF is stored in an StringBuilderobject. Not sure what the correct procedure is to do this, because once used HtmlConverter.Conve

How to write PDF to HttpResponseMessage using iText 7

joy 8 I am trying to use iText 7 and iText7.pdfHtml library to generate PDF and write it to HTTP response. The HTML content of the PDF is stored in an StringBuilderobject. Not sure what the correct procedure is to do this, because once used HtmlConverter.Conve

How to write PDF to HttpResponseMessage using iText 7

joy 8 I am trying to use iText 7 and iText7.pdfHtml library to generate PDF and write it to HTTP response. The HTML content of the PDF is stored in an StringBuilderobject. Not sure what the correct procedure is to do this, because once used HtmlConverter.Conve

How to write PDF to HttpResponseMessage using iText 7

joy 8 I am trying to use iText 7 and iText7.pdfHtml library to generate PDF and write it to HTTP response. The HTML content of the PDF is stored in an StringBuilderobject. Not sure what the correct procedure is to do this, because once used HtmlConverter.Conve

How to put ByteArrayInputStream content into PDF using iText?

Andre Nobili I have the following situation and I have a method: ByteArrayInputStream fis = new ByteArrayInputStream(Bean.getValoreString("PDFmulti", "PDF").getBytes()); As you can see FIS varialbe is ByteArrayInputStream and Bean.getValoreString("PDFmulti",

How to put ByteArrayInputStream content into PDF using iText?

Andre Nobili I have the following situation and I have a method: ByteArrayInputStream fis = new ByteArrayInputStream(Bean.getValoreString("PDFmulti", "PDF").getBytes()); As you can see FIS varialbe is ByteArrayInputStream and Bean.getValoreString("PDFmulti",

Write Arabic in pdf using itext

Mohamed Awad I am using itext lib to generate PDF files. I want to write Arabic words. When I run the following code, the word characters are displayed in reverse. Code used: PdfContentByte cb = docWriter.getDirectContent(); BaseFont bfBold = BaseFont.createFo

Write Arabic in pdf using itext

Mohamed Awad I am using itext lib to generate PDF files. I want to write Arabic words. When I run the following code, the word characters are displayed in reverse. Code used: PdfContentByte cb = docWriter.getDirectContent(); BaseFont bfBold = BaseFont.createFo

Write Arabic in pdf using itext

Mohamed Awad I am using itext lib to generate PDF files. I want to write Arabic words. When I run the following code, the word characters are displayed in reverse. Code used: PdfContentByte cb = docWriter.getDirectContent(); BaseFont bfBold = BaseFont.createFo

Write Arabic in pdf using itext

Mohamed Awad I am using itext lib to generate PDF files. I want to write Arabic words. When I run the following code, the word characters are displayed in reverse. Code used: PdfContentByte cb = docWriter.getDirectContent(); BaseFont bfBold = BaseFont.createFo

HTML to PDF using iText: how to generate a checkbox

RealHowTo: I have a simple HTML page from which iText can generate a PDF. Great, but the checkbox is ignored. what should I do? import java.io.FileOutputStream; import java.io.StringReader; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize;

How to know if a document claims to be PDF/A using itext

alexyz78: I would at least check if the documentation claims it is PDF/A compliant. How to do this with iText? Tag memory: what. The PDF/A spec contains the answer (which won't do well unless someone pays to get it). You can dig the same information from iText

How to rotate watermark (text) in PDF using iText?

Allegiance: I'm using iText to watermark a PDF file (text: "SuperEasy You Done") as described in How to watermark a PDF with text or image ? ( TransparentWatermark2.java ). Check out the project source code on GitHub . Right now, an example of the PDF I get is

How to draw graphics to PDF using iText?

jimdrang : I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code: D

How to know if a document claims to be PDF/A using itext

alexyz78: I would at least check if the documentation claims it is PDF/A compliant. How to do this with iText? Tag memory: what. The PDF/A spec contains the answer (which won't do well unless someone pays to get it). You can dig the same information from iText

How to draw graphics to PDF using iText?

jimdrang : I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code: D

HTML to PDF using iText: how to generate a checkbox

RealHowTo: I have a simple HTML page from which iText can generate a PDF. Great, but the checkbox is ignored. what should I do? import java.io.FileOutputStream; import java.io.StringReader; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize;

How to rotate watermark (text) in PDF using iText?

Allegiance: I'm using iText to watermark a PDF file (text: "SuperEasy You Done") as described in How to watermark a PDF with text or image ? ( TransparentWatermark2.java ). Check out the project source code on GitHub . Right now, an example of the PDF I get is

How to export Vietnamese text to PDF using iText

Chi Nguyen I'm having trouble trying to export a Vietnamese document to PDF using iText. I put Vietnamese words in .xml file like this <td fontfamily="Helvetica" fontstyle="0" fontsize="9" align="0" colspan="48" lineoccupied="1">T\u1ED5 ch\u1EE9c tham gia</td>

HTML to PDF using iText: how to generate a checkbox

RealHowTo: I have a simple HTML page from which iText can generate a PDF. Great, but the checkbox is ignored. what do I do? import java.io.FileOutputStream; import java.io.StringReader; import com.itextpdf.text.Document; import com.itextpdf.text.PageSize; imp

How to know if a document claims to be PDF/A using itext

alexyz78: I would at least check if the documentation claims it is PDF/A compliant. How to do this with iText? Tag memory: what. The PDF/A spec contains the answer (which won't do well unless someone pays to get it). You can dig the same information from iText

How to draw graphics to PDF using iText?

jimdrang : I'm trying to complete an example that draws a graph and writes it to a PDF, but I keep getting the error that the PDF has no pages. If I add something simple with document.add() after opening it works fine, I never see the graph. Here is my code: D

How to rotate watermark (text) in PDF using iText?

Allegiance: I'm using iText to watermark a PDF file (text: "SuperEasy You Done") as described in How to watermark a PDF with text or image ? ( TransparentWatermark2.java ). Check out the project source code on GitHub . Right now, an example of the PDF I get is

How to rotate watermark (text) in PDF using iText?

Allegiance: I'm using iText to watermark a PDF file (text: "SuperEasy You Done") as described in How to watermark a PDF with text or image ? ( TransparentWatermark2.java ). Check out the project source code on GitHub . Right now, an example of the PDF I get is