1.
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
int marginRight,
int marginTop,
int marginBottom);
2.
Normally, you don't have to worry about creating this rectangle, since you can use one of the statics in class
PageSize.java
. These are the pagesizes that are provided: A0-A10, LEGAL, LETTER, HALFLETTER, _11x17, LEDGER, NOTE, B0-B5, ARCH_A-ARCH_E, FLSA and FLSE.
Most these pageSizes are in PORTRAIT-format. If you want them to be in
LANDSCAPE
, all you have to do is rotate() the Rectangle
:
Document document = new Document(PageSize.A4.rotate());
Check out example
Chap0103.java
and its
result
.
3.
When creating a document, you can also define left, right, upper and lower margins:
Document document = new Document(PageSize.A5, 36, 72, 108, 180);
Measurements
When creating a rectangle or choosing a margin, you might wonder what measurement unit is used: centimeters, inches 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
The default border of 36 points corresponds with half an inch.
Remark: if you change the page size, this only has effect on the next page (see page initialisations). If you change the margins, this has an immediate effect, so be careful!
4.
Once our document is created, we can create one or more instances of writers that listen to this document. All writers should be derived from the abstract class com.lowagie.text.DocWriter
.
For the moment there are two possibilities: you can use com.lowagie.text.pdf.PdfWriter to generate documents in the Portable Document Format, or you can use com.lowagie.text.html.HtmlWriter to generate documents in HTML. If for instance you want to generate TeX-documents as well, you could write a package: com.lowagie.text.TeX.TeXWriter.
5.
The constructor of these writer-classes is made private. You can only create an instance with the following method:
public static xxxWriter getInstance(Document document, OutputStream os) throws DocumentException
(xxx
being Pdf or Html)
You can create an instance this way:
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("Chap01xx.pdf"));
but you will hardly ever need the object writer (except if you want to create
Advanced PDF
or if you want to use some very specific functionalities such as
ViewerPreferences
or
Encryption
). So it's sufficient to just get the instance:
PdfWriter.getInstance(document, new FileOutputStream("Chap01xx.pdf"));
6.
itext
最新推荐文章于 2022-09-21 11:35:05 发布