-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathParagraphTextWithStyle.java
More file actions
56 lines (46 loc) · 1.88 KB
/
ParagraphTextWithStyle.java
File metadata and controls
56 lines (46 loc) · 1.88 KB
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
package com.itextpdf.samples.sandbox.layout;
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.FileInputStream;
import java.io.IOException;
/*
* ParagraphTextWithStyle.java
*
* Example showing how to apply reusable styles to text segments.
* Demonstrates creating and applying Style objects for consistent formatting.
*/
public class ParagraphTextWithStyle {
public static final String DEST = "./target/sandbox/layout/paragraphTextWithStyle.pdf";
public static void main(String[] args) throws IOException {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ParagraphTextWithStyle().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws IOException {
PdfDocument pdf = new PdfDocument(new PdfWriter(dest));
PdfFont code = PdfFontFactory.createFont(StandardFonts.COURIER);
Style style = new Style()
.setFont(code)
.setFontSize(14)
.setFontColor(ColorConstants.RED)
.setBackgroundColor(ColorConstants.LIGHT_GRAY);
Paragraph paragraph = 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(".");
try (Document document = new Document(pdf)) {
document.add(paragraph);
}
}
}