// Method 2: Page-by-page comparison public static ComparisonResult comparePageByPage(String pdfPath1, String pdfPath2) throws IOException try (PDDocument doc1 = PDDocument.load(new File(pdfPath1)); PDDocument doc2 = PDDocument.load(new File(pdfPath2))) ComparisonResult result = new ComparisonResult(); int pageCount1 = doc1.getNumberOfPages(); int pageCount2 = doc2.getNumberOfPages(); result.setPageCountsEqual(pageCount1 == pageCount2); result.setPageDifferences(new ArrayList<>()); int minPages = Math.min(pageCount1, pageCount2); PDFTextStripper stripper = new PDFTextStripper(); for (int i = 1; i <= minPages; i++) stripper.setStartPage(i); stripper.setEndPage(i); String text1 = stripper.getText(doc1); String text2 = stripper.getText(doc2); if (!text1.equals(text2)) result.getPageDifferences().add(new PageDifference(i, text1, text2)); return result;
<!-- Optional: For advanced diff visualization --> <dependency> <groupId>com.github.difflib</groupId> <artifactId>difflib</artifactId> <version>1.3.0</version> </dependency> </dependencies> name: PDF Comparison on: pull_request: paths: - '**/*.pdf' workflow_dispatch: inputs: pdf1: description: 'First PDF file path' required: true pdf2: description: 'Second PDF file path' required: true java by comparison pdf github
private static String extractTextFromPDF(String pdfPath) throws IOException try (PDDocument document = PDDocument.load(new File(pdfPath))) PDFTextStripper stripper = new PDFTextStripper(); return stripper.getText(document); int pageCount1 = doc1.getNumberOfPages()
- name: Build and run PDF comparison run: | mvn compile mvn exec:java -Dexec.mainClass="PDFComparisonApp" \ -Dexec.args="$ github.event.inputs.pdf1 $ github.event.inputs.pdf2 \ --github-token $ secrets.GITHUB_TOKEN \ --repo $ github.repository " int pageCount2 = doc2.getNumberOfPages()