Today Deals

Monday, November 5, 2012

PDF Automation in QTP

                                                                            The most challenging issue with PDFs is that it could be of any kind, not just a tabular data; it could have plain text, images or even forms to fill up. So this makes a tester’s life a bit difficult, never mind, we will definitely find an easy of do it…

Although there are already some better approaches we have to deal with PDF documents but I found many of us are facing so many difficulties using this.

There are lots of queries coming at QTP forums asking for an easy way of doing it with PDFs. keeping those in my mind I started creating this API mainly for comparing two PDF documents, and added few more features in it. We will see all of them later in this article.

LearnQuickTestPDF API works with iTextSharp. Sometime back when I was involved in a PDF project I found this really useful library which does a great deal to ease the burden of manipulating PDF documents.It provides all of the primitive functions necessary to create a PDF document. However,since all of the methods are based on primitive operations, it is easy to confuse the look and feel of a document without enforcing certain standards. Visit iText Home to learn more about iTextSharp.

Let us now see how we can use LearnQuickTestPDF

Download and run exe to extract file to hard drive, extract to “C:\LearnQTP”.
Open directory LearnQuickTestPDF and find the Install.vbs, this will make the API ready to use.
But before this, read the terms and conditions first and then execute the install.vbs file by double clicking it. Accept terms and condition by clicking on ‘Yes’ button and proceed.
That’s it you are now ready to use this into QTP.

Use this in the same way you do with other COM APIs.
Set oPDF=createobject("LearnQuickTest.ManipulatePDF")
Once we get the object, we can now proceed with using different methods to manipulate the documents.

Let’s see one by one,
1. Comparing two pdf documents: Use ‘ComparePdfFiles’ method to compare two pdf documents. It returns true if your there is no difference in the two documents otherwise false.
PDFFile1 – Pdf file path to compare with
PDFFile2 – Pdf file path to compare to
ResultFile – text output file to store the log/difference if any
FromPageNum – [Optional Parameter] Page number to start from
ToPageNum -[Optional Parameter] Page number to end to.

Example –
If oPDF.ComparePdfFiles ("C:\Actual.pdf","C:\Expected.pdf","C:\ResultCompare.txt") =True then
reporter.ReportEvent micPass ,"PDF Compare", "No Difference found"
reporter.ReportEvent micFail ,"PDF Compare", "Files are different"
End IF

2. Retrieve text from pdf document: Use ‘GetPDFText’ to retrieve the content from the pdf file.
Parameters –
PDFFile – PDF file path
FromPageNum – [Optional Parameter] Page number to start from
ToPageNum – [Optional Parameter] Page number to end to.

print oPDF.GetPdfText ("C:\fw4.pdf")

3. Find number of pages in the pdf document: Use GetNumberOfPages(“<>”) to find out the number of pages in the document.

msgbox oPDF.GetNumberOfPages("C:\ Expected.pdf")

4.Retrieve field names from the PDF form: Method – GetFieldNames
Use Parameters
PDFFile – document path which contains form
resultFile – text output file to store the Field names

example –
oPDF.GetFieldNames "C:\fw4.pdf","C:\ fw4Fields.txt"

5.Fill the PDF form: Use method – FillPDFForm
Parameters –
sourcePDF – Form PDF path to fill
outputPDF – output pdf path for completed document
FiledNames – Name of the fields to fill in each separated with ‘,’
FieldValues – Values for the corresponding fields separated with ‘,’
LogFile – Path to store log for the action

Fields = "f1_01(0),f1_02(0),f1_03(0),f1_04(0),f1_05(0)
Values = "1,1,1,8,0,1,16,28,Saket K, Test"
oPDF.FillPDFForm "C:\fw4.pdf","C:\fw4Filled.pdf",Fields, Values,"C:\\fillFormLog.txt"

Apart from this we have some more methods to work with
6. GetFieldValue – to retrieve the value of a particular field in the acro form
7. Set SingleValue – to set the a single field value if required.
8. DownloadPDF – to get the document downloaded to your hard drive from a web location.

[This utility has been successfully tested to work with QTP 10.0, Adobe 9.0 and IE 8.0. Please report bugs and issues here.]

Post a Comment

Don't Miss It!!! Let's Get Updated!!! Follow Us by Email for FREE!!!

Most Popular Posts