使用acroFields.setFieldProperty(nameField, "textfont", baseFont, null);的方式不能加粗,因为第三个参数必须是BaseFont类型,不能是Font类型。
可以使用下面的方式加粗:
BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font font = new Font(bf, 12, Font.BOLD);
//Font font = FontFactory.getFont(FontFactory.COURIER, 20, Font.BOLD, BaseColor.RED);
AcroFields s = ps.getAcroFields();
// 设置加粗只能用这种方式
List multiLinePosition = s.getFieldPositions("name");
int page = multiLinePosition.get(0).page;
Rectangle rectangle = multiLinePosition.get(0).position;
float left = rectangle.getLeft();
float right = rectangle.getRight();
float top = rectangle.getTop();
float bottom = rectangle.getBottom();
PdfContentByte pdfContentByte = ps.getOverContent(page);
ColumnText columnText = new ColumnText(pdfContentByte);
Rectangle r = new Rectangle(left, bottom, right, top);
columnText.setSimpleColumn(r);
//FontFactory.getFont(FontFactory.COURIER, 20, Font.BOLD, BaseColor.RED)
Chunk chunk = new Chunk("在路上");
Paragraph paragraph = new Paragraph(12, chunk);
//paragraph.setSpacingBefore(16);
columnText.addText(paragraph);
// 设置字体,如果不设置添加的中文将无法显示
paragraph.setFont(font);
columnText.addElement(paragraph);
columnText.go();
这种方式比较笨重,但是暂时未找到好的方式。