ApplicationTest.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package com.simuwang;
  2. import cn.hutool.core.collection.ListUtil;
  3. import cn.hutool.core.date.DateUtil;
  4. import com.itextpdf.text.Image;
  5. import com.itextpdf.text.pdf.PdfCopy;
  6. import com.itextpdf.text.pdf.PdfReader;
  7. import com.itextpdf.text.pdf.PdfWriter;
  8. import com.simuwang.base.common.conts.DateConst;
  9. import com.simuwang.base.pojo.dto.MailboxInfoDTO;
  10. import com.simuwang.daq.service.EmailParseApiService;
  11. import com.simuwang.daq.service.EmailParseService;
  12. import org.apache.pdfbox.cos.COSDictionary;
  13. import org.apache.pdfbox.cos.COSName;
  14. import org.apache.pdfbox.pdmodel.*;
  15. import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
  16. import org.apache.pdfbox.rendering.ImageType;
  17. import org.apache.pdfbox.rendering.PDFRenderer;
  18. import org.jasypt.util.text.BasicTextEncryptor;
  19. import org.apache.pdfbox.Loader;
  20. import org.junit.jupiter.api.Test;
  21. import org.springframework.beans.factory.annotation.Autowired;
  22. import org.springframework.boot.test.context.SpringBootTest;
  23. import com.itextpdf.text.*;
  24. import javax.imageio.ImageIO;
  25. import java.awt.*;
  26. import java.awt.image.BufferedImage;
  27. import java.io.File;
  28. import java.io.FileNotFoundException;
  29. import java.io.FileOutputStream;
  30. import java.io.IOException;
  31. import java.util.*;
  32. import java.util.List;
  33. @SpringBootTest(classes = Application.class)
  34. public class ApplicationTest {
  35. @Autowired
  36. private EmailParseService emailParseService;
  37. @Autowired
  38. private EmailParseApiService emailParseApiService;
  39. @Test
  40. public void test() {
  41. MailboxInfoDTO emailInfoDTO = this.buildMailbox("", "");
  42. //
  43. // emailInfoDTO.setAccount("jjpj_test");
  44. // emailInfoDTO.setPassword("shzq#919");
  45. // emailInfoDTO.setHost("mail.shzq.com");
  46. // emailInfoDTO.setPort("993");
  47. // emailInfoDTO.setProtocol("imap");
  48. Date startDate = DateUtil.parse("2024-10-10 15:00:00", DateConst.YYYY_MM_DD_HH_MM_SS);
  49. Date endDate = DateUtil.parse("2024-10-10 16:40:00", DateConst.YYYY_MM_DD_HH_MM_SS);
  50. try {
  51. emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
  52. } catch (Exception e) {
  53. throw new RuntimeException(e);
  54. }
  55. }
  56. @Test
  57. public void reportTest() {
  58. MailboxInfoDTO emailInfoDTO = this.buildMailbox("x", "x");
  59. Date startDate = DateUtil.parse("2024-10-15 15:10:30", DateConst.YYYY_MM_DD_HH_MM_SS);
  60. Date endDate = DateUtil.parse("2024-10-15 17:50:30", DateConst.YYYY_MM_DD_HH_MM_SS);
  61. try {
  62. emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
  63. } catch (Exception e) {
  64. throw new RuntimeException(e);
  65. }
  66. }
  67. @Test
  68. public void testReparseEmail() {
  69. emailParseApiService.reparseEmail(593);
  70. }
  71. @Test
  72. public void testReparseFile() {
  73. emailParseApiService.reparseFile(ListUtil.toList(40, 43));
  74. }
  75. @Test
  76. public void testDateFormat() {
  77. String input = "Smppw@2024";
  78. BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
  79. textEncryptor.setPassword("qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm12"); // 用您自己的密钥替换"yourSecretKey"
  80. String encryptedString = textEncryptor.encrypt(input);
  81. String decrypt = textEncryptor.decrypt(encryptedString);
  82. System.out.println("加密后的字符串为: " + encryptedString);
  83. System.out.println("解密后的字符串为: " + decrypt);
  84. }
  85. private MailboxInfoDTO buildMailbox(String account, String pwd) {
  86. MailboxInfoDTO emailInfoDTO = new MailboxInfoDTO();
  87. emailInfoDTO.setUserId(1);
  88. emailInfoDTO.setAccount(account);
  89. emailInfoDTO.setPassword(pwd);
  90. emailInfoDTO.setHost("imap.exmail.qq.com");
  91. emailInfoDTO.setPort("993");
  92. emailInfoDTO.setProtocol("imap");
  93. return emailInfoDTO;
  94. }
  95. @Test
  96. public void testPdfRemoveWatermark() {
  97. pdfRemoveWatermark("D:/pdf/东华期货奔牛1号7.29-8.2基金净值表.pdf");
  98. }
  99. public void pdfRemoveWatermark(String filePath) {
  100. try {
  101. //PDF转图片
  102. pdfToImage(filePath);
  103. List<String> fileNames = getFileNames("D:/pdf/pdfToImage");
  104. List<File> fileList = new ArrayList<>();
  105. for (String fileName : fileNames) {
  106. fileList.add(new File("D:/pdf/pdfToImage/"+fileName));
  107. }
  108. //去除PDF水印
  109. removePdfWatermark(fileList);
  110. //去除水印后的图片转PDF
  111. List<Integer> fileNamesInteger = new ArrayList<>();
  112. List<String> allFiles = new ArrayList<>();
  113. for (String fileName : fileNames) {
  114. if(fileName.endsWith(".png")){
  115. String str = fileName.substring(0, fileName.indexOf(".png"));
  116. fileNamesInteger.add(Integer.valueOf(str));
  117. }
  118. }
  119. Collections.sort(fileNamesInteger);
  120. for (Integer fileName : fileNamesInteger) {
  121. String str ="D:/pdf/pdfToImage/"+ fileName+".png";
  122. allFiles.add(str);
  123. }
  124. String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
  125. //图片转PDF
  126. imgOfPdf("D:/pdf/imgToPdf/"+fileName, allFiles);
  127. } catch (IOException e) {
  128. e.printStackTrace();
  129. }
  130. }
  131. private void pdfToImage(String filePath) throws IOException {
  132. // File file = new File(filePath);
  133. // PDDocument doc = Loader.loadPDF(file);
  134. // PDFRenderer renderer = new PDFRenderer(doc);
  135. // int pageCount = doc.getNumberOfPages();
  136. // BufferedImage merge = null;
  137. // int curY = 0;
  138. // for (int i = 0; i < pageCount; i++) {
  139. // try {
  140. // BufferedImage image = renderer.renderImage(i, 2, ImageType.RGB);
  141. // ImageIO.write(image, "png", new File("D:/pdf/pdfToImage/" + i + ".png"));
  142. // } catch (Exception e) {
  143. // e.printStackTrace();
  144. // }
  145. // }
  146. PDDocument doc = Loader.loadPDF(new File(filePath));
  147. PDPageTree pages = doc.getPages();
  148. int i = 0;
  149. for (PDPage page : pages) {
  150. PDResources resources = page.getResources();
  151. COSDictionary dic = resources.getCOSObject().getCOSDictionary(COSName.XOBJECT);
  152. dic.removeItem(COSName.getPDFName("华泰证券 严禁篡改伪造"));
  153. }
  154. doc.save(new File("D:/pdf/pdfToImage/无水印.pdf"));
  155. // The #close() method must be called once the document is no longer needed.
  156. doc.close();
  157. }
  158. /**
  159. * 得到文件名称
  160. *
  161. * @param path 路径
  162. * @return {@link List}<{@link String}>
  163. */
  164. private static List<String> getFileNames(String path) {
  165. File file = new File(path);
  166. if (!file.exists()) {
  167. return null;
  168. }
  169. List<String> fileNames = new ArrayList<>();
  170. return getFileNames(file, fileNames);
  171. }
  172. /**
  173. * 得到文件名称
  174. *
  175. * @param file 文件
  176. * @param fileNames 文件名
  177. * @return {@link List}<{@link String}>
  178. */
  179. private static List<String> getFileNames(File file, List<String> fileNames) {
  180. File[] files = file.listFiles();
  181. for (File f : files) {
  182. if (f.isDirectory()) {
  183. getFileNames(f, fileNames);
  184. } else {
  185. fileNames.add(f.getName());
  186. }
  187. }
  188. return fileNames;
  189. }
  190. /**
  191. * 去除文件列表里图片的水印并替换
  192. *
  193. * @Param fileList 文件列表
  194. */
  195. public static void removePdfWatermark(List<File> fileList) {
  196. try {
  197. for (File file : fileList) {
  198. if (!file.getName().endsWith("png") && !file.getName().endsWith("jpg")) {
  199. continue;
  200. }
  201. //用ImageIO流读取像素块
  202. BufferedImage bi = ImageIO.read(file);
  203. if (bi != null) {
  204. removeWatermark(bi);
  205. //生成的图片格式
  206. String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);
  207. //用ImageIO流生成的处理图替换原图片
  208. ImageIO.write(bi, formatName, file);
  209. }
  210. }
  211. } catch (IOException e) {
  212. e.printStackTrace();
  213. }
  214. }
  215. /**
  216. * 去除水印
  217. */
  218. private static void removeWatermark(BufferedImage bi) {
  219. Color wColor = new Color(254, 254, 254);
  220. Color hColor = new Color(197, 196, 191);
  221. //白底水印
  222. for (int i = 0; i < bi.getWidth(); i++) {
  223. for (int j = 0; j < bi.getHeight(); j++) {
  224. int color = bi.getRGB(i, j);
  225. Color oriColor = new Color(color);
  226. int red = oriColor.getRed();
  227. int greed = oriColor.getGreen();
  228. int blue = oriColor.getBlue();
  229. if (red == 254 && greed == 254 && blue == 254) {
  230. continue;
  231. }
  232. if (red > 220 && greed > 180 && blue > 80) {
  233. bi.setRGB(i, j, wColor.getRGB());
  234. }
  235. if (red <= 240 && greed >= 200 && blue >= 150) {
  236. bi.setRGB(i, j, wColor.getRGB());
  237. }
  238. }
  239. }
  240. }
  241. public static void imgOfPdf(String filepath, List<String> imgUrl) {
  242. try {
  243. //输出pdf文件路径
  244. String pdfUrl = filepath;
  245. //生成pdf
  246. File file = Pdf(imgUrl, pdfUrl);
  247. file.createNewFile();
  248. } catch (IOException e) {
  249. e.printStackTrace();
  250. }
  251. }
  252. public static File Pdf(List<String> imageUrllist, String mOutputPdfFileName) {
  253. //new一个pdf文档
  254. Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
  255. try {
  256. //pdf写入
  257. PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
  258. //打开文档
  259. doc.open();
  260. //循环图片List,将图片加入到pdf中
  261. for (int i = 0; i < imageUrllist.size(); i++) {
  262. //在pdf创建一页
  263. doc.newPage();
  264. //通过文件路径获取image
  265. Image png1 = Image.getInstance(imageUrllist.get(i));
  266. float height = png1.getHeight();
  267. float width = png1.getWidth();
  268. int percent = getPercent2(height, width);
  269. png1.setAlignment(Image.MIDDLE);
  270. // 表示是原来图像的比例;
  271. png1.scalePercent(percent + 3);
  272. doc.add(png1);
  273. }
  274. doc.close();
  275. } catch (FileNotFoundException e) {
  276. e.printStackTrace();
  277. } catch (DocumentException e) {
  278. e.printStackTrace();
  279. } catch (IOException e) {
  280. e.printStackTrace();
  281. }
  282. //输出流
  283. File mOutputPdfFile = new File(mOutputPdfFileName);
  284. if (!mOutputPdfFile.exists()) {
  285. mOutputPdfFile.deleteOnExit();
  286. return null;
  287. }
  288. //反回文件输出流
  289. return mOutputPdfFile;
  290. }
  291. public static int getPercent2(float h, float w) {
  292. int p = 0;
  293. float p2 = 0.0f;
  294. p2 = 450 / w * 100;
  295. p = Math.round(p2);
  296. return p;
  297. }
  298. }