显示格式化数字并保存双精度值

Show formatted number and save double value

我见过 PDF 表单,您可以在其中以本地格式写入数字,并且 PDF 在后台存储双精度值,可以使用 PDFBox 读取。 我怎样才能告诉我的例子中的领域,例如取数字 125.5(双)并显示“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();
    }
}

我不知道这是否是最好的方法,但现在我会这样,直到有人想出更好的解决方案。

我使用 UI 表示创建我的可见字段,并使用 "background" 值创建我的可见字段,每次我编辑可见字段时,该隐藏字段由 javascript 格式化。 当我尝试读取数据时,我必须忽略可见字段并专注于隐藏字段。

这对我来说是最简单的解决方案(当然需要稍微清理一下)

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;
    }
}