|
|
|
@ -20,11 +20,13 @@ import java.util.Map;
|
|
|
|
|
public class PdfService {
|
|
|
|
|
|
|
|
|
|
// 定义最大和最小字体大小,用于动态调整
|
|
|
|
|
private static final float MAX_FONT_SIZE = 14;
|
|
|
|
|
private static final float MAX_FONT_SIZE = 12;
|
|
|
|
|
private static final float MIN_FONT_SIZE = 8;
|
|
|
|
|
// 预估的字段宽度(根据实际PDF调整)
|
|
|
|
|
private static final float ESTIMATED_FIELD_WIDTH = 100f;
|
|
|
|
|
// 需要特殊处理的字段列表
|
|
|
|
|
private static final String[] AUTO_WRAP_FIELDS = {
|
|
|
|
|
"常住地址", "户籍地址", "户籍地公安机关", "常住地公安机关"
|
|
|
|
|
"常住地址", "户籍地址", "户籍地公安机关", "常住地公安机关","姓名","已(拟)任涉密岗位"
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public ByteArrayOutputStream fillForm(Map<String, String> formData,
|
|
|
|
@ -68,14 +70,23 @@ public class PdfService {
|
|
|
|
|
// for (PDAnnotationWidget widget : field.getWidgets()) {
|
|
|
|
|
// widget.setHighlightMode(PDAnnotationWidget.HIGHLIGHT_MODE_NONE);
|
|
|
|
|
// }
|
|
|
|
|
if (fieldValue != null &&!fieldValue.isEmpty()) {
|
|
|
|
|
if (fieldValue != null && !fieldValue.isEmpty()) {
|
|
|
|
|
field.setValue(fieldValue);
|
|
|
|
|
if (field instanceof PDTextField) {
|
|
|
|
|
PDTextField textField = (PDTextField) field;
|
|
|
|
|
if (isAutoWrapField(fieldName)) {
|
|
|
|
|
// 计算文本在最大字体下的宽度
|
|
|
|
|
float textWidth = calculateTextWidth(fieldValue, font, MAX_FONT_SIZE);
|
|
|
|
|
// 如果文本宽度超过预估字段宽度,启用自动换行并调整字体
|
|
|
|
|
if (textWidth > ESTIMATED_FIELD_WIDTH) {
|
|
|
|
|
textField.setMultiline(true);
|
|
|
|
|
float fontSize = calculateFontSize(fieldValue);
|
|
|
|
|
float fontSize = calculateFontSize(fieldValue, font);
|
|
|
|
|
textField.setDefaultAppearance("/" + font.getName() + " " + fontSize + " Tf 0 g");
|
|
|
|
|
} else {
|
|
|
|
|
// 文本未超出,使用默认字体和不换行
|
|
|
|
|
textField.setMultiline(false);
|
|
|
|
|
textField.setDefaultAppearance("/" + font.getName() + " " + MAX_FONT_SIZE + " Tf 0 g");
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
textField.setDefaultAppearance("/" + font.getName() + " 12 Tf 0 g");
|
|
|
|
|
}
|
|
|
|
@ -118,12 +129,12 @@ public class PdfService {
|
|
|
|
|
if (refField != null) {
|
|
|
|
|
refField.setReadOnly(true);
|
|
|
|
|
}
|
|
|
|
|
if (refField != null &&!file.isEmpty()) {
|
|
|
|
|
if (refField != null && !file.isEmpty()) {
|
|
|
|
|
// 假设图片插入到第一页,你可以根据实际情况修改
|
|
|
|
|
PDPage page = document.getPage(0);
|
|
|
|
|
|
|
|
|
|
// 手动指定图片插入位置
|
|
|
|
|
float x = 435; // 可以根据实际情况调整2
|
|
|
|
|
float x = 435; // 可以根据实际情况调整
|
|
|
|
|
float y = 620; // 可以根据实际情况调整
|
|
|
|
|
|
|
|
|
|
PDImageXObject image = null;
|
|
|
|
@ -165,13 +176,26 @@ public class PdfService {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算适应字段的字体大小(仅用于需要特殊处理的字段)
|
|
|
|
|
private float calculateFontSize(String fieldValue) {
|
|
|
|
|
int textLength = fieldValue.length();
|
|
|
|
|
float fontSize = 10;
|
|
|
|
|
if (textLength > 30) {
|
|
|
|
|
fontSize = Math.max(MIN_FONT_SIZE, 10 - ((textLength - 30) / 5));
|
|
|
|
|
// 计算文本在指定字体和大小下的宽度
|
|
|
|
|
private float calculateTextWidth(String text, PDType0Font font, float fontSize) throws IOException {
|
|
|
|
|
if (text == null || text.isEmpty()) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
// 计算文本宽度,考虑字符间距
|
|
|
|
|
return font.getStringWidth(text) / 1000 * fontSize;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 计算适应字段的字体大小
|
|
|
|
|
private float calculateFontSize(String fieldValue, PDType0Font font) throws IOException {
|
|
|
|
|
float fontSize = MAX_FONT_SIZE;
|
|
|
|
|
float textWidth = calculateTextWidth(fieldValue, font, fontSize);
|
|
|
|
|
|
|
|
|
|
// 循环降低字体大小直到文本适合预估宽度
|
|
|
|
|
while (textWidth > ESTIMATED_FIELD_WIDTH && fontSize > MIN_FONT_SIZE) {
|
|
|
|
|
fontSize -= 0.5f;
|
|
|
|
|
textWidth = calculateTextWidth(fieldValue, font, fontSize);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fontSize;
|
|
|
|
|
}
|
|
|
|
|
}
|