在本地文件系统上以 PDF 格式保存图像

Save an image present in PDF on local File System

这是我第一次使用 PDFBox jar 文件。另外,我最近开始研究 TestComplete。简而言之,所有这些事情对我来说都是新的,过去几个小时我一直被困在一个问题上。我会尽力解释。非常感谢任何帮助!

Objective:

将 PDF 文件中的图像保存到文件系统

问题

执行此行时 objImage.write2file_2(strSavePath);,我收到错误 Object doesn't support this property or method

I am taking some help from here

代码:

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       
}

错误:

如果您对方法名称感到困扰write2file_2,请阅读我分享的 link 中的这段摘录:

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.

附加信息:

我已经在 testcomplete 中导入了 Jar 文件 (pdfbox-app-1.8.13.jar) 及其 classes。我不确定是否需要在此处导入其他 jar 文件或其 class:

XObject 并不总是图像 XObject。而 write2file 在 class PDXObjectImage 中,所以你需要先检查你的对象类型。

关于评论中提出的第二个问题:表单 XObject 不是您可以保存的东西。 XObject 形式是带有资源等的内容流,类似于页面。然而,您可以做的是探索这些资源是否有图像。在 PDFBox 1.8 的 ExtractImages source code 中查看这是如何完成的。

但是还有其他地方可以有图像(例如图案、软蒙版、内联图像);这仅在 PDFBox 2.* 中可用,请参阅那里的 ExtractImages source code。 (注意 class 名称不同)。