2017-02-08 4 views
1

私はこのコードを使用してPDFファイルにコンボボックスを作成します。それには2つの問題があります。PDFBoxの奇妙なコンボボックスの動作

  1. コンボボックスを開いたときに特殊文字(öなど)が正しく表示されますが、コンボボックスが閉じているときは表示できません。
  2. AcrobatでPDFを開くと、値を変更してPDFを保存すると、コンボボックスが何とか消えてしまいます。 PDFをもう一度開くと、もう表示されません。

PDFBoxクラスで何か問題があったのですか、それとも問題がありますか?ここで

は開いた状態での画像です:

opened

、ここでは、閉じた状態でのものです:あなたのコードの終わり近く

closed

public class ComboTest { 
     public static void main(String[] args) { 

     PDFont font = PDType1Font.HELVETICA; 
     Color color = Color.BLACK; 
     float fontSize = 12; 

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

     document.addPage(page); 

     PDAcroForm acroForm = new PDAcroForm(document); 
     PDComboBox comboBox = new PDComboBox(acroForm); 
     comboBox.setPartialName("test"); 

     String defaultAppearanceString = "/" + font.getName() + " " + fontSize + " Tf " 
       + 0 + " " + 0 + " " + 0 + " rg"; 
     comboBox.setDefaultAppearance(defaultAppearanceString); 

     PDAnnotationWidget widget = new PDAnnotationWidget(); 
     widget.setRectangle(new PDRectangle(200, 200, 100, 20)); 
     widget.setAnnotationFlags(4); 
     widget.setPage(page); 
     widget.setParent(comboBox); 

     List<String> exportValues = new ArrayList<>(); 
     List<String> displayValues = new ArrayList<>(); 

     displayValues.add("öne"); 
     displayValues.add("two"); 
     displayValues.add("thrée"); 

     exportValues.add("1"); 
     exportValues.add("2"); 
     exportValues.add("3"); 


     comboBox.setOptions(exportValues, displayValues); 

     List<PDAnnotationWidget> widgets = new ArrayList<>(); 
     widgets.add(widget); 
     try { 
      page.getAnnotations().add(widget); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     comboBox.setWidgets(widgets); 


     try { 
      FileOutputStream output = new FileOutputStream("test.pdf"); 
      document.save(output); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

    } 
    } 
+0

)、あなたのコードにこれを追加すること: 'acroForm.getFields()(コンボボックス)を追加します。 document.getDocumentCatalog()。setAcroForm(acroForm); '。このコードはどこで入手できましたか? –

+0

私はインターネット上で例を見つけませんでしたので、私は自分自身を試しました。これはうまくいきましたが、今はもう消えません。 – user489872

+0

re 1)、font.getName()を "Helv"に置き換えます。これがなぜそうであるか分かりません。 –

答えて

1

、これを追加:

acroForm.getFields().add(comboBox); 
document.getDocumentCatalog().setAcroForm(acroForm); 

これは、あなたのアクロフォームとそのフィールドがPDFに認識されていることを確認します。

特殊文字を再入力して、Helveticaフォントの名前をAdobeの標準名である "Helv"に置き換えます。

よりクリーンなソリューション:デフォルトのリソースを設定します。代わりに「のHelv」の

PDResources dr = new PDResources(); 
dr.put(COSName.getPDFName("Helv"), font); 
acroForm.setDefaultResources(dr); 

あなたもCOSName.getPDFName(font.getName())を使用することができますが、それは、あなたのデフォルトの外観の文字列で同じである必要があります。

だから、完全なコードは以下のようになります。2再

public class ComboTest 
{ 
    public static void main(String[] args) 
    { 

     PDFont font = PDType1Font.HELVETICA; 
     Color color = Color.BLACK; 
     float fontSize = 12; 

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

     document.addPage(page); 

     PDAcroForm acroForm = new PDAcroForm(document); 
     PDComboBox comboBox = new PDComboBox(acroForm); 
     comboBox.setPartialName("test"); 

     // Helv instead of Helvetica 
     String defaultAppearanceString = "/Helv " + fontSize + " Tf " 
       + 0 + " " + 0 + " " + 0 + " rg"; 
     comboBox.setDefaultAppearance(defaultAppearanceString); 

     PDAnnotationWidget widget = new PDAnnotationWidget(); 
     widget.setRectangle(new PDRectangle(200, 200, 100, 20)); 
     widget.setAnnotationFlags(4); 
     widget.setPage(page); 
     widget.setParent(comboBox); 

     List<String> exportValues = new ArrayList<>(); 
     List<String> displayValues = new ArrayList<>(); 

     displayValues.add("öne"); 
     displayValues.add("two"); 
     displayValues.add("thrée"); 

     exportValues.add("1"); 
     exportValues.add("2"); 
     exportValues.add("3"); 

     comboBox.setOptions(exportValues, displayValues); 

     List<PDAnnotationWidget> widgets = new ArrayList<>(); 
     widgets.add(widget); 
     try 
     { 
      page.getAnnotations().add(widget); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

     comboBox.setWidgets(widgets); 

     // new 
     acroForm.getFields().add(comboBox); 
     document.getDocumentCatalog().setAcroForm(acroForm); 
     PDResources dr = new PDResources(); 
     dr.put(COSName.getPDFName("Helv"), font); 
     acroForm.setDefaultResources(dr); 

     try 
     { 
      FileOutputStream output = new FileOutputStream("test.pdf"); 
      document.save(output); 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 
     } 

    } 
} 
+0

'dr.put(" Helv "、font);'は私のためには機能しません。最初の引数は 'COSName'である必要があります – user489872

+1

固定(うまくいけば) –