compare two pdf files (approach) using java [close

2019-05-30 14:28发布

i need to write a java class that compares two pdf files and points out the differences(differences in text/position/font) using some sort of highlighting. my initial approach was use pdfbox to parse the file using pdfbox and store the extracted text using in some data structure that would help me with comparing. Is there any java library that can extract the text,preserve the formatting,help me with indexing and comparing.Can i use tika/ google's diff-match for this. tika extracts text in the form of xhtml but how can i compare two xhtml files?

5条回答
2楼-- · 2019-05-30 14:31

Refer the below given sample code for pdf comparison.

ZPDFCompare obj = new ZPDFCompare();
obj.pdfcompare("C:\\Users\\Desktop\\expectedFile.pdf", "C:\\Users\\Desktop\\actualFile.pdf", "C:\\Users\\Desktop\\expectedFile_Diff.pdf","C:\\Users\\tarun.kumar\\Desktop\\actualFile_Diff.pdf");

zeonpad provided the free java api for pdf comparison.

查看更多
你好瞎i
3楼-- · 2019-05-30 14:38

I had to compare tons of pdf files in my project. my requirement was to compare the pdf files by pixel by pixel. After a lot of googling and as i could not find anything good, I ended up creating my own pdf utility for this purpose.

Please check this blog for more details & jar download.

http://www.testautomationguru.com/introducing-pdfutil-to-compare-pdf-files-extract-resources/

查看更多
Rolldiameter
4楼-- · 2019-05-30 14:39

Check this post on comparing PDF documents. Take note of the line;

PDF is a flexible file format in which you can do things in many different ways. So you could create 2 different PDF versions of a file using Acrobat and Ghostscript (as an example). The files would (hopefully) be identical. But the files would be different sizes and the internal structure of each would be very different

查看更多
5楼-- · 2019-05-30 14:43

As you mentioned, pdfbox to extract its contents and then use google's diff to compare.

查看更多
女痞
6楼-- · 2019-05-30 14:49

I don't know if you were able to solve your problem. Here is my approach to solve this.

First let's convert PDFs to HTMLs using Pdf2dom and then use daisydiff to generate comparison report in HTML. If you want a PDF then convert that HTML report to PDF. But keep in mind that PDF to HTML conversion is not 100% accurate due to complexities in PDF. You can also try another approach of converting PDFs to images and compare pixel to pixel and generate a PDF report. You can try PDFcompare library. It looks promising to me. Let me know if anyone already tried this.

查看更多
登录 后发表回答