Dyna Dorin
Dyna Dorin

Reputation: 33

How to compare PDF files using Java pdfutil

I am trying to compare two PDF files using pdfutil in Java.

I was able to compare the document using boolean approach and get the value but when using visual approach I am not able to get the result file which is set in the setImageDestinationPath.

There are no errors just the file is not there.

public static void PDF_COMPARISON(String object, String data) throws Exception{
  // boolean isEquals = false;
  try {
    String result = Constants.Path_Result+DriverScript.s;

    File[] file10 = new File(object).listFiles(File::isFile);

    for (File file1: file10) {
      String fileExtension = file1.getName().split("\\.")[file1.getName().split("\\.").length - 1];
      if (fileExtension.toLowerCase().equals("pdf")) {
        String file12 =file1.getAbsolutePath();
        System.out.println(file12);

        File[] file121 = new File(data).listFiles(File::isFile);

        for (File file2: file121) {
          String fileExtension1 = file2.getName().split("\\.")[file2.getName().split("\\.").length - 1];

          if (fileExtension1.toLowerCase().equals("pdf")) {
            String file22 = file2.getAbsolutePath();
            System.out.println(file22);
            PDFUtil pdfUtil = new PDFUtil();

            //boolean compare=pdfUtil.compare(file12, file22);
            // System.out.println(compare);

            pdfUtil.setCompareMode(CompareMode.VISUAL_MODE);
            pdfUtil.compare(file12, file22);

            pdfUtil.highlightPdfDifference(true);
            pdfUtil.setImageDestinationPath(result);

            //System.out.println(Constants.Path_Result);
            //pdfUtil.compare(file12, file22);
          }
        }
      }
    }
  } catch(Exception e) {
    Log.error("Not able to Close the Browser --- " + e.getMessage());
    DriverScript.bResult = false;
  }
}

Simplifyed code

String file1="J:/files/doc1.pdf";
String file2="J:/files/doc2.pdf";
PDFUtil pdfUtil = new PDFUtil();

pdfUtil.setCompareMode(CompareMode.VISUAL_MODE);
pdfUtil.compare(file1, file2);
pdfUtil.highlightPdfDifference(true);
pdfUtil.setImageDestinationPath("J:/imgpath");
pdfUtil.compare(file1, file2);```

Upvotes: 1

Views: 2861

Answers (1)

Dyna Dorin
Dyna Dorin

Reputation: 33

It seems like it was a limitation of the pdfutil the file 1and file2 should have the same number of pages in the PDF for pdfutil to work

Upvotes: 1

Related Questions