将PDF中存在的图像保存在本地文件系统中 [英] Save an image present in PDF on local File System

查看:126
本文介绍了将PDF中存在的图像保存在本地文件系统中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次使用PDFBox jar文件.另外,我最近开始研究TestComplete.简而言之,所有这些事情对我来说都是新事物,最近几个小时我一直只停留在一个问题上.我将尽力解释.非常感谢您的帮助!

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!

目标:

要在文件系统上保存PDF文件中存在的图像

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

问题:

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

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

我从这里寻求帮助

代码:

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感到困扰,请从我共享的链接中阅读以下摘录:

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

在Java中,类的构造函数具有此类的名称. TestComplete将构造函数名称更改为newInstance().如果一个 类具有重载的构造函数,TestComplete将其命名为 newInstance,newInstace_2,newInstance_3等.

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)及其类.我不确定是否需要在此处导入其他jar文件或其类:

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:

推荐答案

XObject并不总是图像XObject.而且write2file位于PDXObjectImage类中,因此您需要首先检查对象类型.

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

回答注释中的第二个问题:XObject表单不是您可以保存的东西. XObject表单是具有资源等内容的内容流,类似于页面.但是,您可以做的是也探索这些资源是否具有图像.在

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.

但是,在其他地方可以有图像(例如图案,软掩膜,嵌入式图像);仅在PDFBox 2. *中可用.请参见

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

这篇关于将PDF中存在的图像保存在本地文件系统中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆