LearnQuickTestPDF API: Manipulating PDFs in QTP

Through this article I would like to throw an insight on an alternate and easy way to manipulate/automate variety of operations in PDF files using QTP. Please allow me to introduce an interface which will help you to easily working with PDFs. The focus of this API is relatively small, but interesting. Hope you will find this useful. – Saket.

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.

parameters:
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"
Else
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.

Example

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

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

Example

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

Example

Fields = "f1_01(0),f1_02(0),f1_03(0),f1_04(0),f1_05(0)
,f1_06(0),f1_07(0),f1_08(0),f1_09(0),f1_10(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.

That’s all for now, I may update/modify this API if I find anything useful related and can be added into this. Please feel free to ask me at QTP Forums for any issues using this API.

[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.]

75 comments ↓

#1 Pavan Turlapati on 06.28.10 at 10:03

This is an interesting article on pdf document after reading, printing to pdf from the same website in the past.

#2 Tejpal on 06.28.10 at 10:49

Hi Saket,
Its good to see that someone has come up with the idea of working with the PDF file and its really useful.
This will make those programmers task more easy who have been stuck in manipulating with PDF file in QTP.
!!!Keep it up great going!!!

#3 Milindsd on 06.28.10 at 10:56

Congrats Saket for disclosing effective method to manipulate PDFs.

#4 Vipul Borad on 06.28.10 at 20:39

Hi Saket,

This is very userful articale for me. Very good work. In our company, we deal with PDFs all the time, this will be really make our life easier.

Thank you,
Vipul Borad

#5 RajaPatil on 06.30.10 at 13:12

Excellent work!!!

#6 Parag Ahire on 07.01.10 at 00:01

Saket,
The article is interesting and worth pursuing at our end.

Would it be possible to provide an API that would ignore certain locations of the PDF while performing the differences?

For example if we have two PDF’s that we know would be different at certain locations (a variable number) where we have a date field or a serial number field, we could provide references to these locations while performing the comparison.

It would be great if you could provide some pointers on the above requriement.

Thanks,
Parag

#7 Saket Kumar on 07.01.10 at 11:14

Thanks all, I am glad you found this useful.
Parag – I will definetely look into this for comaring that way and come up with the solution soon.

#8 Jenny on 07.01.10 at 22:50

What a great article Saket.. Hats off to you!!!!!! it is really effective method to manipulate PDFs.

Cheerz,
Jenny

#9 Parag Ahire on 07.07.10 at 00:41

Thank you Saket. We look forward to your updates.

Our development team will defintely benefit with the added API to ignore specific and variable content while performing the comparison of content within the PDF document.

#10 Reva on 07.07.10 at 18:15

Hi Sankit,

Thanks for the very usefule article. I have a doubt, is it possible to work out with annotations(like crop, highlight,zoom) in pdf? If something could be done please let me know..

Regards,
Reva……

#11 Parag Ahire on 07.08.10 at 05:50

Saket,
Our development team will benefit with the added API to ignore known differences in the PDF. We look forward to updates on this topic.

Thank you,
Parag

#12 Saket Kumar on 07.08.10 at 15:29

Reva – would you like to elaborate it please, explaining exactly what do you need to work out with annotations. you may use our forums to post this.

#13 Saket Kumar on 07.08.10 at 15:31

Thanks all :) , good to see it working for all of you. Keep suggesting any new modifications you need to have in this API.

#14 Jyotsna on 07.22.10 at 14:40

Hi Saket
It’s really useful…..
Thanks………………

#15 Raghu on 07.23.10 at 03:51

Hi Saket ,
Does this API is freeware ? In the term and condition i didn’t see any price or support price .Can you please provide some more information on it.
Thanks,Raghu

#16 Saket Kumar on 07.23.10 at 11:54

LearnQuickTestPDF API is Free for both personal/official or commercial use and may be installed and used by
on only one computer. However credit should always go to LearnQTP.com. Its component parts may not be separated for use on more than one computer.

This is provided “AS IS” without warranty of any kind, you must assume all risk of using it.

I will appreciate if you tell us the story of successfully implementing it :)

#17 Saket Kumar on 07.23.10 at 11:55

Please visit this thread for more info on this http://www.learnqtp.com/forums/Thread-LearnQuickTestPdf-API

#18 bharat on 07.28.10 at 20:18

Hi Saket,
If more than one system is used the same file is there any problem? Related to license?

#19 Saket Kumar on 08.02.10 at 18:36

Hi Bharat, I did not get your question. would you like to elaborate a bit?

#20 aashiq@hcl on 08.04.10 at 11:52

@Saket Kumar
I just want to know the PDF compare property, I am working in Dialogue(tool for generating PDF) documents testing, where we will be having an exisiting PDF(no change) and the new PDF where text changes are made and some texts are eliminated , so will this PDF compare property in QTP help us to spot out the chnages that were made in the existing PDF…pls do reply me….asik.ali@hcl.in

#21 RJ on 08.10.10 at 14:32

Hi Saket,
Here are the problem I am getting.

1.The Pdf object is not giving list of methods in QTP(9.5) after oPDF.
2.oPDF.GetFieldNames method work for sample file given but not for my Pdf file. it is generating blank output.
3.I am ultimately trying to get field value but its not coming due to pint 2 above. Also please explain syntax for same

what could be the problem
I have downloaded the version and indtalled as given in your doc.

Thanks,
RJ

#22 Rushi on 08.11.10 at 10:16

Hi Saket,

I have downloaded the PDF API and using as per instructions given.

I am facing problem as -

1.It is not showing the list of methods. (oPDF.)
2.How do I get specific value from Pdf file.

Thanks,
Rushi

#23 Virendra on 08.11.10 at 14:14

Saket,

I am stuck in first place itself.
After running the install.vbs am not getting the pdf utility work with QTP at all.
Don’t know the cause but is it not compatible with older versions of QTP? Mine is 9.2.

Please suggest.

Virendra

#24 Saket Kumar on 08.23.10 at 16:40

hi aashiq@hcl,
I think you can use this for your requirement. I beleive this should work. let me know if you find any issue.

#25 Saket Kumar on 08.23.10 at 16:54

hi RJ /Rushi – are you still facing these issues, not able to understand what is causing this.
do you see the .tlb file there in the folder. if it is there delete the file and carefully follow the same steps to install again. see if it works.

#26 Saket Kumar on 08.23.10 at 16:56

Hi Virendra,
very sorry for this, I have not tested this for 9.2. I will see, if I can figure out the issue and let you know.

#27 Rushi on 08.23.10 at 18:57

Hi Saket,
Thanks for reply.

1.I have deleted .tlb file there in the folder.
2.Re-installed, do you expect .tlb file again to appair in the folder?
3.Again I have deleted the same
4.Tried the

Set oPDF=createobject(“LearnQuickTest.ManipulatePDF”)

but
oPDF. Unable to see list of methods

just for your knowledge I am using QTP 9.2 v

-
Rushi

#28 Saket Kumar on 09.11.10 at 08:38

@Rushi – yes the tlb should be there in the folder, dont delete that file

#29 srinivasulu.t on 09.19.10 at 12:26

Hi Friends,
i am working as a software engineer, i am getting one problem that is using object spy or OR, then applicaton will close ,

After Click on the Close button I am getting below error.

In this problem I found below things
1. Some times object is not recognized
2. Objects are overloaded in the ORM and all objects are not Recognized in the ORM ( Object repository is not recognized the objects)
3. When ever open the qtp and open the application I am getting above message
4. Using object spy application getting above second screen short

Any one pls Provied for ans to me

#30 bhanu on 09.27.10 at 12:23

Plz send syntax for the below queries
1. Get Field Value – to retrieve the value of a particular field in the acro form
2. Set Single Value – to set the a single field value if required.
3. Download PDF – to get the document downloaded to your hard drive from a web location.
Thanking You
Bhanu

#31 Venkat Reddy on 10.01.10 at 12:34

Hi Saket,

It is very helpfull while working with PDF using QTP. I have downloaded and worked with all methods which u have provided. But one method is not working “GetFieldValue” , its throwing an exception like “Invalid procedure call or argument: ‘GetFieldValue’ “. Below is the LOC i have used
1)Set oPDF=createobject(“LearnQuickTest.ManipulatePDF”)

2)oPDF.GetFieldValue “C:\Documents and Settings\tshiva\Desktop\IEP_9494″.

#32 Shalini on 10.10.10 at 17:29

what is the difference b/w logical properties and physical properties of objects?

#33 ranjit on 10.11.10 at 19:12

My Questions is can you explain project architecture.

#34 venkat reddy on 10.19.10 at 11:52

HI Saket,

i have used the below method, its working fine, but the source PDF file is getting damaged . its showing message like “file is used by some other person or file damaged” PLz help me out.

Set oPDF=createobject(“LearnQuickTest.ManipulatePDF”)
strBOD=oPDF.GetFieldValue (“C:\Documents and Settings\Desktop\PDFForm_10152010_025806.pdf”,”PDFForm[1].txtGN_MeetingDate”)
msgbox strBOD

Thnx in advance

Regards
Venkat Reddy

#35 Naveena on 10.27.10 at 17:14

Hi Saket

Firstly, i wanted to Congratulate you for your work, Great job!!!! well done!!!!

I need to compare PDF files, i have followed the same procedure to install the API, but the methods are not listed for me in QTP.

im using QTP 9.2 trial version, colud u let me know what might be the issue?

Regards,
Naveena

#36 Lokanadham on 10.28.10 at 16:48

Hi Saket,
After running the install.vbs am not getting the pdf utility work with QTP at all.

Don’t know the cause I am using QTP 9.2.

Can you Please suggest me on this issue.

#37 Saket Kumar on 10.29.10 at 10:28

It seems, this utility does not work properly with QTP 9.2. I am working on to fix it to work with QTP 9.2.

#38 Saket Kumar on 10.29.10 at 10:30

Hi Venkat,
have you destroyed the object created? use
set oPDF = Nothing
It should work

#39 Venkat on 11.01.10 at 13:53

Hi Saket,

Thanks for reply…but i have tried both
set oPDF = Nothing
set oPDF = NULL

Even though its not working…..once it retrives the value from PDF successfully, second time onwards it shows blank value..when i tried to open the PDF it says “this file is in use”…after closing the QTP…then i tried to open the PDF, then it says like ” PDF is damaged or corrupted”

Regards
Venkat Reddy

#40 Lokanadham on 11.02.10 at 12:58

Hi Saket,

Do you have any updates on PDF utility for QTP 9.2

#41 MohanReddy on 11.04.10 at 12:29

Hi saket,

Really it to good presenation.which is very usefully to work on PDF files.

#42 Lokanadham on 11.15.10 at 15:09

Hi,

After running the install.vbs I am not getting the PDF utility options. I am using QTP 9.2.

If anybody have the solution please let me know.

Thanks in Advance.

#43 Saket Kumar on 11.23.10 at 15:07

I will surely update the fixes in few days, hopefully there should not be any limitations now for QTP versions.
for QTP 9.2 users – please note that Intellisense feature has been introduced in QTP 10, so you wont get the list of methods at the time of scripting same as using any other API, Please refer this post to get help on using methods.
Please let me know any issues (apart from those mentioned above) you are facing while using this utility, so that I can fix them as well.

#44 Saket Kumar on 11.23.10 at 15:12

I will surely update the fixes in few days, hopefully there should not be any limitations now for QTP versions.
for QTP 9.2 users – please note that Intellisense feature has been introduced in QTP 10, so you wont get the list of methods at the time of scripting same as using any other API, Please refer this post to get help on using methods.
Please let me know any issues (apart from those mentioned above) you are facing while using this utility, so that I can fix them as well.

#45 Amit Oswal on 12.06.10 at 21:58

Great stuff Saket…. Wondering if the comparison results could be a little better i.e. show the differences in a third file say “Result.PDF” or “Result.doc”, highlighting the differences between the PDF’s like word does using he compare and merge.

#46 Karun on 12.08.10 at 22:25

HI

Can any one tell me how can I Set Iteration of number of rows for an Action via a script ?

Thanks

#47 Jalpa on 12.16.10 at 22:51

Hi Saket! Thanks a lot for the API!!.It has helped me a lot.but the PDFs that i need to compare have images too. and the comparison that the API does is only textual. Can you help me with comparing PDF images?

#48 Manesh on 12.22.10 at 00:51

Hi Saket,
From where can I downoad LearnQuickTestPDF API?

#49 Rahull on 12.22.10 at 14:26

Hi Saket,

Will you please let me know the parameters for oPDF.GetFieldValue(sourcePdf, Formkey)
here Formkey means what?

Thanks

#50 Saket Kumar on 12.31.10 at 12:34

hi Manesh, click on the download link mentioned in the post above to download LearnQuickTestPDF api.

Rahull – the FormKey is the field/token which is in your pdf containg a value in it.

#51 Jalpa on 12.31.10 at 17:07

The API gives a comparison in a text form with page Nos.. but I have a requirement where in I want to highlight the differences in the PDF itslelf. I have explored itextsharp, I could read pdf and do comparison byte by byte but I am not able to edit the existing PDF to highlight differences. Please suggest if there is any way to it with itextsharp.

#52 Manesh on 01.05.11 at 02:13

Hi Saket,
How to close the runtime Pdf which opens when getting a field value ?

First time when I run the script to get field value it’s working fine but when I run that again it’s getting null value because it is unable to open file as the file is not
closed proerly when we run this script for first time

Let me know if you have solution for this.

#53 Saket Kumar on 01.11.11 at 19:27

@ Manesh – Try destroying the object at the end of your statements.

#54 Mike on 01.19.11 at 02:45

This is by far the EASIEST and most flexible little helper I’ve ever found for QTP.

What I needed to do was just verify that a PDF had been created. Using your API, I save the PDF as a text file, then set the text of the file to a variable, then get a substring of the text and verify its what I expect it to be.

THANK YOU! :)

#55 Saket Kumar on 01.21.11 at 10:38

I am glad this was helpful for you. Thank you Mike :)

#56 Shanmugavel on 01.31.11 at 17:26

Hi ,
I downloaded and tried to run the exe “LearnQuickTestPDF.exe” but got error below…

—————————
16 bit MS-DOS Subsystem
—————————
C:\Users\SHANMU~1.CHI\Desktop\LEARNQ~1.EXE
The NTVDM CPU has encountered an illegal instruction.
CS:0dee IP:0149 OP:63 6b 65 64 3c Choose ‘Close’ to terminate the application.
—————————
Close Ignore
—————————

Can you help me on this…

#57 Rama on 02.04.11 at 01:45

Hi Saket,

Can you please share me PDF methods or Properies related document.

Appreciate your help.

Thanks
Rama

#58 Bobby on 02.10.11 at 21:29

Hi Saket,

Great Work… I am using QTP 10x version and dowloaded API into my local system…But I am not able to get the methods from the API.Do I need to call that API or store it in the same folder where QTP is in? And Can yo please share me the list of methods this API has? It would be helpful for me if you share it and Thanks a lot for sharing such a wonderful article

#59 Bobby on 02.16.11 at 21:07

I am trying to register this com dll to use it in QTP.But,it is throwing an error that module is not compatible with the version of Windows that you are running.Check if the module is compatible with an x86 or x64 version of regsvr32.exe.I am using Windows Vista .Can you please help me out with this problem?

#60 Shilu on 02.21.11 at 19:23

Hi Saket,

First Congrats saket.

I have extracted the dowloaded files to “C:\LearnQTP\” and installed the dll From “LearnQuickTestPDF” dir.

But its not working when I try to use the functions from qtp.
When i closely analysed, i came to know that “LearnQuickTest.ManipulatePDF.dll” is not registered , bcoz its not created the LearnQuickTest.ManipulatePDF.tld” file in the same location. Also When I debugged the code in Install its not assigning the foldername with “C:\LearnQTP\LearnQuickTestPDF”.

So can you please help me out to solve this issue? it will be helpful to me since my current project has lot of PDF reports to check.
Or can you please tell me how to register it manually???

#61 art on 02.23.11 at 20:47

hi Saket, so to use this at work, we need to purchase a license? how does it work? Thanks

#62 art on 02.24.11 at 22:50

thanks Saket – all works. Indeed, nice dll! I didn’t get the agreement part about licensing, is there a paid version of this so we can purchase it to start using at work?

#63 Ankur on 02.24.11 at 23:23

@art: Please check comment #16 by Saket above for T & C.

#64 Bobby on 03.15.11 at 23:53

Hi Saket,

I tried this API and now it is working fine once I registered the dll.My concern is when we are using Compare PDFs method,it is not compared the values which are filled in the form with the original one.For ex., if we take your example fw4.pdf and fw4filled.pdf,when we compare it is passing the test instead of showing the differences with the values filled in fw4filled.pdf.Can you please let me know how can I get the differences in thsi scenario as it is something that deals with my real time job.Appreciate your help.

#65 subh on 04.25.11 at 21:44

Hi Sanket,

this is utility sounds very helpful. could you list down all possible methods and also let me know if there is any method to validate tables or images.

Regards,
Subh

#66 Rani on 06.07.11 at 04:33

Hello Sanket,

Thank you so much for the API.

The following is not working. If I compare 2 pdfs (file1 is copy of file2) I still get the message that there is difference. Can you please help me with this. Thank you so much for all your help.

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

#67 Balaji on 06.21.11 at 08:14

Hi Saket,

LearnQuickTestPDF API is very helpful and thanks for your contibution.

I’m trying to use the command “DownloadPDF” and when i try to open the destination PDF after execution, i’m getting an error: There was an error opening this document. The file is damaged and could not be repaired”

Syntax:
Set oPDF=createobject(“LearnQuickTest.ManipulatePDF”)
oPDF.DownloadPDF SourcePDFURL,”C:\Users\Desktop\test.pdf”

Not sure what exactly is causing this error.

Can you please help me on this?

#68 sanjay on 06.24.11 at 11:07

Nice i am very happy see this..
i have implemented successfully and also read the all method in this dll

#69 Babar Khurshid on 07.05.11 at 11:08

I must say, Awsome work indeed. Great. Keep it up.

#70 Kash on 07.27.11 at 01:07

Hi Saket

Great Work and Appreciated !!!

Its really helpful…

I also have the same question which Jalpa, Subh has asked on this post. What If PDF file has Images, Table and other Toolbar. As per the current compare function it only verify text.

I would also suggest that for reporting if we can get the details as well.

#71 Kishore on 09.29.11 at 18:37

i am getting follwoing error when i use your API.

Rebuild failed: trailer not found.; Original message: PDF startxref not found.

#72 Jon on 12.03.11 at 00:09

Hi,

I have a rquirement, where need to read data from a table structure from PDF and validate the same against database. I have tried “LearnQuickTest.ManipulatePDF”, but not geting data in tabular structure and “GetFieldNames” returns a blank file.Is there any way to get data in tabular structure ? Thanks in advance !!

#73 prasad on 01.14.12 at 11:57

1) vbscript to convert .bmp to .pdf.

2) My scenario is
In my application tr. are 3 levels for each level I need to capture screen shot and save all screen shots in one pdf
any one help me for above scenarios

#74 Preethi on 01.18.12 at 14:44

Hi Saket,

Is that possible to compare pdf files with graphs and charts using this API.

#75 Sandeep on 01.24.12 at 15:35

Hi

Can you let me know, how we can save the contents of pdf file in text with wrap text … All the text is coming in single line.

Leave a Comment