HelloWorld.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorld {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
new HelloWorld().createPdf(RESULT);
}
/**
* Creates a PDF document.
* @param filename the path to the new PDF document
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
} |
HelloWorldNarrow.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Hello World: document constructor.
*/
public class HelloWorldNarrow {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_narrow.pdf";
/**
* Creates a PDF file: hello_narrow.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
// Using a custom page size
Rectangle pagesize = new Rectangle(216f, 720f);
Document document = new Document(pagesize, 36f, 72f, 108f, 180f);
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
document.add(new Paragraph(
"Hello World! Hello People! " +
"Hello Sky! Hello Sun! Hello Moon! Hello Stars!"));
// step 5
document.close();
}
} |
HelloWorldMaximum.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Creates a PDF with the biggest possible page size.
*/
public class HelloWorldMaximum {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_maximum.pdf";
/**
* Creates a PDF file: hello_maximum.pdf
* Important notice: the PDF is valid (in conformance with
* ISO-32000), but some PDF viewers won't be able to render
* the PDF correctly due to their own limitations.
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
// maximum page size
Document document = new Document(new Rectangle(14400, 14400));
// step 2
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// changes the user unit
writer.setUserunit(75000f);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();
}
} |
HelloWorldLetter.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Hello World example using the paper size Letter.
*/
public class HelloWorldLetter {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_letter.pdf";
/**
* Creates a PDF file: hello_letter.pdf.
* @param args no arguments needed
*/
public static void main(String[] args) throws DocumentException, IOException {
// step 1
// Specifying the page size
Document document = new Document(PageSize.LETTER);
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();
}
} |
HelloWorldLandscape1.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* A possible way to create a document in the landscape format.
*/
public class HelloWorldLandscape1 {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_landscape1.pdf";
/**
* Creates a PDF file: hello_landscape1.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
// landscape format using the rotate() method
Document document = new Document(PageSize.LETTER.rotate());
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();
}
} |
HelloWorldLandscape2.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfWriter;
/**
* A possible way to create a document in the landscape format.
*/
public class HelloWorldLandscape2 {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_landscape2.pdf";
/**
* Creates a PDF file: hello_landscape2.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
// landscape format because width > height
Document document = new Document(new Rectangle(792, 612));
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World"));
// step 5
document.close();
}
} |
HelloWorldMirroredMargins.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Examples with mirrored margins.
*/
public class HelloWorldMirroredMargins {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_mirrored_margins.pdf";
/**
* Creates a PDF file: hello_mirrored_margins.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
document.setPageSize(PageSize.A5);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroring(true);
// step 3
document.open();
// step 4
document.add(new Paragraph(
"The left margin of this odd page is 36pt (0.5 inch); " +
"the right margin 72pt (1 inch); " +
"the top margin 108pt (1.5 inch); " +
"the bottom margin 180pt (2.5 inch)."));
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph.add("Hello World! Hello People! " +
"Hello Sky! Hello Sun! Hello Moon! Hello Stars!");
}
document.add(paragraph);
document.add(new Paragraph(
"The right margin of this even page is 36pt (0.5 inch); " +
"the left margin 72pt (1 inch)."));
// step 5
document.close();
}
} |
HelloWorldMirroredMarginsTop.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Examples with mirrored margins.
*/
public class HelloWorldMirroredMarginsTop {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_mirrored_top.pdf";
/**
* Creates a PDF file: hello_mirrored_margins.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// setting page size, margins and mirrered margins
document.setPageSize(PageSize.A5);
document.setMargins(36, 72, 108, 180);
document.setMarginMirroringTopBottom(true);
// step 3
document.open();
// step 4
document.add(new Paragraph(
"The left margin of this odd page is 36pt (0.5 inch); " +
"the right margin 72pt (1 inch); " +
"the top margin 108pt (1.5 inch); " +
"the bottom margin 180pt (2.5 inch)."));
Paragraph paragraph = new Paragraph();
paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
for (int i = 0; i < 20; i++) {
paragraph.add("Hello World! Hello People! " +
"Hello Sky! Hello Sun! Hello Moon! Hello Stars!");
}
document.add(paragraph);
document.add(new Paragraph("The top margin 180pt (2.5 inch)."));
// step 5
document.close();
}
} |
HelloWorldMemory.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Creates a PDF file in memory.
*/
public class HelloWorldMemory {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_memory.pdf";
/**
* Creates a PDF file: hello_memory.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
// we'll create the file in memory
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, baos);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
// let's write the file in memory to a file anyway
FileOutputStream fos = new FileOutputStream(RESULT);
fos.write(baos.toByteArray());
fos.close();
}
} |
HelloWorldVersion_1_7.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Creates a Hello World in PDF version 1.7
*/
public class HelloWorldVersion_1_7 {
/** Path to the resulting PDF file. */
public static final String RESULT =
"results/part1/chapter01/hello_1_7.pdf";
/**
* Creates a PDF file: hello.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
// Creating a PDF 1.7 document
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(RESULT));
writer.setPdfVersion(PdfWriter.VERSION_1_7);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello World!"));
// step 5
document.close();
}
} |
HelloWorldDirect.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorldDirect {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_direct.pdf";
/**
* Creates a PDF file: hello_direct.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer =
PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
PdfContentByte canvas = writer.getDirectContentUnder();
writer.setCompressionLevel(0);
canvas.saveState(); // q
canvas.beginText(); // BT
canvas.moveText(36, 788); // 36 788 Td
canvas.setFontAndSize(BaseFont.createFont(), 12); // /F1 12 Tf
canvas.showText("Hello World"); // (Hello World)Tj
canvas.endText(); // ET
canvas.restoreState(); // Q
// step 5
document.close();
}
} |
HelloWorldColumn.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;
/**
* First iText example: Hello World.
*/
public class HelloWorldColumn {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello_column.pdf";
/**
* Creates a PDF file: hello_column.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// step 1
Document document = new Document();
// step 2
PdfWriter writer
= PdfWriter.getInstance(document, new FileOutputStream(RESULT));
// step 3
document.open();
// step 4
// we set the compression to 0 so that we can read the PDF syntax
writer.setCompressionLevel(0);
// writes something to the direct content using a convenience method
Phrase hello = new Phrase("Hello World");
PdfContentByte canvas = writer.getDirectContentUnder();
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
hello, 36, 788, 0);
// step 5
document.close();
}
} |
HelloZip.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
| /*
* This class is part of the book "iText in Action - 2nd Edition"
* written by Bruno Lowagie (ISBN: 9781935182610)
* For more info, go to: http://itextpdf.com/examples/
* This example only works with the AGPL version of iText.
*/
package part1.chapter01;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
* Writing one PDF file to a ZipOutputStream.
*/
public class HelloZip {
/** Path to the resulting PDF file. */
public static final String RESULT
= "results/part1/chapter01/hello.zip";
/**
* Creates a zip file with five PDF documents:
* hello1.pdf to hello5.pdf
* @param args no arguments needed
*/
public static void main(String[] args)
throws DocumentException, IOException {
// creating a zip file with different PDF documents
ZipOutputStream zip =
new ZipOutputStream(new FileOutputStream(RESULT));
for (int i = 1; i <= 3; i++) {
ZipEntry entry = new ZipEntry("hello_" + i + ".pdf");
zip.putNextEntry(entry);
// step 1
Document document = new Document();
// step 2
PdfWriter writer = PdfWriter.getInstance(document, zip);
writer.setCloseStream(false);
// step 3
document.open();
// step 4
document.add(new Paragraph("Hello " + i));
// step 5
document.close();
zip.closeEntry();
}
zip.close();
}
} |