0

语境

我正在使用名为PsPdfKit的第三方库来编辑 PDF 文件,在 PDF 文档的顶部添加注释 - 例如 PNG 图像和文本装饰。但是这个库有一个限制,我不能在受密码保护的 PDF 文件上使用这个注释功能。我可以打开并查看文档,但实际上无法将这些注释放入其中。我现在要做的是弄清楚是否可以创建 PDF 文件的可编辑副本。

问题

在 Android 上是否有任何方法可以创建受密码保护的 PDF 文件的可编辑副本?同样,这些受密码保护的 PDF 文件只会阻止在 PDF 上写入任何更改,您实际上不需要密码即可查看 PDF 内容。

我的想法是创建 PDF 文件的可编辑副本,然后将该副本传递给 PsPdfKit 库。

4

1 回答 1

0

好的,所以我终于找到了解决此问题的方法。我所做的是通过 PsPdfKit 实例化受密码保护的 PDF 文件,然后使用受密码保护文件的页面位图创建文件的可写副本。有点笨拙的解决方案,但它确实允许我使用注释功能。

// We open the password-protected file.
val readOnlyPdfDocument = PdfDocumentLoader.openDocument(applicationContext, readOnlyFile.toUri())
// Use the PsPdfKit API to create a copy of the pages of this document
val task = PdfProcessorTask.newPage(NewPage.fromPage(readOnlyPdfDocument, 0).backgroundColor(Color.RED).build())
for (i in 1 until readOnlyPdfDocument.pageCount) {
   task.addNewPage(NewPage.fromPage(readOnlyPdfDocument, i).backgroundColor(Color.RED).build(), i)
}
// Finally store the writeable PDF document in a new file
val writeableFile = File(applicationContext.cacheDir, "Writeable.pdf")
PdfProcessor.processDocument(task, writeableFile)
于 2022-01-01T22:58:28.443 回答