PDFFillInformationUtil.java 3.1 KB

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