-
Notifications
You must be signed in to change notification settings - Fork 18
/
ShowTextAtPosition.java
175 lines (161 loc) · 7.76 KB
/
ShowTextAtPosition.java
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package mkl.testarea.itext7.content;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.junit.BeforeClass;
import org.junit.Test;
import com.itextpdf.io.image.ImageData;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.io.util.StreamUtil;
import com.itextpdf.kernel.colors.DeviceRgb;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.StampingProperties;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.borders.SolidBorder;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.properties.VerticalAlignment;
/**
* @author mkl
*/
public class ShowTextAtPosition {
final static File RESULT_FOLDER = new File("target/test-outputs", "content");
@BeforeClass
public static void setUpBeforeClass() throws Exception {
RESULT_FOLDER.mkdirs();
}
/**
* <a href="https://stackoverflow.com/questions/57602156/how-to-add-paragraph-and-border-using-openpdf">
* How to add paragraph and border using OpenPDF
* </a>
* <p>
* This test shows how to create a vertically and horizontally
* centered paragraph with a dark blue border.
* </p>
*/
@Test
public void testShowCenteredBorderedParagraph() throws IOException {
String firstName = "Mister";
String lastName = "Nine";
try ( PdfWriter pdfWriter = new PdfWriter(new File(RESULT_FOLDER, "CenterParagraph.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument) ) {
Paragraph paragraph = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName);
paragraph.setWidth(100).setBorder(new SolidBorder(new DeviceRgb(0f, 0f, 0.6f), 3));
PageSize box = pdfDocument.getDefaultPageSize();
document.showTextAligned(paragraph, (box.getLeft() + box.getRight()) / 2, (box.getTop() + box.getBottom()) / 2,
TextAlignment.CENTER, VerticalAlignment.MIDDLE);
}
}
/**
* <a href="https://stackoverflow.com/questions/57602156/how-to-add-paragraph-and-border-using-openpdf">
* How to add paragraph and border using OpenPDF
* </a>
* <p>
* This test shows how to create a vertically and horizontally
* centered paragraph, decorated with header and footer images
* and surrounded by a dark blue border.
* </p>
*/
@Test
public void testShowCenteredParagraphWithExtras() throws IOException {
String firstName = "Mister";
String lastName = "Nine";
Image img = null;
try ( InputStream imageResource = getClass().getResourceAsStream("/mkl/testarea/itext7/annotate/Willi-1.jpg") ) {
ImageData data = ImageDataFactory.create(StreamUtil.inputStreamToArray(imageResource));
img = new Image(data);
img.scaleToFit(100f, 100f);
}
try ( PdfWriter pdfWriter = new PdfWriter(new File(RESULT_FOLDER, "CenterParagraphWithExtras.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfWriter);
Document document = new Document(pdfDocument) ) {
PageSize box = pdfDocument.getDefaultPageSize();
Paragraph paragraph = new Paragraph("Hello! This PDF is created for "+firstName+" "+lastName);
paragraph.setWidth(100);
document.showTextAligned(paragraph, (box.getLeft() + box.getRight()) / 2, (box.getTop() + box.getBottom()) / 2,
TextAlignment.CENTER, VerticalAlignment.MIDDLE);
PdfCanvas pdfCanvas = new PdfCanvas(pdfDocument.getLastPage());
Rectangle borderRectangle = new Rectangle(box.getLeft() + 5, box.getBottom() + 5, box.getWidth() - 10, box.getHeight() - 10);
pdfCanvas.setColor(new DeviceRgb(0f, 0f, 0.6f), false);
pdfCanvas.setLineWidth(3);
pdfCanvas.rectangle(borderRectangle);
pdfCanvas.stroke();
img.setFixedPosition(box.getLeft() + 40, box.getTop() - 150);
document.add(img);
img.setFixedPosition(box.getLeft() + 40, box.getBottom() + 50);
document.add(img);
}
}
/**
* <a href="https://stackoverflow.com/questions/65339560/add-a-text-on-an-existing-pdf-document-by-appending-something-after-the-pdf-cont">
* Add a text on an existing PDF document by appending something after the PDF content
* </a>
* <p>
* This test adds content to a PDF in append mode for Fratt (the OP
* of the SO question) to analyze and reverse-engineer.
* </p>
*/
@Test
public void testAddCenteredBorderedParagraph() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("test.pdf");
PdfReader pdfReader = new PdfReader(resource);
PdfWriter pdfWriter = new PdfWriter(new File(RESULT_FOLDER, "test-CenterParagraph.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter, new StampingProperties().useAppendMode());
Document document = new Document(pdfDocument) ) {
pdfWriter.setCompressionLevel(0);
Paragraph paragraph = new Paragraph("Hello! This text is added for Fratt");
paragraph
.setWidth(100)
.setBorder(new SolidBorder(new DeviceRgb(0f, 0f, 0.6f), 3))
.setRotationAngle(Math.PI / 4);
Rectangle box = pdfDocument.getFirstPage().getCropBox();
document.showTextAligned(paragraph,
(box.getLeft() + box.getRight()) / 2,
(box.getTop() + box.getBottom()) / 2,
TextAlignment.CENTER,
VerticalAlignment.MIDDLE);
}
}
/**
* <a href="https://stackoverflow.com/questions/67601323/add-paragraph-via-itext-layout-document-showtextaligned-on-first-page-only">
* Add Paragraph via itext.Layout.Document.ShowTextAligned on first Page only
* </a>
* <p>
* In the course of this question it became clear that the code in
* {@link #testAddCenteredBorderedParagraph()} does not add to the
* first page but instead to the large. Using a different overload
* of <code>Document.showTextAligned</code> allows adding to the
* first page, though.
* </p>
*/
@Test
public void testAddCenteredBorderedParagraphOriginalFile() throws IOException {
try ( InputStream resource = getClass().getResourceAsStream("originalFile.pdf");
PdfReader pdfReader = new PdfReader(resource);
PdfWriter pdfWriter = new PdfWriter(new File(RESULT_FOLDER, "originalFile-CenterParagraph.pdf"));
PdfDocument pdfDocument = new PdfDocument(pdfReader, pdfWriter, new StampingProperties().useAppendMode());
Document document = new Document(pdfDocument) ) {
pdfWriter.setCompressionLevel(0);
Paragraph paragraph = new Paragraph("Hello! This text is added for Fratt");
paragraph
.setWidth(100)
.setBorder(new SolidBorder(new DeviceRgb(0f, 0f, 0.6f), 3))
.setRotationAngle(Math.PI / 4);
Rectangle box = pdfDocument.getFirstPage().getCropBox();
document.showTextAligned(paragraph,
(box.getLeft() + box.getRight()) / 2,
(box.getTop() + box.getBottom()) / 2,
1,
TextAlignment.CENTER,
VerticalAlignment.MIDDLE,
0);
}
}
}