Save an image present in PDF on local File System

亡梦爱人 提交于 2019-12-11 09:47:27

问题


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:

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:


回答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).



来源:https://stackoverflow.com/questions/47013995/save-an-image-present-in-pdf-on-local-file-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!