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
| import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.pdmodel.PDPage; import org.apache.pdfbox.pdmodel.PDPageContentStream; import org.apache.pdfbox.pdmodel.common.PDRectangle; import org.apache.pdfbox.pdmodel.font.PDType1Font; import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import java.io.IOException;
public class PDFGenerator {
public static void main(String[] args) { String pdfFilePath = "output.pdf"; String imgPath = "<image_path>"; try { PDDocument document = new PDDocument(); PDPage page1 = new PDPage(PDRectangle.A4); document.addPage(page1);
PDPageContentStream contentStream = new PDPageContentStream(document, page1);
float x = 100; float y = 500; float width = 300; float height = 200;
contentStream.drawImage(PDImageXObject.createFromFile(imgPath, document), x, y, width, height);
contentStream.close();
PDPage page2 = new PDPage(PDRectangle.A4); document.addPage(page2); PDPageContentStream contentStream2 = new PDPageContentStream(document, page2);
drawTableHeaders(contentStream2); drawTableRow(contentStream2, 1, "Data 1", "Value 1"); drawTableRow(contentStream2, 2, "Data 2", "Value 2"); contentStream2.close();
document.save(pdfFilePath); document.close(); } catch (IOException e) { e.printStackTrace(); } }
private static void drawTableHeaders(PDPageContentStream contentStream) throws IOException { float margin = 50; float yStart = 700; float tableWidth = 500; float yPosition = yStart; float rowHeight = 20; float cellMargin = 5f;
float[] columnWidths = {200, 300};
contentStream.setFont(PDType1Font.HELVETICA_BOLD, 12); contentStream.setLineWidth(1f); contentStream.moveTo(margin, yPosition); contentStream.lineTo(margin + tableWidth, yPosition); contentStream.stroke();
yPosition -= rowHeight; contentStream.beginText(); contentStream.newLineAtOffset(margin + cellMargin, yPosition - 15); contentStream.showText("Column 1"); contentStream.newLineAtOffset(columnWidths[0], 0); contentStream.showText("Column 2"); contentStream.endText(); }
private static void drawTableRow(PDPageContentStream contentStream, int rowNum, String data1, String data2) throws IOException { float margin = 50; float yStart = 700; float tableWidth = 500; float yPosition = yStart - rowNum * 20; float rowHeight = 20; float cellMargin = 5f;
float[] columnWidths = {200, 300};
contentStream.setLineWidth(1f); contentStream.moveTo(margin, yPosition); contentStream.lineTo(margin + tableWidth, yPosition); contentStream.stroke();
yPosition -= rowHeight; contentStream.beginText(); contentStream.setFont(PDType1Font.HELVETICA, 12); contentStream.newLineAtOffset(margin + cellMargin, yPosition - 15); contentStream.showText(data1); contentStream.newLineAtOffset(columnWidths[0], 0); contentStream.showText(data2); contentStream.endText(); } }
|