Explorar o código

修改PdfFormUtil增加图片变量替换保护性代码

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi hai 1 ano
pai
achega
ca56f129c6

+ 14 - 10
tools-pdf/src/main/java/com/qmth/boot/tools/pdf/PdfFormUtil.java

@@ -28,21 +28,25 @@ public class PdfFormUtil {
         AcroFields form = stamp.getAcroFields();
         // 遍历表单字段
         for (String key : form.getAllFields().keySet()) {
-            boolean image = form.getFieldType(key) == 1;
             String param = getParam(key, params);
             // 替换图片变量区内容
-            if (image && param.length() > 0) {
-                int size = form.getFieldItem(key) != null ? form.getFieldItem(key).size() : 0;
-                for (int i = 0; i < size; i++) {
-                    PushbuttonField field = form.getNewPushbuttonFromField(key, i);
-                    field.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
-                    field.setProportionalIcon(true);
-                    field.setImage(Image.getInstance(ByteArray.fromBase64(param).value()));
-                    form.replacePushbuttonField(key, field.getField(), i);
+            if (form.getFieldType(key) == 1) {
+                try {
+                    Image image = Image.getInstance(ByteArray.fromBase64(param).value());
+                    int size = form.getFieldItem(key) != null ? form.getFieldItem(key).size() : 0;
+                    for (int i = 0; i < size; i++) {
+                        PushbuttonField field = form.getNewPushbuttonFromField(key, i);
+                        field.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
+                        field.setProportionalIcon(true);
+                        field.setImage(image);
+                        form.replacePushbuttonField(key, field.getField(), i);
+                    }
+                } catch (Exception exception) {
+                    continue;
                 }
             }
             // 替换文字变量区内容
-            else if (!image) {
+            else {
                 form.setFieldProperty(key, "textfont", baseFont, null);
                 form.setField(key, param);
             }

+ 1 - 1
tools-pdf/src/test/java/com/qmth/boot/test/tools/pdf/PdfFormUtilTest.java → tools-pdf/src/test/java/com/qmth/boot/test/tools/pdf/PdfFormTest.java

@@ -12,7 +12,7 @@ import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
 
-public class PdfFormUtilTest {
+public class PdfFormTest {
 
     @Test
     public void test() throws IOException, WriterException {