Friday, March 14, 2014

QTP : Read PDF file even without buying PDF Writer or converting into text file

Often while automating manual test cases, we come across the scenarios of validating PDF data.
But we don't have any direct way of doing so.
The two  most common ways are :
1. Install PDF writer and use its API's to read data from PDF file.
    This would cost us the license for PDF writer

2. Convert the PDF file into text file using some tool and then do the validation .
    This would add a new layer of file conversion and also create dependency.

Considering the above shortcomings we were able to have a workaround for that.
This involves moving the PDF data into system clipboard and then extracting the value from clipboard..
''This Function takes file path as input and return the PDF data 
Function ReadPDF(Filename)
Set oShell = CreateObject("wscript.shell")
        'Open the PDF file
oShell.run Filename
wait(4)
        
        '' Select all data from PDF file using 'Control + a ' keys
oShell.Sendkeys "^a"
wait(2)
        '' Move data from PDF file to clipboard using 'Control + c ' keys
oShell.Sendkeys "^c"
wait(2)
       'Fetching data from clipboard
Set oClipboard = CreateObject("htmlfile")
strClipboard = objClipboard.ParentWindow.ClipboardData.GetData("Text")
       'Close the PDF File
Call terminateProcess("AcroRd32.exe")
       'Return the value of clipboard data
ReadPDF= strClipboard
End Function
'This function takes the name of the process to be terminated
Function terminateProcess(name)
   Dim objWMIService, objProcess, colProcess
Dim strComputer, strProcessKill 
strComputer = "."
Set objWMIService = GetObject("winMgmts:\\localhost") 
Set oNetwork =Createobject("Wscript.Network")
usrname = oNetwork.UserName
Set colProcess = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & name &"'")
For Each objProcess in colProcess
objProcess.GetOwner strNameOfUser,strUserDomain
If  strNameOfUser=usrname Then
objProcess.Terminate()
End If 
Next 
End Function

No comments:

Post a Comment