// 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();
<!-- 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
public static class PageDifference private int pageNumber; private String text1; private String text2; public PageDifference(int pageNumber, String text1, String text2) this.pageNumber = pageNumber; this.text1 = text1; this.text2 = text2; // Getters public int getPageNumber() return pageNumber; public String getText1() return text1; public String getText2() return text2; private boolean pageCountsEqual
private static boolean compareImages(List<BufferedImage> images1, List<BufferedImage> images2) if (images1.size() != images2.size()) return false; for (int i = 0; i < images1.size(); i++) BufferedImage img1 = images1.get(i); BufferedImage img2 = images2.get(i); if (img1.getWidth() != img2.getWidth() return true; private boolean imagesIdentical
private static void saveReport(String report, String filename) throws IOException Files.write(Paths.get(filename), report.getBytes()); System.out.println("Report saved to: " + filename);
<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>
// 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;