2017-02-06 4 views
0

私はあなたのローカルフォーマットで数値を書くことができたPDFフォームを見てきました.PDFBoxで読むことができるバックグラウンドで二重の値が保存されています。 私の例でフィールドをどのように伝えることができますか? 125.5(ダブル)と「125,5」(私のロケール)を表示しますか? ユーザーがフィールドを編集するとき、バックグラウンドの値は有効な倍精度です。いくつかのメカニズムが組み込まれていますか、または回避策はどのように見えますか?前もって感謝します。フォーマット番号を表示して二重値を保存

public final class CreateSimpleForm 
{ 
    private static final PDFont FONT = PDType1Font.HELVETICA; 
    private static final float FONT_SIZE = 12; 

    private CreateSimpleForm() 
    { 
    } 

    public static void main(String[] args) throws IOException 
    { 
     PDDocument document = new PDDocument(); 
     PDPage page = new PDPage(PDRectangle.A4); 
     document.addPage(page); 

     PDResources resources = new PDResources(); 
     resources.put(COSName.getPDFName("Helv"), FONT); 

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

     acroForm.setDefaultResources(resources); 

     String defaultAppearanceString = "/Helv 0 Tf 0 g"; 
     acroForm.setDefaultAppearance(defaultAppearanceString); 

     PDTextField textBox = new PDTextField(acroForm); 
     textBox.setPartialName("SampleField"); 
     defaultAppearanceString = "/Helv " + FONT_SIZE + " Tf 0 0 0 rg"; 
     textBox.setDefaultAppearance(defaultAppearanceString); 

     acroForm.getFields().add(textBox); 

     PDAnnotationWidget widget = textBox.getWidgets().get(0); 
     PDRectangle rect = new PDRectangle(50, 750, 200, 50); 
     widget.setRectangle(rect); 
     widget.setPage(page); 

     widget.setPrinted(true); 
     page.getAnnotations().add(widget); 

     textBox.setValue("Sample field"); 

     document.save("test.pdf"); 
     document.close(); 
    } 
} 

答えて

0

これが最善の方法であるかどうかはわかりませんが、今のところ誰かがより良い解決策を思い付くまでそれを残しておきます。

私は可視のフィールドを編集するたびにjavascriptでフォーマットされた "バックグラウンド"値でUI表現と非表示フィールドで私の可視フィールドを作成します。 データを読み込もうとすると、表示されているフィールドを省略し、隠しフィールドに集中する必要があります。

これは(もちろんビットをクリーンアップする必要があります)私のための最も簡単な解決策

public final class CreateSimpleForm { 
    private static final PDFont FONT = PDType1Font.HELVETICA; 
    private PDAcroForm acroForm; 
    private String defaultAppearanceString; 
    private PDPage page; 

    public static void main(String[] args) throws IOException { 
     new CreateSimpleForm(); 
    } 

    private CreateSimpleForm() throws IOException { 
     PDDocument document = new PDDocument(); 
     page = new PDPage(PDRectangle.A4); 
     document.addPage(page); 

     PDResources resources = new PDResources(); 
     resources.put(COSName.getPDFName("Helv"), FONT); 

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

     acroForm.setDefaultResources(resources); 

     defaultAppearanceString = "/Helv 0 Tf 0 g"; 
     acroForm.setDefaultAppearance(defaultAppearanceString); 


     createFormattedField("myField", 125.5); 

     document.save("test.pdf"); 
     document.close(); 
    } 

    private void createFormattedField(String name, Double value) throws IOException { 
     String nameHidden = name + "_hidden"; 
     PDTextField textBox = createField(name, false); 
     textBox.setValue(String.format("%1$,.2f", value)); 
     createField(name + "_hidden", true).setValue(value.toString()); 
     PDActionJavaScript tfJs = new PDActionJavaScript("this.getField(\"" + nameHidden + "\").value = this.getField(\"" + name + "\").value.replace(/\\./g,'').replace(/\\,/g,'.');"); 
     PDAnnotationAdditionalActions actions = new PDAnnotationAdditionalActions(); 
     actions.setPC(tfJs); 
     actions.setBl(tfJs); 
     textBox.getWidgets().get(0).setActions(actions); 
    } 

    private PDTextField createField(String name, boolean hidden) throws IOException { 
     PDTextField textBox = new PDTextField(acroForm); 
     textBox.setPartialName(name); 
     textBox.setDefaultAppearance(defaultAppearanceString); 

     acroForm.getFields().add(textBox); 

     PDAnnotationWidget widget = textBox.getWidgets().get(0); 
     PDRectangle rect = new PDRectangle(50, 750, 200, 50); 
     widget.setRectangle(rect); 
     widget.setPage(page); 

     widget.setPrinted(true); 
     page.getAnnotations().add(widget); 

     widget.setHidden(hidden); 
     return textBox; 
    } 
} 
です
関連する問題