邮件的附件是一个加密的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();
}
}
3616

被折叠的 条评论
为什么被折叠?



