当前位置:   article > 正文

java 生成pdf文件加密_java pdf加密

java pdf加密

前言

一、Ftl模板制作

1.编写HTML标签

2.修改文件后缀,把html改成ftl

二、生成PDF并加密

1.引入jar包

2.关键代码

总结


前言

使用html页面改成ftl模板,生成PDF,并对生成的PDF进行加密


一、Ftl模板制作

1.编写HTML标签

示例如下:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head lang="en">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title>1212</title>
  7. <style type="text/css">
  8. /*解决html转pdf文件中文不显示的问题*/
  9. body {
  10. font-family: "Microsoft YaHei";
  11. font-size: 7pt;
  12. line-height: 17pt;
  13. }
  14. .imgdev {
  15. width: 100%;
  16. display: inline-block;
  17. }
  18. .headerdev {
  19. width: 100%;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div class="imgdev">
  25. <img src="${logo1}" width="310" height="72"/>
  26. <div style="color: #555">
  27. <div>${info1}</div>
  28. <div>${info2}</div>
  29. <hr style="background-color: #888;height: 1px"/>
  30. <div style="fontWeight: 600">${com}</div>
  31. <div style="fontWeight: 700">${comen}</div>
  32. </div>
  33. </div>
  34. <div class="headerdev">
  35. ${context}
  36. <br/>
  37. <img src="${auto}" width="90" height="42" style="padding-left: 50px"/>
  38. <img src="${auto2}" width="100" height="80"/>
  39. <div class="line"></div>
  40. <p>${title}</p>
  41. </div>
  42. </body>
  43. </html>

2.修改文件后缀,把html改成ftl


二、生成PDF并加密

1.引入jar包

  1. <!-- spring boot 项目请添加此依赖 -->
  2. <dependency>
  3.     <groupId>org.springframework.boot</groupId>
  4.     <artifactId>spring-boot-starter-freemarker</artifactId>
  5. </dependency>
  6. <!-- 非spring boot 项目请添加此依赖 -->
  7. <dependency>
  8.     <groupId>org.freemarker</groupId>
  9.     <artifactId>freemarker</artifactId>
  10.     <version>2.3.30</version>
  11. </dependency>
  12. <!-- 对生成的PDF加密的jar -->
  13. <dependency>
  14. <groupId>e-iceblue</groupId>
  15. <artifactId>spire.office.free</artifactId>
  16. <version>2.2.0</version>
  17. </dependency>

2.关键代码

代码如下:

  1. public class PDFTest {
  2. private static String path ="Y:\\1";
  3. /**
  4. * 生成PDF并加密
  5. */
  6. @org.junit.jupiter.api.Test
  7. public byte[] test()
  8. {
  9. Configuration configuration = new Configuration();
  10. configuration.setDefaultEncoding("UTF-8");
  11. //构建PDF需要替换的值
  12. Map psnMap = (Map) this.getFormInfo(pk, visa);
  13. Map map = this.dataDownPdfQuery(psnMap);
  14. String templateName="1.ftl";
  15. configuration.setDirectoryForTemplateLoading(new File(path));
  16. Template templatehtml = configuration.getTemplate(templateName);
  17. File file = new File(templateName);
  18. StringWriter writer = new StringWriter();
  19. templatehtml.process(map, writer);
  20. writer.flush();
  21. OutputStream os = null;
  22. File files=new File(path + File.separator + templateName.replace(".ftl", ".pdf") );
  23. os = new FileOutputStream(files);
  24. ITextRenderer renderer = new ITextRenderer();
  25. renderer.setDocumentFromString(writer.toString());
  26. ITextFontResolver fontResolver = renderer.getFontResolver();
  27. fontResolver.addFont(path + File.separator+"fonts/mcyahei.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  28. renderer.layout();
  29. renderer.createPDF(os);
  30. os.close();
  31. //上半段是生成PDF,下半截是对生成的PDF进行设置密码加密
  32. //创建PdfDocument实例
  33. PdfDocument doc = new PdfDocument();
  34. //加载PDF文件
  35. doc.loadFromFile(files.getAbsolutePath());
  36. doc.getPages().add();
  37. //对文件进行加密
  38. PdfEncryptionKeySize keySize = PdfEncryptionKeySize.Key_128_Bit;
  39. String date="20221222"
  40. String openPassword = date;//打开文档时,仅用于查看文档
  41. String permissionPassword = date;//打开文档时,可编辑文档
  42. EnumSet flags = EnumSet.of(PdfPermissionsFlags.Print, PdfPermissionsFlags.Fill_Fields);
  43. doc.getSecurity().encrypt(openPassword, permissionPassword, flags, keySize);
  44. doc.getPages().remove(doc.getPages().get(doc.getPages().getCount()-1));
  45. //保存文件
  46. doc.saveToFile(files.getAbsolutePath());
  47. doc.close();
  48. return EntryFileConfiguration.fileToBytes(files);
  49. }
  50. }

注意注意!

如果内容中出现了特殊符号记得用转义符替换,&换&

  1. // 获取相应的需要替换的内容
  2. Iterator<Entry<String, Object>> map1it = map.entrySet().iterator();
  3. while (map1it.hasNext()) {
  4. Map.Entry<String, Object> entry = (Entry<String, Object>) map1it
  5. .next();
  6. if (!(entry.getValue() instanceof List)) {
  7. if (entry.getValue() != null
  8. && entry.getValue().toString().contains("&")) {
  9. String newValue = entry.getValue().toString().replace("&", "&amp;");
  10. map.put(entry.getKey(), newValue);
  11. }
  12. if (entry.getValue() != null&& entry.getValue().toString().contains("<")) {
  13. String newValue = entry.getValue().toString().replace("<", "&lt;");
  14. map.put(entry.getKey(), newValue);
  15. }
  16. } else {
  17. for (Object childMap : (List) entry.getValue()) {
  18. if (childMap != null && (childMap instanceof Map)) {
  19. Map<String, Object> child = (Map<String, Object>) childMap;
  20. for (Entry<String, Object> childEntry : child.entrySet()) {
  21. if (childEntry.getValue() == null) {
  22. continue;
  23. }
  24. if (childEntry.getValue().toString().contains("&")) {
  25. String newValue = childEntry.getValue().toString().replace("&", "&amp;");
  26. child.put(childEntry.getKey(), newValue);
  27. }
  28. if (childEntry.getValue().toString().contains("<")) {
  29. String newValue = childEntry.getValue().toString().replace("<", "&lt;");
  30. child.put(childEntry.getKey(), newValue);
  31. }
  32. }
  33. }
  34. }
  35. }

总结

本文仅仅简单介绍了通过ftl模板生成PDF的使用,而使用spire.office.free jar提供了大量能使我们快速便捷地处理PDF加密解密的方法,继续探究冲冲冲!

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号