简体   繁体   English

如何使用PDFBox将图标导入PDF中的按钮字段?

[英]How to import an icon to a button field in a PDF using PDFBox?

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. 我正在寻找一种将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? 我能找到的最接近的是相反的方向,即如何从按钮字段中提取图标到独立的图像文件,在这里: 如何使用Apache PDFBox从PDF中的按钮图标中提取图像?

I would prefer to use PDFBox for this task. 我希望将PDFBox用于此任务。

Any help is greatly appreciated. 任何帮助是极大的赞赏。

You can create a button with an image appearance using PDFBox like this: 您可以使用PDFBox创建具有图像外观的按钮,如下所示:

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测试testCreateSimpleImageButton

As you did not mention any version requirements, I assumed you meant a current PDFBox 2.0.x. 由于您没有提到任何版本要求,因此我假设您的意思是当前的PDFBox2.0.x。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM