PdfUtil.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package jnpf.util;
  2. import com.lowagie.text.pdf.PdfReader;
  3. import lombok.Cleanup;
  4. import lombok.extern.slf4j.Slf4j;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.nio.file.Files;
  9. /**
  10. * PDF工具
  11. * @author JNPF开发平台组
  12. * @version V5.0.0
  13. * @copyright 引迈信息技术有限公司
  14. * @date 2025/1/14 15:00
  15. */
  16. @Slf4j
  17. public class PdfUtil {
  18. /**
  19. * 校验pdf文件是否包含js脚本
  20. **/
  21. public static boolean containsJavaScript(File file) {
  22. try {
  23. return containsJavaScript(Files.readAllBytes(file.toPath()));
  24. } catch (IOException e) {
  25. log.error("PDF文档解析失败: {}", e.getMessage());
  26. }
  27. return false;
  28. }
  29. /**
  30. * 校验pdf文件是否包含js脚本
  31. **/
  32. public static boolean containsJavaScript(byte[] filebyte) {
  33. try {
  34. @Cleanup PdfReader reader = new PdfReader(filebyte);
  35. String javaScript = reader.getJavaScript();
  36. if(StringUtil.isNotEmpty(javaScript)) {
  37. return true;
  38. }
  39. } catch (Exception e) {
  40. log.error("PDF文档解析失败: {}", e.getMessage());
  41. }
  42. return false;
  43. }
  44. /**
  45. * 校验pdf文件是否包含js脚本
  46. **/
  47. public static boolean containsJavaScript(InputStream inputStream) {
  48. try {
  49. @Cleanup PdfReader reader = new PdfReader(inputStream);
  50. String javaScript = reader.getJavaScript();
  51. if(StringUtil.isNotEmpty(javaScript)) {
  52. return true;
  53. }
  54. } catch (Exception e) {
  55. log.error("PDF文档解析失败: {}", e.getMessage());
  56. }
  57. return false;
  58. }
  59. }