我想使用java netbeans从pdf中的按钮获取图像图标,并将其放在某个面板中。 不过我在这里打砖块。 我正在使用PDFBox作为我的PDF导出器,我似乎无法理解。 我已经成功从表单字段中读取,但只要我尝试在PDFBox中找到它,就没有按钮提取器。 我应该怎么做?是否有可能使用这种方法,或者是否有其他方法。 提前谢谢。
编辑: 我已经使用此代码中的示例实用程序找到了extractimages:
File myFile = new File(filename);
try {
//PDDocument pdDoc = PDDocument.loadNonSeq( myFile, null );
PDDocument pdDoc = null;
pdDoc = PDDocument.load( myFile );
PDDocumentCatalog pdCatalog = pdDoc.getDocumentCatalog();
PDAcroForm pdAcroForm = pdCatalog.getAcroForm();
// dipakai untuk membaca isi file
List pages = pdDoc.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
while( iter.hasNext() )
{
PDPage page = (PDPage)iter.next();
PDResources resources = page.getResources();
Map images = resources.getImages();
if( images != null )
{
Iterator imageIter = images.keySet().iterator();
while( imageIter.hasNext() )
{
String key = (String )imageIter.next();
PDXObjectImage image = (PDXObjectImage)images.get(key);
BufferedImage imagedisplay= image.getRGBImage();
jLabel5.setIcon(new ImageIcon(imagedisplay)); // NOI18N
}
}
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, "error " + e.getMessage());
}
然而,我仍然无法从按钮图像中读取。
顺便说一下,我从这个页面阅读教程,将按钮图像添加到pdf。
https://acrobatusers.com/tutorials/how-to-create-a-button-form-field-to-insert-a-pdf-file
第二编辑:
在这里,我还为您提供了包含图标的pdf的链接。 PDF Link。
提前谢谢。
答案 0 :(得分:3)
当您在PDF中讨论按钮时,我认为您的意思是交互式表单按钮。
PDFBox中的按钮没有明确的图标提取器。但是,随着带有自定义图标的按钮(以及一般注释)将这些图标定义为其外观的一部分,可以简单地(递归地)遍历注释外观的资源并收集 XObject 子类型图片:
public void extractAnnotationImages(PDDocument document, String fileNameFormat) throws IOException
{
List<PDPage> pages = document.getDocumentCatalog().getAllPages();
if (pages == null)
return;
for (int i = 0; i < pages.size(); i++)
{
String pageFormat = String.format(fileNameFormat, "-" + i + "%s", "%s");
extractAnnotationImages(pages.get(i), pageFormat);
}
}
public void extractAnnotationImages(PDPage page, String pageFormat) throws IOException
{
List<PDAnnotation> annotations = page.getAnnotations();
if (annotations == null)
return;
for (int i = 0; i < annotations.size(); i++)
{
PDAnnotation annotation = annotations.get(i);
String annotationFormat = annotation.getAnnotationName() != null && annotation.getAnnotationName().length() > 0
? String.format(pageFormat, "-" + annotation.getAnnotationName() + "%s", "%s")
: String.format(pageFormat, "-" + i + "%s", "%s");
extractAnnotationImages(annotation, annotationFormat);
}
}
public void extractAnnotationImages(PDAnnotation annotation, String annotationFormat) throws IOException
{
PDAppearanceDictionary appearance = annotation.getAppearance();
extractAnnotationImages(appearance.getDownAppearance(), String.format(annotationFormat, "-Down%s", "%s"));
extractAnnotationImages(appearance.getNormalAppearance(), String.format(annotationFormat, "-Normal%s", "%s"));
extractAnnotationImages(appearance.getRolloverAppearance(), String.format(annotationFormat, "-Rollover%s", "%s"));
}
public void extractAnnotationImages(Map<String, PDAppearanceStream> stateAppearances, String stateFormat) throws IOException
{
if (stateAppearances == null)
return;
for (Map.Entry<String, PDAppearanceStream> entry: stateAppearances.entrySet())
{
String appearanceFormat = String.format(stateFormat, "-" + entry.getKey() + "%s", "%s");
extractAnnotationImages(entry.getValue(), appearanceFormat);
}
}
public void extractAnnotationImages(PDAppearanceStream appearance, String appearanceFormat) throws IOException
{
PDResources resources = appearance.getResources();
if (resources == null)
return;
Map<String, PDXObject> xObjects = resources.getXObjects();
if (xObjects == null)
return;
for (Map.Entry<String, PDXObject> entry : xObjects.entrySet())
{
PDXObject xObject = entry.getValue();
String xObjectFormat = String.format(appearanceFormat, "-" + entry.getKey() + "%s", "%s");
if (xObject instanceof PDXObjectForm)
extractAnnotationImages((PDXObjectForm)xObject, xObjectFormat);
else if (xObject instanceof PDXObjectImage)
extractAnnotationImages((PDXObjectImage)xObject, xObjectFormat);
}
}
public void extractAnnotationImages(PDXObjectForm form, String imageFormat) throws IOException
{
PDResources resources = form.getResources();
if (resources == null)
return;
Map<String, PDXObject> xObjects = resources.getXObjects();
if (xObjects == null)
return;
for (Map.Entry<String, PDXObject> entry : xObjects.entrySet())
{
PDXObject xObject = entry.getValue();
String xObjectFormat = String.format(imageFormat, "-" + entry.getKey() + "%s", "%s");
if (xObject instanceof PDXObjectForm)
extractAnnotationImages((PDXObjectForm)xObject, xObjectFormat);
else if (xObject instanceof PDXObjectImage)
extractAnnotationImages((PDXObjectImage)xObject, xObjectFormat);
}
}
public void extractAnnotationImages(PDXObjectImage image, String imageFormat) throws IOException
{
image.write2OutputStream(new FileOutputStream(String.format(imageFormat, "", image.getSuffix())));
}
(来自ExtractAnnotationImageTest.java)
不幸的是,OP没有提供示例PDF,因此我将代码应用于this example file
(存储为资源),如下所示:
/**
* Test using <a href="http://examples.itextpdf.com/results/part2/chapter08/buttons.pdf">buttons.pdf</a>
* created by <a href="http://itextpdf.com/examples/iia.php?id=154">part2.chapter08.Buttons</a>
* from ITEXT IN ACTION — SECOND EDITION.
*/
@Test
public void testButtonsPdf() throws IOException
{
try (InputStream resource = getClass().getResourceAsStream("buttons.pdf"))
{
PDDocument document = PDDocument.load(resource);
extractAnnotationImages(document, new File(RESULT_FOLDER, "buttons%s.%s").toString());;
}
}
(来自ExtractAnnotationImageTest.java)
并得到了这些图片:
和
这里有两个问题:
因此,请使用您的PDF检查此代码。如果需要,可以改进。
OP同时提供了一个示例文件imageicon.pdf
像这样调用上面的方法
/**
* Test using <a href="http://www.docdroid.net/TDGVQzg/imageicon.pdf.html">imageicon.pdf</a>
* created by the OP.
*/
@Test
public void testImageiconPdf() throws IOException
{
try (InputStream resource = getClass().getResourceAsStream("imageicon.pdf"))
{
PDDocument document = PDDocument.load(resource);
extractAnnotationImages(document, new File(RESULT_FOLDER, "imageicon%s.%s").toString());;
}
}
(来自ExtractAnnotationImageTest.java)
输出此图片:
因此,它运作得很好!
OP在评论中指出
仍然使用junit测试方法混淆,但是当我尝试将其调用到我的主程序时,它总是返回“stream close”错误。我已经把文件放在与我的jar相同的目录中,也试图手动给出路径,但仍然是同样的错误。
因此,我在类中添加了main
方法以允许它
在代码中:
public static void main(String[] args) throws IOException
{
ExtractAnnotationImageTest extractor = new ExtractAnnotationImageTest();
for (String arg : args)
{
try (PDDocument document = PDDocument.load(arg))
{
extractor.extractAnnotationImages(document, arg+"%s.%s");;
}
}
}