首先,我要说我是PDFBox及其基础PDF格式的新手。
我有一个传入的PDF (从HTML转换),其中包含一个用于签名字段的占位符。我能够创建PDAcroForm
和签名PDSignatureField
。另外,我可以将表格绝对定位在页面上。我要弄清楚的是如何找到文本占位符并将表单锚定在该位置...
这是我的缩写代码:
try (
final PDDocument document = PDDocument.load(context.getInboundPDF());
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()
) {
final PDPage page = document.getPage(0);
final PDAcroForm acroForm = new PDAcroForm(document);
document.getDocumentCatalog().setAcroForm(acroForm);
final PDSignatureField signatureField = new PDSignatureField(acroForm);
acroForm.getFields().add(signatureField);
final PDAnnotationWidget widget = signatureField.getWidgets().get(0);
final PDRectangle rectangle = new PDRectangle(50, 650, 200, 50);
widget.setRectangle(rectangle);
widget.setPage(page);
page.getAnnotations().add(widget);
document.save(outputStream);
context.setOutboundPDF(outputStream.toByteArray());
} catch(final Exception e) {
LOG.error(e.getMessage(), e);
}
我知道绝对定位是由PDRectangle
对象上的尺寸数据产生的。表单元素是否必须始终绝对定位?能否找到一个段落占位符并将其替换为签名字段小部件?我可以将后续内容下移到页面中以适应签名字段与其占位符之间的高度差吗?如果是这样,如何处理页面溢出?
非常感谢。