Merge pull request #7506 from amit2103/BAEL-16045-24
[BAEL-16045] - Check Article Code Matches GitHub for https://www.bael…
This commit is contained in:
77
pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java
Normal file
77
pdf/src/main/java/com/baeldung/pdf/PDFSampleMain.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
BIN
pdf/src/main/resources/Java_logo.png
Normal file
BIN
pdf/src/main/resources/Java_logo.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 14 KiB |
Reference in New Issue
Block a user