【发布时间】:2021-01-26 18:28:43
【问题描述】:
我一直在使用以下代码以编程方式合并加密的 pdf 文件。
public void mergeMyFiles(String filesToBeMerged[], String mergedFileLocation, String password) {
try {
int pageOffset = 0;
ArrayList masterBookMarkList = new ArrayList();
int fileIndex = 0;
String outFile = mergedFileLocation;
Document document = null;
PdfCopy writer = null;
PdfReader reader = null;
PdfReader.unethicalreading = true;
for (fileIndex = 0; fileIndex < filesToBeMerged.length; fileIndex++) {
/**
* Create a reader for the file that we are reading
*/
reader = new PdfReader(filesToBeMerged[fileIndex], password.getBytes());
/**
* Replace all the local named links with the actual destinations.
*/
reader.consolidateNamedDestinations();
/**
* Retrieve the total number of pages for this document
*/
int totalPages = reader.getNumberOfPages();
/**
* Get the list of bookmarks for the current document
* If the bookmarks are not empty, store the bookmarks
* into a master list
*/
List bookmarks = SimpleBookmark.getBookmark(reader);
if (bookmarks != null) {
if (pageOffset != 0)
SimpleBookmark.shiftPageNumbers(bookmarks, pageOffset,
null);
masterBookMarkList.addAll(bookmarks);
System.out.println("Bookmarks found and storing...");
} else {
System.out.println("No bookmarks in this file...");
}
pageOffset += totalPages;
/**
* Merging the files to the first file.
* If we are passing file1, file2 and file3,
* we will merge file2 and file3 to file1.
*/
if (fileIndex == 0) {
/**
* Create the document object from the reader
*/
document = new Document(reader.getPageSizeWithRotation(1));
/**
* Create a pdf write that listens to this document.
* Any changes to this document will be written the file
*
* outFile is a location where the final merged document
* will be written to.
*/
System.out.println("Creating an empty PDF...");
writer = new PdfCopy(document, new FileOutputStream(outFile));
/**
* Open this document
*/
document.open();
}
/**
* Add the conent of the file into this document (writer).
* Loop through multiple Pages
*/
System.out.println("Merging File: " + filesToBeMerged[fileIndex]);
PdfImportedPage page;
for (int currentPage = 1; currentPage <= totalPages; currentPage++) {
page = writer.getImportedPage(reader, currentPage);
writer.addPage(page);
}
/**
* This will get the documents acroform.
* This will return null if no acroform is part of the document.
*
* Acroforms are PDFs that have been turned into fillable forms.
*/
System.out.println("Checking for Acroforms");
PRAcroForm form = reader.getAcroForm();
if (form != null) {
//writer.copyAcroForm(reader);
writer.addDocument(reader);
System.out.println("Acroforms found and copied");
} else
System.out.println("Acroforms not found for this file");
System.out.println();
}
/**
* After looping through all the files, add the master bookmarklist.
* If individual PDF documents had separate bookmarks, master bookmark
* list will contain a combination of all those bookmarks in the
* merged document.
*/
if (!masterBookMarkList.isEmpty()) {
writer.setOutlines(masterBookMarkList);
System.out.println("All bookmarks combined and added");
} else {
System.out.println("No bookmarks to add in the new file");
}
/**
* Finally Close the main document, which will trigger the pdfcopy
* to write back to the filesystem.
*/
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我最近在这行代码尝试创建 pdfReader 时开始收到此错误:
reader = new PdfReader(filesToBeMerged[fileIndex], password.getBytes());
com.itextpdf.text.exceptions.InvalidPdfException:未知加密类型 R = 6 在 com.itextpdf.text.pdf.PdfReader.readPdf(PdfReader.java:738) 在 com.itextpdf.text.pdf.PdfReader.(PdfReader.java:181) 在 com.itextpdf.text.pdf.PdfReader.(PdfReader.java:219) 在 com.itextpdf.text.pdf.PdfReader.(PdfReader.java:207) 在 com.project.mainPageShop.mergeMyFiles(mainPageShop.java:4368) 在 com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11757) 在 com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11628) 在 android.os.AsyncTask.finish(AsyncTask.java:755) 在 android.os.AsyncTask.access$900(AsyncTask.java:192) 在 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772) 在 android.os.Handler.dispatchMessage(Handler.java:107) 在 android.os.Looper.loop(Looper.java:237) 在 android.app.ActivityThread.main(ActivityThread.java:7814) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
请注意密码是正确的,并且用于使用此代码工作的同一文件现在引发此异常。
更新:
我使用 com.itextpdf:itext7-core:7.0.2
使用了以下代码public void mergePDFFiles(String FILE1, String FILE2, String mergedFileLocation, String password) {
try {
PdfReader pdf1 = new PdfReader(FILE1);
pdf1.setUnethicalReading(true);
PdfReader pdf2 = new PdfReader(FILE2);
pdf2.setUnethicalReading(true);
PdfDocument pdfDocument = new PdfDocument(pdf1, new PdfWriter(mergedFileLocation));
PdfDocument pdfDocument2 = new PdfDocument(pdf2);
PdfMerger merger = new PdfMerger(pdfDocument);
merger.merge(pdfDocument2, 1, pdfDocument2.getNumberOfPages());
pdfDocument2.close();
pdfDocument.close();
} catch (IOException e) {
e.printStackTrace();
}
}
这是 logcat 结果:
致命异常:主要 进程:com.project,PID:7665 com.itextpdf.kernel.PdfException:未知加密类型 R == 6。 在 com.itextpdf.kernel.pdf.PdfEncryption.readAndSetCryptoModeForStdHandler(PdfEncryption.java:508) 在 com.itextpdf.kernel.pdf.PdfEncryption.(PdfEncryption.java:181) 在 com.itextpdf.kernel.pdf.PdfReader.readDecryptObj(PdfReader.java:1061) 在 com.itextpdf.kernel.pdf.PdfReader.readPdf(PdfReader.java:531) 在 com.itextpdf.kernel.pdf.PdfDocument.open(PdfDocument.java:1585) 在 com.itextpdf.kernel.pdf.PdfDocument.(PdfDocument.java:281) 在 com.itextpdf.kernel.pdf.PdfDocument.(PdfDocument.java:249) 在 com.project.mainPageShop.mergePDFFiles(mainPageShop.java:4353) 在 com.neelwafurat.iKitabForAndroid.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11788) 在 com.project.mainPageShop$DownloadFileAsync.onPostExecute(mainPageShop.java:11659) 在 android.os.AsyncTask.finish(AsyncTask.java:755) 在 android.os.AsyncTask.access$900(AsyncTask.java:192) 在 android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:772) 在 android.os.Handler.dispatchMessage(Handler.java:107) 在 android.os.Looper.loop(Looper.java:237) 在 android.app.ActivityThread.main(ActivityThread.java:7814) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
错误发生在以下行:
PdfDocument pdfDocument = new PdfDocument(pdf1, new PdfWriter(mergedFileLocation));
在下面找到使用密码的示例加密 pdf 文件的链接:123456
【问题讨论】:
-
encryption type R = 6比 itext 5.x 更新。 -
我已将 iText 更新为 itextpdf-5.5.9.jar 和 distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 是否有更新我可以更新到的版本?
-
正如我所说,据我所知,itext 5.x 不支持 R=6。多年来,只有错误修复和安全修复,没有新功能。
-
我尝试了 iText 7,出现同样的错误,如何处理加密的 pdf 文件?
-
对于 iText 7,请分享一个示例 pdf。我确信这是他们想要在 iText 7 中支持的东西。
标签: java android pdf itext itext7