我见过PDF表单,您可以用本地格式编写数字,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 :(得分:0)
我不知道这是不是最好的方法,但现在我会这样做,直到有人提出更好的解决方案。
我创建了带有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;
}
}