1
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
/**
* Example written for JavaOne 2016.
* Differences between iText 5 and iText 7 are discussed in the JavaOne talk
* "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
* This is the iText 5 version of one of the examples.
*/
package javaone.edition16;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import sandbox.WrapToTest;
@WrapToTest
public class HelloWorldStyles {
public static final String DEST
= "results/javaone/edition16/hello_styles.pdf";
public static void main(String[] args)
throws DocumentException, IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new HelloWorldStyles().createPdf(DEST);
}
public void createPdf(String dest)
throws DocumentException, IOException {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(dest));
document.open();
Font code = new Font(FontFamily.COURIER, 12, Font.NORMAL, BaseColor.RED);
Paragraph p = new Paragraph("In this example, named ");
p.add(createBgChunk("HelloWorldStyles", code));
p.add(", we experiment with some text in ");
p.add(createBgChunk("code style", code));
p.add(".");
document.add(p);
document.close();
}
public Chunk createBgChunk(String s, Font font) {
Chunk chunk = new Chunk(s, font);
chunk.setBackground(BaseColor.LIGHT_GRAY);
return chunk;
}
}
HelloWorldStyles.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
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2019 iText Group NV
Authors: iText Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
/**
* Example written for JavaOne 2016.
* Differences between iText 5 and iText 7 are discussed in the JavaOne talk
* "Oops, I broke my API" by Raf Hens and Bruno Lowagie.
* This is the iText 7 version of one of the examples.
*/
package com.itextpdf.sandbox.javaone16;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.Style;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import java.io.File;
import java.io.IOException;
public class HelloWorldStyles {
public static final String DEST
= "results/javaone16/hello_styles.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new HelloWorldStyles().createPdf(DEST);
}
public void createPdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFont code = PdfFontFactory.createFont(StandardFonts.COURIER);
Style style = new Style()
.setFont(code)
.setFontSize(12)
.setFontColor(ColorConstants.RED)
.setBackgroundColor(ColorConstants.LIGHT_GRAY);
try (Document document = new Document(pdf)) {
document.add(
new Paragraph()
.add("In this example, named ")
.add(new Text("HelloWorldStyles").addStyle(style))
.add(", we experiment with some text in ")
.add(new Text("code style").addStyle(style))
.add("."));
}
}
}