|
| 1 | +package com.applitools.imagetester.demo; |
| 2 | + |
| 3 | +import org.apache.pdfbox.pdmodel.PDDocument; |
| 4 | +import org.apache.pdfbox.pdmodel.PDPage; |
| 5 | +import org.apache.pdfbox.pdmodel.PDPageContentStream; |
| 6 | +import org.apache.pdfbox.pdmodel.common.PDRectangle; |
| 7 | +import org.apache.pdfbox.pdmodel.font.PDType1Font; |
| 8 | + |
| 9 | +import java.io.File; |
| 10 | +import java.io.IOException; |
| 11 | + |
| 12 | +/** |
| 13 | + * Generates sample PDFs for the BatchMapper demo. |
| 14 | + * |
| 15 | + * Run from the project root with: |
| 16 | + * mvn exec:java -Dexec.mainClass="com.applitools.imagetester.demo.GenerateDemoPDFs" |
| 17 | + * |
| 18 | + * Output files: |
| 19 | + * demo/batchmapper/before/contract.pdf — original 4-page contract |
| 20 | + * demo/batchmapper/after/contract.pdf — updated 5-page contract (page inserted after page 2) |
| 21 | + */ |
| 22 | +public class GenerateDemoPDFs { |
| 23 | + |
| 24 | + public static void main(String[] args) throws IOException { |
| 25 | + createBeforePdf("demo/batchmapper/before/contract.pdf"); |
| 26 | + createAfterPdf("demo/batchmapper/after/contract.pdf"); |
| 27 | + System.out.println("Done. PDFs written to demo/batchmapper/before/ and demo/batchmapper/after/"); |
| 28 | + } |
| 29 | + |
| 30 | + // ----------------------------------------------------------------------- |
| 31 | + // Before: 4 pages |
| 32 | + // Page 1 — Cover |
| 33 | + // Page 2 — Introduction |
| 34 | + // Page 3 — Details |
| 35 | + // Page 4 — Conclusion |
| 36 | + // ----------------------------------------------------------------------- |
| 37 | + private static void createBeforePdf(String outputPath) throws IOException { |
| 38 | + new File(outputPath).getParentFile().mkdirs(); |
| 39 | + try (PDDocument doc = new PDDocument()) { |
| 40 | + addPage(doc, "CONTRACT", "Page 1 of 4", |
| 41 | + "Service Agreement", |
| 42 | + "This document outlines the terms and conditions between", |
| 43 | + "Acme Corp and its client for professional services rendered."); |
| 44 | + |
| 45 | + addPage(doc, "INTRODUCTION", "Page 2 of 4", |
| 46 | + "1. Background", |
| 47 | + "Acme Corp has been providing enterprise solutions since 2010.", |
| 48 | + "This agreement governs the scope of work described herein."); |
| 49 | + |
| 50 | + addPage(doc, "DETAILS", "Page 3 of 4", |
| 51 | + "2. Scope of Work", |
| 52 | + "The vendor shall deliver software integration services", |
| 53 | + "as outlined in Exhibit A, within the agreed timeline."); |
| 54 | + |
| 55 | + addPage(doc, "CONCLUSION", "Page 4 of 4", |
| 56 | + "3. Signatures", |
| 57 | + "By signing below, both parties agree to the terms set forth", |
| 58 | + "in this contract. Effective date: January 1, 2025."); |
| 59 | + |
| 60 | + doc.save(outputPath); |
| 61 | + } |
| 62 | + System.out.println("Created: " + outputPath); |
| 63 | + } |
| 64 | + |
| 65 | + // ----------------------------------------------------------------------- |
| 66 | + // After: 5 pages — new page inserted between Introduction and Details |
| 67 | + // Page 1 — Cover (unchanged) |
| 68 | + // Page 2 — Introduction (unchanged) |
| 69 | + // Page 3 — NEW: Compliance Notice <-- inserted here |
| 70 | + // Page 4 — Details (was page 3) |
| 71 | + // Page 5 — Conclusion (was page 4) |
| 72 | + // ----------------------------------------------------------------------- |
| 73 | + private static void createAfterPdf(String outputPath) throws IOException { |
| 74 | + new File(outputPath).getParentFile().mkdirs(); |
| 75 | + try (PDDocument doc = new PDDocument()) { |
| 76 | + addPage(doc, "CONTRACT", "Page 1 of 5", |
| 77 | + "Service Agreement", |
| 78 | + "This document outlines the terms and conditions between", |
| 79 | + "Acme Corp and its client for professional services rendered."); |
| 80 | + |
| 81 | + addPage(doc, "INTRODUCTION", "Page 2 of 5", |
| 82 | + "1. Background", |
| 83 | + "Acme Corp has been providing enterprise solutions since 2010.", |
| 84 | + "This agreement governs the scope of work described herein."); |
| 85 | + |
| 86 | + addPage(doc, "COMPLIANCE NOTICE", "Page 3 of 5 *** NEW PAGE ***", |
| 87 | + "1a. Regulatory Compliance", |
| 88 | + "All services are subject to compliance with applicable data", |
| 89 | + "protection regulations including GDPR and CCPA."); |
| 90 | + |
| 91 | + addPage(doc, "DETAILS", "Page 4 of 5", |
| 92 | + "2. Scope of Work", |
| 93 | + "The vendor shall deliver software integration services", |
| 94 | + "as outlined in Exhibit A, within the agreed timeline."); |
| 95 | + |
| 96 | + addPage(doc, "CONCLUSION", "Page 5 of 5", |
| 97 | + "3. Signatures", |
| 98 | + "By signing below, both parties agree to the terms set forth", |
| 99 | + "in this contract. Effective date: January 1, 2025."); |
| 100 | + |
| 101 | + doc.save(outputPath); |
| 102 | + } |
| 103 | + System.out.println("Created: " + outputPath); |
| 104 | + } |
| 105 | + |
| 106 | + private static void addPage(PDDocument doc, String heading, String subheading, |
| 107 | + String section, String line1, String line2) throws IOException { |
| 108 | + PDPage page = new PDPage(PDRectangle.LETTER); |
| 109 | + doc.addPage(page); |
| 110 | + |
| 111 | + try (PDPageContentStream cs = new PDPageContentStream(doc, page)) { |
| 112 | + float margin = 72; |
| 113 | + float width = page.getMediaBox().getWidth() - 2 * margin; |
| 114 | + float y = page.getMediaBox().getHeight() - margin; |
| 115 | + |
| 116 | + // Heading |
| 117 | + cs.beginText(); |
| 118 | + cs.setFont(PDType1Font.HELVETICA_BOLD, 28); |
| 119 | + cs.newLineAtOffset(margin, y - 40); |
| 120 | + cs.showText(heading); |
| 121 | + cs.endText(); |
| 122 | + |
| 123 | + // Subheading |
| 124 | + cs.beginText(); |
| 125 | + cs.setFont(PDType1Font.HELVETICA, 12); |
| 126 | + cs.newLineAtOffset(margin, y - 70); |
| 127 | + cs.showText(subheading); |
| 128 | + cs.endText(); |
| 129 | + |
| 130 | + // Divider line (drawn as a thin rectangle) |
| 131 | + cs.setLineWidth(0.5f); |
| 132 | + cs.moveTo(margin, y - 85); |
| 133 | + cs.lineTo(margin + width, y - 85); |
| 134 | + cs.stroke(); |
| 135 | + |
| 136 | + // Body text |
| 137 | + cs.beginText(); |
| 138 | + cs.setFont(PDType1Font.HELVETICA_BOLD, 14); |
| 139 | + cs.newLineAtOffset(margin, y - 120); |
| 140 | + cs.showText(section); |
| 141 | + cs.endText(); |
| 142 | + |
| 143 | + cs.beginText(); |
| 144 | + cs.setFont(PDType1Font.HELVETICA, 12); |
| 145 | + cs.setLeading(20f); |
| 146 | + cs.newLineAtOffset(margin, y - 150); |
| 147 | + cs.showText(line1); |
| 148 | + cs.newLine(); |
| 149 | + cs.showText(line2); |
| 150 | + cs.endText(); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments