Forum Discussion
I use below code to get images from PDF document, this may help you
var pdfObject = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(fileName); var pageArray = pdfObject.getDocumentCatalog().getAllPages().toArray(); // Get the collection of pages var curPage = pageArray.oleValue[1];//use page number here var arrofImages = curPage.getResources().getImages().values().toArray(); for(p = 0 ; p < arrofImages.length ; p++) { var curImage = arrofImages.items(p); }
Hi,
Thanks for the help. I am getting the an error : Object doesn't support this property or method
function test()
{
strPDFPath = "J:\\Documents\\PV.ps.pdf"
strImagePath = "J:\\Documents\\expectedgrapgh.png"
objFile = JavaClasses.java_io.File.newInstance(strPDFPath)
objDoc = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_2(objFile);
arrPages = objDoc.getDocumentCatalog().getPages();
//var pageArray = pdfObject.getDocumentCatalog().getAllPages().toArray(); // Get the collection of pages
for(var i=1; i<=arrPages.getCount(); i++)
{
pageObj = objDoc.getPage(i);
arrofImages = pageObj.getResources().getImages().values().toArray(); //getting error at this line
imgMap = pageObj.getResources().getXObject();
imgArray = imgMap.values().toArray();
//imageObj = imgArray.items(imgIndex);
for(var j = 0 ; j < imgArray.length ; j++)
{
curImage = imgArray.items(p);
- AlexKaras8 years ago
Champion Level 2
a) It might help if you let us know where the mentioned error occurs;
b) The following threads might help:
Getting exception while trying to read PDF file using pdfbox dll
http://community.smartbear.com/t5/Functional-Web-Testing/Getting-exception-while-trying-to-read-PDF-file-using-pdfbox-dll/td-p/85511
http://smartbear.com/forums/f81/t88540/getting-exception-while-trying-to-read-pdf-file/
how to get the content of a pdf file on window (PDFBox)
https://community.smartbear.com/t5/TestComplete-Desktop-Testing/Compare-pdf-files/m-p/122339
http://community.smartbear.com/t5/Functional-Web-Testing/how-to-get-the-content-of-a-pdf-file-on-window/td-p/60183
http://smartbear.com/forums/f75/t61368/how-to-get-the-content-of-a-pdf-file-on-window/
Unable to read pdf : “dotNET.org_apache_pdfbox_pdmodel.PDDocument” is null or not an object
http://community.smartbear.com/t5/Functional-Web-Testing/Unable-to-read-pdf-dotNET-org-apache-pdfbox-pdmodel-PDDocument/td-p/94722> var curImage = arrofImages.items(p);
Wondering in what format the image is stored to the curImage variable? Is it Base64 encoded byte array or something else? Have you tried (and, hopefully succeeded in) to convert it to something compatible with TestComplete's Picture object?
- shankar_r8 years agoCommunity Hero
Below is the sample how i did image comparison with expected image and PDF image
curImagePath = "c:\pdfimages/pdfimage1.png"; var curImage = arrofImages.items(p); curImage.write2file_2(curImagePath ); var actualImage = Utils.Picture; actualImage.LoadFromFile(curImagePath); //Load the expected image var expectedImage = Utils.Picture; expectedImage.LoadFromFile(imagePath); if(Regions.Compare(expectedImage,actualImage)) { Log.Checkpoint("Passed");}else{ Log.Error("Failed");}
- AlexKaras8 years ago
Champion Level 2
I see, thank you a lot, Shankar!
Format is handled internally by PDFBox. Sounds good for me.
- jasmeenkaur278 years agoContributor
AlexKaras- I am getting error at this line
arrofImages = pageObj.getResources().getImages().values().toArray();
shankar_r - Could you please confiorm which jar are you using. Mine is 1.8.13
- AlexKaras8 years ago
Champion Level 2
Hi,
> which jar are you using. Mine is 1.8.13
https://community.smartbear.com/t5/TestComplete-Desktop-Testing/Compare-pdf-files/m-p/122418#M6672 link describes some differences between PDFBox version 1.8.x and 2.x and may help.