首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在PDFBOX-1.8.10中是否有保存图像的字段?

在PDFBOX-1.8.10中是否有保存图像的字段?
EN

Stack Overflow用户
提问于 2015-09-10 10:17:20
回答 1查看 1.6K关注 0票数 3

在PDFBOX中,我想插入一些图像作为表单字段,可以存储在一个顶体中。稍后,对于用户在特定PDTextBox字段中的一些值更改,我希望调用Javascript函数来显示/隐藏上面的图像字段。还请确认javascript中是否支持将图像字段隐藏/显示(也就是说,如果我能够为该图像字段提供名称,那么我认为这是可能的)。

PDFBOX 1.8.10中是否有实现这一目标的功能?如果是,请提供参考代码/文件。

谢谢你的帮助。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-15 07:38:38

我已经修正了上面的问题,把一个图像当作一个领域。

我使用了PDPushButton字段并在其中设置了图像,这样我也可以通过java脚本显示/隐藏图像。

查看下面的示例代码:

代码语言:javascript
运行
复制
package sample.pdfbox.example;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import org.apache.pdfbox.cos.COSArray;
import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.cos.COSFloat;
import org.apache.pdfbox.cos.COSName;
import org.apache.pdfbox.cos.COSString;
import org.apache.pdfbox.exceptions.COSVisitorException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.common.PDStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceDictionary;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAppearanceStream;
import org.apache.pdfbox.pdmodel.interactive.form.PDAcroForm;
import org.apache.pdfbox.pdmodel.interactive.form.PDPushButton;

public class MyPushButton2 
{
    public static void main(String arg[])  throws IOException, COSVisitorException
    {
        PDDocument document = new PDDocument();
        PDPage page = new PDPage(PDPage.PAGE_SIZE_LETTER);
        document.addPage(page);

        PDFont font = PDType1Font.HELVETICA;
        PDResources res = new PDResources();
        String fontName = res.addFont(font);
        String da = "/Helv 0 Tf 0 g";

        COSDictionary acroFormDict = new COSDictionary(); 
        acroFormDict.setItem(COSName.FIELDS, new COSArray());
        acroFormDict.setItem(COSName.DA, new COSString(da));

        PDAcroForm acroForm =  new PDAcroForm(document, acroFormDict);
        acroForm.setDefaultResources(res);
        document.getDocumentCatalog().setAcroForm(acroForm);


        float x = 10f;
        float y = page.getMediaBox().getHeight();  
        float yOffset = 15f;
        float yCurrent = y - yOffset;  

     // Create appearance stream for local image
        COSArray rectStream = new COSArray();
        rectStream.add(new COSFloat(0));
        rectStream.add(new COSFloat(0));
        rectStream.add(new COSFloat(25));
        rectStream.add(new COSFloat(25));

        final PDRectangle boundingBox = new PDRectangle(rectStream);
        InputStream imageStream = MyPushButton2.class.getResourceAsStream("/Penguins.jpg");
        BufferedImage bImage = ImageIO.read(imageStream);
        PDXObjectImage image = new PDPixelMap(document,bImage);

        PDResources holderFormResources = new PDResources();
        PDStream holderFormStream = new PDStream(document);
        PDXObjectForm holderForm = new PDXObjectForm(holderFormStream);
        holderForm.setResources(holderFormResources);
        holderForm.setBBox(boundingBox);
        holderForm.setFormType(1);

        PDAppearanceDictionary appearance = new PDAppearanceDictionary();
        appearance.getCOSObject().setDirect(true);
        PDAppearanceStream appearanceStream = new PDAppearanceStream(holderForm.getCOSStream());
        appearance.setNormalAppearance(appearanceStream);

        PDResources innerFormResources = new PDResources();
        PDStream innerFormStream = new PDStream(document);
        PDXObjectForm innerForm = new PDXObjectForm(innerFormStream);
        innerForm.setResources(innerFormResources);
        innerForm.setBBox(boundingBox);
        innerForm.setFormType(1);

        String innerFormName = holderFormResources.addXObject(innerForm, "FRM");

        PDResources imageFormResources = new PDResources();
        PDStream imageFormStream = new PDStream(document);

        PDXObjectForm imageForm = new PDXObjectForm(imageFormStream);
        imageForm.setResources(imageFormResources);
        imageForm.setBBox(boundingBox);
        imageForm.setFormType(1);

        String imageFormName = innerFormResources.addXObject(imageForm, "n");
        String imageName = imageFormResources.addXObject(image, "img");

        String holderFormComment = "q 1 0 0 1 0 0 cm /" + innerFormName + " Do Q \n";
        String innerFormComment = "q q 5 0 0 5 0 0 cm /" + imageFormName + " Do Q\n";
        String imgFormComment = "q 5 0 0 5 0 0 cm /" + imageName + " Do Q\n";

        appendRawCommands(holderFormStream.createOutputStream(), holderFormComment);
        appendRawCommands(innerFormStream.createOutputStream(), innerFormComment);
        appendRawCommands(imageFormStream.createOutputStream(), imgFormComment);

        // Create Pushbutton containing status image
        COSDictionary cosDict = new COSDictionary();
        COSArray rect = new COSArray();
        rect.add(new COSFloat(100));
        rect.add(new COSFloat(500));
        rect.add(new COSFloat(300));
        rect.add(new COSFloat(700));

        cosDict.setItem(COSName.RECT, rect);
        cosDict.setItem(COSName.FT, COSName.getPDFName("Btn")); // Field Type
        cosDict.setItem(COSName.TYPE, COSName.ANNOT);
        cosDict.setItem(COSName.SUBTYPE, COSName.getPDFName("Widget"));
        cosDict.setItem(COSName.T, new COSString("ImageField1"));
        cosDict.setInt(COSName.F, 4);
        cosDict.setInt(COSName.FF, 65536);

        PDPushButton button = new PDPushButton(document.getDocumentCatalog().getAcroForm(), cosDict);
        button.setReadonly(true);

        button.getWidget().setAppearance(appearance);
        page.getAnnotations().add(button.getWidget());                          
        acroForm.getFields().add(button); // Added this line for Tilman's comment

        yCurrent = yCurrent - 20 - yOffset;

        File file = new File("C:\\pdf\\ImageButton.pdf");
        System.out.println("Sample file saved at : " + file.getAbsolutePath());
        document.save(file);
        document.close();
    }

    public static void appendRawCommands(OutputStream os, String commands) throws IOException {
        os.write(commands.getBytes("ISO-8859-1"));
        os.close();
    }

}

希望这个解决方案有帮助!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32499129

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档