diff --git a/pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java b/pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java new file mode 100644 index 0000000000..195f49e962 --- /dev/null +++ b/pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java @@ -0,0 +1,77 @@ +package com.baeldung.pdf; + +import java.io.FileOutputStream; +import java.io.IOException; +import java.net.URISyntaxException; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.stream.Stream; + +import com.itextpdf.text.BadElementException; +import com.itextpdf.text.BaseColor; +import com.itextpdf.text.Document; +import com.itextpdf.text.Element; +import com.itextpdf.text.Image; +import com.itextpdf.text.Phrase; +import com.itextpdf.text.pdf.PdfPCell; +import com.itextpdf.text.pdf.PdfPTable; +import com.itextpdf.text.pdf.PdfWriter; + +public class PDFSampleMain { + + public static void main(String[] args) { + + try { + + Document document = new Document(); + PdfWriter.getInstance(document, new FileOutputStream("iTextTable.pdf")); + + document.open(); + + PdfPTable table = new PdfPTable(3); + addTableHeader(table); + addRows(table); + addCustomRows(table); + + document.add(table); + document.close(); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + private static void addTableHeader(PdfPTable table) { + Stream.of("column header 1", "column header 2", "column header 3") + .forEach(columnTitle -> { + PdfPCell header = new PdfPCell(); + header.setBackgroundColor(BaseColor.LIGHT_GRAY); + header.setBorderWidth(2); + header.setPhrase(new Phrase(columnTitle)); + table.addCell(header); + }); + } + + private static void addRows(PdfPTable table) { + table.addCell("row 1, col 1"); + table.addCell("row 1, col 2"); + table.addCell("row 1, col 3"); + } + + private static void addCustomRows(PdfPTable table) throws URISyntaxException, BadElementException, IOException { + Path path = Paths.get(ClassLoader.getSystemResource("Java_logo.png").toURI()); + Image img = Image.getInstance(path.toAbsolutePath().toString()); + img.scalePercent(10); + + PdfPCell imageCell = new PdfPCell(img); + table.addCell(imageCell); + + PdfPCell horizontalAlignCell = new PdfPCell(new Phrase("row 2, col 2")); + horizontalAlignCell.setHorizontalAlignment(Element.ALIGN_CENTER); + table.addCell(horizontalAlignCell); + + PdfPCell verticalAlignCell = new PdfPCell(new Phrase("row 2, col 3")); + verticalAlignCell.setVerticalAlignment(Element.ALIGN_BOTTOM); + table.addCell(verticalAlignCell); + } +} diff --git a/pdf/src/main/resources/Java_logo.png b/pdf/src/main/resources/Java_logo.png new file mode 100644 index 0000000000..383327b203 Binary files /dev/null and b/pdf/src/main/resources/Java_logo.png differ