2017-02-06 4 views
0

로컬 형식으로 숫자를 쓸 수있는 PDF 양식을 보았으며 PDFBox로 읽을 수있는 백그라운드에서 이중 값을 저장했습니다. 예제에서 해당 필드를 어떻게 알 수 있습니까? 125.5 (double)의 숫자를 사용하고 "125,5"(내 지역)를 표시 하시겠습니까? 그리고 사용자가 필드를 편집 할 때 배경의 값은 여전히 ​​유효한 double입니다. 일부 메커니즘이 내장되어 있거나 해결 방법이 어떻게 생겼습니까? 미리 감사드립니다.서식이 지정된 숫자 표시 및 이중 값 저장

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

이것이 최선의 방법인지는 모르겠지만 지금은 누군가가 더 나은 해결책을 제시 할 때까지 그대로 두겠습니다.

나는 시각 표시 필드를 편집 할 때마다 javacript로 형식화 된 "background"값을 가진 숨겨진 필드와 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; 
    } 
}