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?
问题:
回答1:
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/
回答2:
As you mentioned, pdfbox to extract its contents and then use google's diff to compare.
回答3:
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
回答4:
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.
回答5:
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.