我必须比较两个PDF。我不能使用任何比较实用程序,因为我们想要自动化测试,文档存储在不同的服务器和位置。
我使用PDFbox来比较PDF。一个文档包含QR码,一个包含条形码。另一方面,两个文档都包含一个PDF。
我使用下面给出的代码:
public List<Object> compareDoc(Message<PDFStore> message) throws Exception{
List<String> insertedRows = compareResult.getInsertedRows();
List<String> deletedRows = compareResult.getDeletedRows();
List<String> oldValueOfChangedRows = compareResult.getOldValueOfChangedRows();
List<String> newValueOfChangedRows = compareResult.getNewValueOfChangedRows();
try{
compareResult.init();
PDFStore store = message.getPayload();
byte[] scritturaPDF = store.getScritturaPDF();
FileUtils.writeByteArrayToFile(new File("C:\\Old Sys BackUp\\Data\\text1.pdf"), scritturaPDF);
byte[] reconfPDF = store.getReconfPDF();
FileUtils.writeByteArrayToFile(new File("C:\\Old Sys BackUp\\Data\\text2.pdf"), reconfPDF);
strip("C:\\Old Sys BackUp\\Data\\text1.pdf", "C:\\Old Sys BackUp\\Data\\text3.pdf");
strip("C:\\Old Sys BackUp\\Data\\text2.pdf", "C:\\Old Sys BackUp\\Data\\text4.pdf");
String[] fileArray = {"C:\\Old Sys BackUp\\Data\\text3.pdf" ,"C:\\Old Sys BackUp\\Data\\text3.txt"};
String[] fileArray1 = {"C:\\Old Sys BackUp\\Data\\text4.pdf","C:\\Old Sys BackUp\\Data\\text4.txt"};
ExtractText.main(fileArray);
ExtractText.main(fileArray1);
List<String> scritturaPDFlist = fileToLines("C:\\Old Sys BackUp\\Data\\text3.txt");
List<String> reconfPDFList = fileToLines("C:\\Old Sys BackUp\\Data\\text4.txt");
Patch patch = DiffUtils.diff(scritturaPDFlist, reconfPDFList);
DiffRowGenerator.Builder builder = new DiffRowGenerator.Builder();
builder.showInlineDiffs(true);
DiffRowGenerator dfg = builder.build();
List<DiffRow> diffList = dfg.generateDiffRows(scritturaPDFlist, reconfPDFList);
if(patch.getDeltas().size() > 0){
List<Delta> deltas = patch.getDeltas();
for (Delta delta: deltas) {
logger.trace("difference is " + delta);
}
logger.trace("difference count is " + patch.getDeltas().size());
}
if(diffList !=null){
for(DiffRow diffRow : diffList){
if (diffRow.getTag().equals(DiffRow.Tag.INSERT)){
insertedRows.add(changeString( diffRow.getNewLine()));
}
else if (diffRow.getTag().equals(DiffRow.Tag.DELETE)){
deletedRows.add(changeString( diffRow.getOldLine()));
}
else if (diffRow.getTag().equals(DiffRow.Tag.CHANGE)){
oldValueOfChangedRows.add(changeString( diffRow.getNewLine()));
newValueOfChangedRows.add(changeString( diffRow.getOldLine()));
}
}
}
}catch(Exception e){
e.printStackTrace();
}
List<Object> o = new ArrayList<Object> ();
o.add(insertedRows);
o.add(deletedRows);
o.add(oldValueOfChangedRows);
o.add(newValueOfChangedRows);
return o;
}
public static void strip(String pdfFile, String pdfFileOut) throws Exception {
PDDocument document = PDDocument.load(pdfFile);
System.out.println("page coutn -> " + document.getNumberOfPages());
PDDocumentCatalog catalog = document.getDocumentCatalog();
for (Object pageObj : catalog.getAllPages()) {
PDPage page = (PDPage) pageObj;
PDResources resources = page.findResources();
resources.getImages().clear();
}
document.save(pdfFileOut);
}
private static String changeString(String s){
if (s != null && s.contains("<")){
int i = s.indexOf("<");
int j = s.indexOf(">");
String s1 = s.substring(0, i);
String s2 = s.substring(j+1,s.length());
s = s1 + s2;
s = changeString(s);
}
return s;
}
private static List<String> fileToLines(String filename) {
List<String> lines = new LinkedList<String>();
String line = "";
try {
BufferedReader in = new BufferedReader(new FileReader(filename));
while ((line = in.readLine()) != null) {
if (!(line.contains("Name:")) && !(line.contains("Title:")))
lines.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return lines;
}
我无法获得100%正确的错配。有人可以帮忙吗?