Thursday, July 29, 2010

How To Compare Two PDF Files

Comparing two PDF files using Quick Test Professional is bit tricky. Lets do it. As we know to compare two files first thing one would require is to get the contents of file thus require file handling and then the logic to compare content. It can be done in two steps:

(1) Opening the pdf files and getting their text content from clipboard

(2) Comparing the two strings containing text contents.

Here is an example:

 


Dim varPath1, varPath2, str1, str2
varPath1 = "C:\first.pdf"
varPath2 = "C:\second.pdf"

str1= getPDFText (varPath1)
str2= getPDFText (varPath2)

if StrComp(str1,str2,0) <> 0 Then
 msgbox "PDF files do not have same text content"
else
 msgbox "PDF files have same text content"
end If

Public Function getPDFText (varPath)

 Dim MyClipboard

 SystemUtil.Run varPath
 
 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micAltDwn + "t" + micAltUp

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type "s"

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micDwn

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type "Enter"

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micCtrlDwn + "a" + micCtrlUp

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Type micCtrlDwn + "c" + micCtrlUp

 Set MyClipboard = CreateObject("Mercury.Clipboard")

 getPDFText = MyClipboard.GetText

 MyClipboard.Clear

 Set MyClipboard=Nothing

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Activate

 Window("regexpwndtitle:=Adobe Reader","regexpwndclass:=AdobeAcrobat").Close

End Function


2 comments:

  1. Great topic, rarer to be found, clearly explained.


    Thanks, NP

    ReplyDelete
    Replies
    1. Thank you NP. Yes I have also done a lot of R&D to get this solution.

      ~Vijayendra

      Delete