对加密的pdf文件进行解密,并获取pdf内的附件内容(为跳转网址以及附件内容另存指定位置)

邮件的附件是一个加密的pdf,需要对其进行密码解密,
解密后将未加密的文件另存新的存放地址,并对处理后的pdf内容进行解析,
pdf的附件是一个url下载地址,获取此下载地址,并跳转网址,
成功下载对应的文件,存放至本地;对pdf的内的附近也进行另存至指定地址

import org.apache.pdfbox.cos.COSDictionary;
import org.apache.pdfbox.pdmodel.PDDocument;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.attachments.PdfAttachmentCollection;
import org.apache.pdfbox.pdmodel.interactive.annotation.PDAnnotation;

import java.io.*;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

    public static void main(String[] args) throws Exception {
        String inputFile ="F:\\123.pdf";
        String outputFile = "F:\\321.pdf";
        String password = "password";
        boolean isEncrypted = false;

        try  {
            PDDocument document = PDDocument.load(new File(inputFile),password);
            isEncrypted = document.isEncrypted();

            if (isEncrypted) {
                System.out.println("There is a password");
                    document.setAllSecurityToBeRemoved(true);
                    document.save(outputFile);
                    System.out.println("PDF  File decrypted and saved successfully !");
                    
				PdfDocument pdf = new PdfDocument();
                pdf.loadFromFile(outputFile);
                PdfAttachmentCollection attachmentCollection = pdf.getAttachments();
                for(int i = 0; i<attachmentCollection.getCount(); i++){

                    String fileName = attachmentCollection.get(i).getFileName();
                    fileName = "F:\\\\"+fileName;
                    File file = new File(fileName);
                    OutputStream outputStream = new FileOutputStream(file);
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
                    bufferedOutputStream.write(attachmentCollection.get(i).getData());
                    bufferedOutputStream.close();
                    System.out.println("PDF File decryption and save attachment successfully!");

                }
                    

                    //todo  Read the information from the file, obtain the URL to download the file
                    
                PDDocument doc = PDDocument.load(new File(outputFile));
                List<PDAnnotation> annotations = doc.getPage(0).getAnnotations();

                for (int i = 0; i < annotations.size(); i++) {
                    PDAnnotation pdAnnotation = annotations.get(i);
                    COSDictionary cosObject = pdAnnotation.getCOSObject();
                    String cosBase = cosObject.getItem("A").toString();
                    String url = null;
                    String pattern = String.format("%s(.+?)%s", Pattern.quote("COSString{"), Pattern.quote("};}"));
                    Matcher matcher = Pattern.compile(pattern).matcher(cosBase);
                    if (matcher.find()) {
                        url = matcher.group(1);
                    }
                Pattern patterns = Pattern.compile("https?:\\/\\/[^\\s]+");
                Matcher matchers = patterns.matcher(url);

                while (matchers.find()) {
                    System.out.println(matchers.group());
                }              
                }                 
            }
             else {
                System.out.println("No password");
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        
        }

依赖包如下:

org.apache.pdfbox
pdfbox
2.0.24


e-iceblue
spire.pdf
10.3.4

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瑾莫

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值