123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package com.simuwang;
- import cn.hutool.core.collection.ListUtil;
- import cn.hutool.core.date.DateUtil;
- import com.itextpdf.text.Image;
- import com.itextpdf.text.pdf.PdfCopy;
- import com.itextpdf.text.pdf.PdfReader;
- import com.itextpdf.text.pdf.PdfWriter;
- import com.simuwang.base.common.conts.DateConst;
- import com.simuwang.base.pojo.dto.MailboxInfoDTO;
- import com.simuwang.daq.service.EmailParseApiService;
- import com.simuwang.daq.service.EmailParseService;
- import org.apache.pdfbox.cos.COSDictionary;
- import org.apache.pdfbox.cos.COSName;
- import org.apache.pdfbox.pdmodel.*;
- import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
- import org.apache.pdfbox.rendering.ImageType;
- import org.apache.pdfbox.rendering.PDFRenderer;
- import org.jasypt.util.text.BasicTextEncryptor;
- import org.apache.pdfbox.Loader;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import com.itextpdf.text.*;
- import javax.imageio.ImageIO;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.*;
- import java.util.List;
- @SpringBootTest(classes = Application.class)
- public class ApplicationTest {
- @Autowired
- private EmailParseService emailParseService;
- @Autowired
- private EmailParseApiService emailParseApiService;
- @Test
- public void test() {
- MailboxInfoDTO emailInfoDTO = this.buildMailbox("", "");
- //
- // emailInfoDTO.setAccount("jjpj_test");
- // emailInfoDTO.setPassword("shzq#919");
- // emailInfoDTO.setHost("mail.shzq.com");
- // emailInfoDTO.setPort("993");
- // emailInfoDTO.setProtocol("imap");
- Date startDate = DateUtil.parse("2024-10-10 15:00:00", DateConst.YYYY_MM_DD_HH_MM_SS);
- Date endDate = DateUtil.parse("2024-10-10 16:40:00", DateConst.YYYY_MM_DD_HH_MM_SS);
- try {
- emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- @Test
- public void reportTest() {
- MailboxInfoDTO emailInfoDTO = this.buildMailbox("x", "x");
- Date startDate = DateUtil.parse("2024-10-15 15:10:30", DateConst.YYYY_MM_DD_HH_MM_SS);
- Date endDate = DateUtil.parse("2024-10-15 17:50:30", DateConst.YYYY_MM_DD_HH_MM_SS);
- try {
- emailParseService.parseEmail(emailInfoDTO, startDate, endDate);
- } catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
- @Test
- public void testReparseEmail() {
- emailParseApiService.reparseEmail(593);
- }
- @Test
- public void testReparseFile() {
- emailParseApiService.reparseFile(ListUtil.toList(40, 43));
- }
- @Test
- public void testDateFormat() {
- String input = "Smppw@2024";
- BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
- textEncryptor.setPassword("qwertyuiopasdfghjklzxcvbnm1234567890qwertyuiopasdfghjklzxcvbnm12"); // 用您自己的密钥替换"yourSecretKey"
- String encryptedString = textEncryptor.encrypt(input);
- String decrypt = textEncryptor.decrypt(encryptedString);
- System.out.println("加密后的字符串为: " + encryptedString);
- System.out.println("解密后的字符串为: " + decrypt);
- }
- private MailboxInfoDTO buildMailbox(String account, String pwd) {
- MailboxInfoDTO emailInfoDTO = new MailboxInfoDTO();
- emailInfoDTO.setUserId(1);
- emailInfoDTO.setAccount(account);
- emailInfoDTO.setPassword(pwd);
- emailInfoDTO.setHost("imap.exmail.qq.com");
- emailInfoDTO.setPort("993");
- emailInfoDTO.setProtocol("imap");
- return emailInfoDTO;
- }
- @Test
- public void testPdfRemoveWatermark() {
- pdfRemoveWatermark("D:/pdf/东华期货奔牛1号7.29-8.2基金净值表.pdf");
- }
- public void pdfRemoveWatermark(String filePath) {
- try {
- //PDF转图片
- pdfToImage(filePath);
- List<String> fileNames = getFileNames("D:/pdf/pdfToImage");
- List<File> fileList = new ArrayList<>();
- for (String fileName : fileNames) {
- fileList.add(new File("D:/pdf/pdfToImage/"+fileName));
- }
- //去除PDF水印
- removePdfWatermark(fileList);
- //去除水印后的图片转PDF
- List<Integer> fileNamesInteger = new ArrayList<>();
- List<String> allFiles = new ArrayList<>();
- for (String fileName : fileNames) {
- if(fileName.endsWith(".png")){
- String str = fileName.substring(0, fileName.indexOf(".png"));
- fileNamesInteger.add(Integer.valueOf(str));
- }
- }
- Collections.sort(fileNamesInteger);
- for (Integer fileName : fileNamesInteger) {
- String str ="D:/pdf/pdfToImage/"+ fileName+".png";
- allFiles.add(str);
- }
- String fileName = filePath.substring(filePath.lastIndexOf("/")+1);
- //图片转PDF
- imgOfPdf("D:/pdf/imgToPdf/"+fileName, allFiles);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void pdfToImage(String filePath) throws IOException {
- // File file = new File(filePath);
- // PDDocument doc = Loader.loadPDF(file);
- // PDFRenderer renderer = new PDFRenderer(doc);
- // int pageCount = doc.getNumberOfPages();
- // BufferedImage merge = null;
- // int curY = 0;
- // for (int i = 0; i < pageCount; i++) {
- // try {
- // BufferedImage image = renderer.renderImage(i, 2, ImageType.RGB);
- // ImageIO.write(image, "png", new File("D:/pdf/pdfToImage/" + i + ".png"));
- // } catch (Exception e) {
- // e.printStackTrace();
- // }
- // }
- PDDocument doc = Loader.loadPDF(new File(filePath));
- PDPageTree pages = doc.getPages();
- int i = 0;
- for (PDPage page : pages) {
- PDResources resources = page.getResources();
- COSDictionary dic = resources.getCOSObject().getCOSDictionary(COSName.XOBJECT);
- dic.removeItem(COSName.getPDFName("华泰证券 严禁篡改伪造"));
- }
- doc.save(new File("D:/pdf/pdfToImage/无水印.pdf"));
- // The #close() method must be called once the document is no longer needed.
- doc.close();
- }
- /**
- * 得到文件名称
- *
- * @param path 路径
- * @return {@link List}<{@link String}>
- */
- private static List<String> getFileNames(String path) {
- File file = new File(path);
- if (!file.exists()) {
- return null;
- }
- List<String> fileNames = new ArrayList<>();
- return getFileNames(file, fileNames);
- }
- /**
- * 得到文件名称
- *
- * @param file 文件
- * @param fileNames 文件名
- * @return {@link List}<{@link String}>
- */
- private static List<String> getFileNames(File file, List<String> fileNames) {
- File[] files = file.listFiles();
- for (File f : files) {
- if (f.isDirectory()) {
- getFileNames(f, fileNames);
- } else {
- fileNames.add(f.getName());
- }
- }
- return fileNames;
- }
- /**
- * 去除文件列表里图片的水印并替换
- *
- * @Param fileList 文件列表
- */
- public static void removePdfWatermark(List<File> fileList) {
- try {
- for (File file : fileList) {
- if (!file.getName().endsWith("png") && !file.getName().endsWith("jpg")) {
- continue;
- }
- //用ImageIO流读取像素块
- BufferedImage bi = ImageIO.read(file);
- if (bi != null) {
- removeWatermark(bi);
- //生成的图片格式
- String formatName = file.getName().substring(file.getName().lastIndexOf(".") + 1);
- //用ImageIO流生成的处理图替换原图片
- ImageIO.write(bi, formatName, file);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 去除水印
- */
- private static void removeWatermark(BufferedImage bi) {
- Color wColor = new Color(254, 254, 254);
- Color hColor = new Color(197, 196, 191);
- //白底水印
- for (int i = 0; i < bi.getWidth(); i++) {
- for (int j = 0; j < bi.getHeight(); j++) {
- int color = bi.getRGB(i, j);
- Color oriColor = new Color(color);
- int red = oriColor.getRed();
- int greed = oriColor.getGreen();
- int blue = oriColor.getBlue();
- if (red == 254 && greed == 254 && blue == 254) {
- continue;
- }
- if (red > 220 && greed > 180 && blue > 80) {
- bi.setRGB(i, j, wColor.getRGB());
- }
- if (red <= 240 && greed >= 200 && blue >= 150) {
- bi.setRGB(i, j, wColor.getRGB());
- }
- }
- }
- }
- public static void imgOfPdf(String filepath, List<String> imgUrl) {
- try {
- //输出pdf文件路径
- String pdfUrl = filepath;
- //生成pdf
- File file = Pdf(imgUrl, pdfUrl);
- file.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public static File Pdf(List<String> imageUrllist, String mOutputPdfFileName) {
- //new一个pdf文档
- Document doc = new Document(PageSize.A4, 0, 0, 0, 0);
- try {
- //pdf写入
- PdfWriter.getInstance(doc, new FileOutputStream(mOutputPdfFileName));
- //打开文档
- doc.open();
- //循环图片List,将图片加入到pdf中
- for (int i = 0; i < imageUrllist.size(); i++) {
- //在pdf创建一页
- doc.newPage();
- //通过文件路径获取image
- Image png1 = Image.getInstance(imageUrllist.get(i));
- float height = png1.getHeight();
- float width = png1.getWidth();
- int percent = getPercent2(height, width);
- png1.setAlignment(Image.MIDDLE);
- // 表示是原来图像的比例;
- png1.scalePercent(percent + 3);
- doc.add(png1);
- }
- doc.close();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (DocumentException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- //输出流
- File mOutputPdfFile = new File(mOutputPdfFileName);
- if (!mOutputPdfFile.exists()) {
- mOutputPdfFile.deleteOnExit();
- return null;
- }
- //反回文件输出流
- return mOutputPdfFile;
- }
- public static int getPercent2(float h, float w) {
- int p = 0;
- float p2 = 0.0f;
- p2 = 450 / w * 100;
- p = Math.round(p2);
- return p;
- }
- }
|