小编Joh*_*nna的帖子

Apache PDFBox:从 ​​PDAnnotationWidget 或 PDTextField 获取对齐方式和字体

我有一个带有表单字段的现有 pdf 文件,可以由用户填写。此表单字段具有在创建 pdf 文件时定义的字体和文本对齐方式。

我使用 Apache PDFBox 在 pdf 中查找表单字段:

PDDocument document = PDDocument.load(pdfFile);
PDAcroForm form = document.getDocumentCatalog().getAcroForm();

PDTextField textField = (PDTextField)form.getField("anyFieldName");
if (textField == null) {
  textField = (PDTextField)form.getField("fieldsContainer.anyFieldName");
}

List<PDAnnotationWidget> widgets = textField.getWidgets();
PDAnnotationWidget annotation = null;
if (widgets != null && !widgets.isEmpty()) {
  annotation = widgets.get(0);

  /* font and alignment needed here */
}
Run Code Online (Sandbox Code Playgroud)

如果我将表单字段的内容设置为

textField.setValue("This is the text");
Run Code Online (Sandbox Code Playgroud)

那么表单字段中的文本具有与此字段预定义的相同字体和对齐方式。

但是我需要第二个字段的对齐方式和字体(顺便说一句,这不是表单字段)。

如何找出PDType1Font为此表单字段定义的对齐方式(左、中、右)和字体(我需要 a及其大小)?…… 喜欢font = annotation.getFont()alignment = annotation.getAlignment()两者都不存在。

如何获得字体和对齐方式?

    1. 17:编辑 …

java pdf pdf-generation pdfbox

3
推荐指数
1
解决办法
1501
查看次数

标签 统计

java ×1

pdf ×1

pdf-generation ×1

pdfbox ×1