如何使用PDFBox将图标导入PDF中的按钮字段? [英] How to import an icon to a button field in a PDF using PDFBox?

查看:207
本文介绍了如何使用PDFBox将图标导入PDF中的按钮字段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法将PDF文件中按钮字段的正常外观设置为图像文件,但我找不到有关此过程的任何信息。

I'm looking for a way to set the normal appearance of a button field in a PDF file to an image file, but am not finding any information about this process.

我能找到的最接近的是相反的,即如何将图标从按钮字段提取到独立的图像文件,这里:如何使用Apache PDFBox从PDF中的按钮图标中提取图像?

The closest I could find was the opposite, ie how to extract an icon from a button field to a stand-alone image file, here: How can i extract image from button icon in PDF using Apache PDFBox?

我更喜欢使用PDFBox来完成这项任务。

I would prefer to use PDFBox for this task.

非常感谢任何帮助。

推荐答案

您可以使用PDFBox创建一个带有图像外观的按钮,如下所示:

You can create a button with an image appearance using PDFBox like this:

try (   InputStream resource = getClass().getResourceAsStream("2x2colored.png");
        PDDocument document = new PDDocument()  )
{
    BufferedImage bufferedImage = ImageIO.read(resource);
    PDImageXObject pdImageXObject = LosslessFactory.createFromImage(document, bufferedImage);
    float width = 10 * pdImageXObject.getWidth();
    float height = 10 * pdImageXObject.getHeight();

    PDAppearanceStream pdAppearanceStream = new PDAppearanceStream(document);
    pdAppearanceStream.setResources(new PDResources());
    try (PDPageContentStream pdPageContentStream = new PDPageContentStream(document, pdAppearanceStream))
    {
        pdPageContentStream.drawImage(pdImageXObject, 0, 0, width, height);
    }
    pdAppearanceStream.setBBox(new PDRectangle(width, height));

    PDPage page = new PDPage(PDRectangle.A4);
    document.addPage(page);

    PDAcroForm acroForm = new PDAcroForm(document);
    document.getDocumentCatalog().setAcroForm(acroForm);

    PDPushButton pdPushButton = new PDPushButton(acroForm);
    pdPushButton.setPartialName("ImageButton");
    List<PDAnnotationWidget> widgets = pdPushButton.getWidgets();
    for (PDAnnotationWidget pdAnnotationWidget : widgets)
    {
        pdAnnotationWidget.setRectangle(new PDRectangle(50, 750, width, height));
        pdAnnotationWidget.setPage(page);
        page.getAnnotations().add(pdAnnotationWidget);

        PDAppearanceDictionary pdAppearanceDictionary = pdAnnotationWidget.getAppearance();
        if (pdAppearanceDictionary == null)
        {
            pdAppearanceDictionary = new PDAppearanceDictionary();
            pdAnnotationWidget.setAppearance(pdAppearanceDictionary);
        }

        pdAppearanceDictionary.setNormalAppearance(pdAppearanceStream);
    }

    acroForm.getFields().add(pdPushButton);

    document.save(new File(RESULT_FOLDER, "imageButton.pdf"));
}

CreateImageButton.java test testCreateSimpleImageButton

(CreateImageButton.java test testCreateSimpleImageButton)

由于您没有提及任何版本要求,我认为您的意思是当前的PDFBox 2.0.x 。

As you did not mention any version requirements, I assumed you meant a current PDFBox 2.0.x.

这篇关于如何使用PDFBox将图标导入PDF中的按钮字段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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