1

This is my first experience of using PDFBox jar files. Also, I have recently started working on TestComplete. In short, all these things are new for me and I have been stuck on one issue for last few hours. I will try to explain as much as I can. Would really appreciate any help!

Objective:

To save an image present in a PDF file on the file system

Issue:

When this line gets executed objImage.write2file_2(strSavePath);, I get the error Object doesn't support this property or method.

I am taking some help from here

Code:

function fn_PDFImage()
{
    var objPdfFile, strPdfFilePath, strSavePath, objPages, objPage, objImages, objImage, imgbuffer;
    strPdfFilePath = "C:\\Users\\aabb\\Desktop\\name.pdf";
    strSavePath = "C:\\Users\\aabb\\Desktop\\abc";

    objPdfFile = JavaClasses.org_apache_pdfbox_pdmodel.PDDocument.load_3(strPdfFilePath);
    objPages = objPdfFile.getDocumentCatalog().getAllPages();

    //getting a page with index=1
    objPage = objPages.get(1)           
    objImages = objPage.getResources().getXObjects().values().toArray();
    Log.Message(objImages.length);          //This is returning 14. i.e, 14 images

    //getting an image with index=1
    objImage = objImages.items(1);
    Log.Message(typeof objImage);           //returns "Object" which means it is not null

    //saving the image
    objImage.write2file_2(strSavePath);      //<---GETTING AN ERROR HERE       
}

ERROR:

enter image description here

If you are bothered about the method namewrite2file_2, please read this excerpt from the link which I have shared:

In Java, the constructor of a class has the name of this class. TestComplete changes the constructor names to newInstance(). If a class has overloaded constructors, TestComplete names them like newInstance, newInstace_2, newInstance_3 and so on.

Additional Info:

I have imported Jar file(pdfbox-app-1.8.13.jar) and their classes in testcomplete. I am not sure if I need to import some other jar file or its class here:

enter image description here

Gurmanjot Singh
  • 10,224
  • 2
  • 19
  • 43
  • 1
    what type is `objImages`? XObjects are not always image XObjects. And write2file is in the class PDXObjectImage. – Tilman Hausherr Oct 30 '17 at 13:49
  • @TilmanHausherr Thanks for pointing me in the right direction. It seems that whenever the xobject is of subtype `PDXObjectForm`, we get that error. I executed my code for some other PDF file containing only `PDPixelMap` or `PDJpeg` and it worked fine. It was able to save the images on the file system. Could you please post this as an answer. Also, could you please tell me a way to save the Xobjects of subtype `PDXObjectForm`? – Gurmanjot Singh Oct 30 '17 at 15:07

1 Answers1

1

XObjects are not always image XObjects. And write2file is in the class PDXObjectImage so you need to check your object type first.

Re the second question asked in the comment: the form XObject isn't something you can save. XObject forms are content streams with resources etc, similar to pages. However what you can do is to explore these too whether the resources have images. See how this is done in the ExtractImages source code of PDFBox 1.8.

However there are other places where there can be images (e.g. patterns, soft masks, inline images); this is only available in PDFBox 2.*, see the ExtractImages source code there. (Note that the class names are different).

Tilman Hausherr
  • 17,731
  • 7
  • 58
  • 97