Skip to content

Commit 5a588a4

Browse files
authored
BAEL-9388 Align Paragraph at The Center of The PDF Page in Java (#18718)
* Update PDFSampleMain.java * Update PDFSampleMain.java * Update PDFSampleMain.java * Update PDFSampleMain.java
1 parent afbd3c4 commit 5a588a4

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

text-processing-libraries-modules/pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import com.itextpdf.text.BaseColor;
1313
import com.itextpdf.text.Document;
1414
import com.itextpdf.text.Element;
15+
import com.itextpdf.text.Paragraph;
1516
import com.itextpdf.text.Image;
1617
import com.itextpdf.text.Phrase;
1718
import com.itextpdf.text.pdf.PdfPCell;
@@ -29,6 +30,13 @@ public static void main(String[] args) {
2930

3031
document.open();
3132

33+
Document documentParagraph = new Document();
34+
PdfWriter.getInstance(documentParagraph, new FileOutputStream("iTextParagraph.pdf"));
35+
36+
documentParagraph.open();
37+
38+
addParagraphInCenter(documentParagraph);
39+
3240
PdfPTable table = new PdfPTable(3);
3341
addTableHeader(table);
3442
setAbsoluteColumnWidths(table);
@@ -45,6 +53,12 @@ public static void main(String[] args) {
4553
}
4654
}
4755

56+
private static void addParagraphInCenter(Document document) throws IOException, DocumentException {
57+
Paragraph paragraph = new Paragraph("This paragraph will be horizontally centered.");
58+
paragraph.setAlignment(Element.ALIGN_CENTER);
59+
document.add(paragraph);
60+
}
61+
4862
private static void addTableHeader(PdfPTable table) {
4963
Stream.of("column header 1", "column header 2", "column header 3")
5064
.forEach(columnTitle -> {

0 commit comments

Comments
 (0)