2016-10-10 2 views
1

私は、PDFファイル内のボタンフィールドの通常の外観をイメージファイルに設定する方法を探していますが、このプロセスに関する情報は見つかりません。PDFBoxのボタンフィールドにアイコンをインポートするにはどうすればいいですか?

私は見つけることができる最も近いここで、スタンドアロンのイメージファイルへのボタンフィールドからアイコンを抽出する方法、すなわち、反対だった:How can i extract image from button icon in PDF using Apache PDFBox?

私はこの作業のためにPDFBoxを使用することを好むだろう。

ご協力いただきまして誠にありがとうございます。

答えて

1

あなたはこのよう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テストtestCreateSimpleImageButton

現在のPDFBox 2.0.x。

関連する問題