PDFFillInformationUtil.java 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package com.kingdee.eas.custom.ssPDF.utils;
  2. import com.itextpdf.text.pdf.*;
  3. import com.kingdee.bos.BOSException;
  4. //import net.lingala.zip4j.ZipFile;
  5. import net.lingala.zip4j.exception.ZipException;
  6. import net.lingala.zip4j.model.ZipParameters;
  7. import java.io.ByteArrayOutputStream;
  8. import java.io.File;
  9. import java.io.FileOutputStream;
  10. import java.io.IOException;
  11. import java.util.ArrayList;
  12. import java.util.Iterator;
  13. import java.util.List;
  14. import java.util.Map;
  15. public class PDFFillInformationUtil {
  16. /**
  17. * 给pdf填充数据
  18. * @param file 原始文件
  19. * @param field map<String,String> key 为字段名,value 为填充值
  20. * @return 填充后的文件 文件类型为byte[]
  21. */
  22. public File fillInformation(byte[] file, Map<String,String> field,String tempFilePath,String fileName) throws IOException {
  23. try {
  24. System.setProperty("javax.xml.parsers.DocumentBuilderFactory","com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl");
  25. // 读取PDF模板
  26. PdfReader pdfReader = new PdfReader(file);
  27. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  28. // 创建需就改PDF文件
  29. PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
  30. AcroFields form = pdfStamper.getAcroFields();
  31. BaseFont bf = BaseFont.createFont("c:/windows/fonts/SIMSUN.TTC,1",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 设置字段数据
  32. ArrayList<BaseFont> fontList = new ArrayList<BaseFont>();
  33. fontList.add(bf);
  34. form.setSubstitutionFonts(fontList);
  35. Iterator<Map.Entry<String, String>> iterator = field.entrySet().iterator();
  36. while (iterator.hasNext()) {
  37. Map.Entry<String, String> entry = iterator.next();
  38. String key = entry.getKey();
  39. String value = entry.getValue();
  40. //给字段添加值
  41. form.setField(key, value);
  42. //form.setFieldProperty(key, "setflags", PdfFormField.MARKUP_HIGHLIGHT, null);
  43. }
  44. //生成后的pdf 可以在编辑
  45. pdfStamper.setFormFlattening(false);
  46. // 关闭修改器,并释放资源
  47. pdfStamper.close();
  48. pdfReader.close();
  49. File outFile = new File(tempFilePath+"/"+fileName);
  50. // if (!outFile.createNewFile()) {
  51. // throw new RuntimeException("pdf 填充数据失败!!");
  52. // }
  53. if (!(outFile.exists())) {
  54. outFile.createNewFile();
  55. } else {
  56. outFile.delete();
  57. outFile.createNewFile();
  58. }
  59. FileOutputStream fos = new FileOutputStream(outFile);
  60. baos.writeTo(fos);
  61. fos.close(); // 关闭流释放资源
  62. return outFile;
  63. } catch (Exception e) {
  64. e.printStackTrace();
  65. throw new RuntimeException("pdf 填充数据失败!!");
  66. }
  67. }
  68. /**
  69. * 创建压缩包
  70. *
  71. * @param destFileName
  72. * @param files
  73. * @return
  74. */
  75. // public static String compress(String destFileName, List<File> files) throws BOSException {
  76. // ZipParameters parameters = new ZipParameters();
  77. // //parameters.setCompressionMethod(8);
  78. // //parameters.setCompressionLevel(5);
  79. // try {
  80. // ZipFile zipFile = new ZipFile(destFileName);
  81. // for (int i = 0; i < files.size(); ++i) {
  82. // File file = files.get(i);
  83. // zipFile.addFile(file, parameters);
  84. // }
  85. // return destFileName;
  86. // } catch (ZipException var9) {
  87. // var9.printStackTrace();
  88. // throw new BOSException(var9.getMessage());
  89. // }
  90. // }
  91. }