Tutorial: iText by Example

Frequently Asked Questions

UNDER CONSTRUCTION
Measurements:
If you want to create a Rectangle (PageSize), define a margin, choose a font size or add something at an absolute place, you might wonder what measurement unit is used: centimeters, inches, points or pixels. In fact, the default measurement system roughly corresponds to the various definitions of the typographic unit of measurement known as the point. There are 72 points in 1 inch.
If you want to create a rectangle in PDF that has the size of an A4-page, you have to calculate the number of points:
  • 21 cm / 2.54 = 8.2677 inch
  • 8.2677 * 72 = 595 points
  • 29.7 cm / 2.54 = 11.6929 inch
  • 11.6929 * 72 = 842 points
Example: java com.lowagie.examples.general.faq.Measurements
Demonstrates the measurement system (points, inches, centimeters): see Measurements.pdf
Go to top of the page
How to create a new page in iText:
It strikes me how many times this question is asked on the mailing-list. The method you need to create a new page isn't that cryptic: document.newPage(). Of course, there are some little caveats:
  • If you are construction an Object such as Section, you can't just invoke newPage() on the document; you should tell the object, that you want to go to the next page. In this case, you should add a Chunk.NEXPAGE to the object.
  • If you invoke newPage and the current page is blank, no new page will be added. You can insert blank pages if you add something 'invisible' to the blank page. In the example below, a Chunk.NEWLINE is added.
    document.newPage();
    document.add(Chunk.NEWLINE);
    document.newPage();
    Or you can tell the writer the page isn't empty (allthough it IS). This is done with the method setPageEmpty
    document.newPage();
    writer.setPageEmpty(false);
    document.newPage();
Example: java com.lowagie.examples.general.faq.NewPage
Shows how to go to a new page or insert a blank page: see NewPage.pdf
Go to top of the page
iText Version:
If you file a bugreport or post a question to the mailing-list, one of the first questions you will be asked is:
What version of iText are you using?
There are two ways you can find out the answer. You can open the PDF file in Acrobat and go to the window File -> Document Properties... Under 'Description' you'll see what library/product was used as 'PDF Producer'. In the screenshot below, it was iText1.1.2 by lowagie.com (based on itext-paulo-144). You can always check the most recent releasenumber on the iText download page.
Document Properties Window
Of course, if a post-processing tool was used, another product will be listed as PDF Producer. In that case, you can ask the library programmatically for its version with Document.getVersion() and send the output to the System.out or System.err.
Example: java com.lowagie.examples.general.faq.iTextVersion
Shows the iText version that was used to generate this example: see version.pdf
Go to top of the page
PDF Version:
Read the Portable Document Format Reference Manual Version 1.6 (section 3.4.1 'File Header' page 68):
The first line of a PDF file is a header identifying the version of the PDF specification to which the file conforms. For a file conforming to PDF version 1.5, the header should be
%PDF−1.5
iText generated documents have version PDF-1.4 by default, but you can change the version with the method setPdfVersion. The parameter can be PdfWriter.VERSION_1_2, PdfWriter.VERSION_1_3, PdfWriter.VERSION_1_4, PdfWriter.VERSION_1_5 or PdfWriter.VERSION_1_6.
writer.setPdfVersion(PdfWriter.VERSION_1_2);
Remark: this only changes the header (iText doesn't keep the version in the catalog yet). If you change the version number to a lower version, you are responsible for making sure that you don't use newer features.
Example: java com.lowagie.examples.general.faq.PdfVersion
Change the PDF version of a document: see pdfversion.pdf
Document Properties Window
Go to top of the page
Why are there some strange characters in the PDF Header?:
Read the Portable Document Format Reference Manual Version 1.6 (section 3.4.1 'File Header' page 68):
If a PDF file contains binary data, as most do, it is recommended that the header line be immediately followed by a comment line containing at least four binary characters—that is, characters whose codes are 128 or greater. This ensures proper behavior of file transfer applications that inspect data near the beginning of a file to determine whether to treat the file's contents as text or as binary.
iText adds these 4 binary characters automatically. The second line of an iText generated PDF file always looks like this when opened in a text editor:
%âãÏÓ
Go to top of the page



Amazon books:
amazon.co.uk-link