Java By Comparison Pdf Github Link
<!-- 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
<dependencies> <!-- PDFBox for PDF manipulation --> <dependency> <groupId>org.apache.pdfbox</groupId> <artifactId>pdfbox</artifactId> <version>3.0.0</version> </dependency> <!-- GitHub API for integration --> <dependency> <groupId>org.kohsuke</groupId> <artifactId>github-api</artifactId> <version>1.318</version> </dependency>
// Helper classes public static class ComparisonResult private boolean textIdentical; private boolean pageCountsEqual; private boolean imagesIdentical; private List<String> textDifferences; private List<PageDifference> pageDifferences; // Getters and setters public boolean isTextIdentical() return textIdentical; public void setTextIdentical(boolean textIdentical) this.textIdentical = textIdentical; public boolean isPageCountsEqual() return pageCountsEqual; public void setPageCountsEqual(boolean pageCountsEqual) this.pageCountsEqual = pageCountsEqual; public boolean isImagesIdentical() return imagesIdentical; public void setImagesIdentical(boolean imagesIdentical) this.imagesIdentical = imagesIdentical; public List<String> getTextDifferences() return textDifferences; public void setTextDifferences(List<String> textDifferences) this.textDifferences = textDifferences; public List<PageDifference> getPageDifferences() return pageDifferences; public void setPageDifferences(List<PageDifference> pageDifferences) this.pageDifferences = pageDifferences; @Override public String toString() StringBuilder sb = new StringBuilder(); sb.append("PDF Comparison Results:\n"); sb.append("Text identical: ").append(textIdentical).append("\n"); sb.append("Page counts equal: ").append(pageCountsEqual).append("\n"); sb.append("Images identical: ").append(imagesIdentical).append("\n"); if (textDifferences != null && !textDifferences.isEmpty()) sb.append("Text differences:\n"); for (String diff : textDifferences) sb.append(" ").append(diff).append("\n"); if (pageDifferences != null && !pageDifferences.isEmpty()) sb.append("Page differences:\n"); for (PageDifference diff : pageDifferences) sb.append(" Page ").append(diff.getPageNumber()).append(" differs\n"); return sb.toString(); java by comparison pdf github
jobs: compare-pdfs: runs-on: ubuntu-latest
// 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; !-- PDFBox for PDF manipulation -->
private static String generateReport(String pdf1, String pdf2, PDFComparator.ComparisonResult textResult, PDFComparator.ComparisonResult pageResult) StringBuilder report = new StringBuilder(); report.append("PDF COMPARISON REPORT\n"); report.append("=====================\n\n"); report.append("File 1: ").append(pdf1).append("\n"); report.append("File 2: ").append(pdf2).append("\n"); report.append("Timestamp: ").append(new Date()).append("\n\n"); report.append("SUMMARY\n"); report.append("-------\n"); report.append("Text Comparison: ").append(textResult.isTextIdentical() ? "IDENTICAL" : "DIFFERENT").append("\n"); report.append("Page Count: ").append(pageResult.isPageCountsEqual() ? "EQUAL" : "DIFFERENT").append("\n\n"); if (!textResult.isTextIdentical() && textResult.getTextDifferences() != null) report.append("DETAILED DIFFERENCES\n"); report.append("--------------------\n"); for (String diff : textResult.getTextDifferences()) report.append(diff).append("\n\n"); return report.toString();
- 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 " !-- GitHub API for integration -->
// Method 3: Image-based comparison (requires PDF to image conversion) public static ComparisonResult compareByImages(String pdfPath1, String pdfPath2) throws IOException // Convert PDF pages to images first List<BufferedImage> images1 = convertPDFToImages(pdfPath1); List<BufferedImage> images2 = convertPDFToImages(pdfPath2); ComparisonResult result = new ComparisonResult(); result.setImagesIdentical(compareImages(images1, images2)); return result;