如何在Android上创建受密码保护的PDF的可编辑副本



上下文

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

问题

在安卓系统上有什么方法可以创建受密码保护的PDF文件的可编辑副本吗?同样,这些受密码保护的PDF文件只会阻止在PDF上进行任何更改,您实际上不需要密码即可查看PDF内容。

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

好的,所以我终于找到了解决这个问题的方法。我所做的是通过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)

最新更新