PDF Kit支持判断PDF文档是否加密及删除PDF加密锁。
调用isEncrypted方法,判断PDF文档是否加密。
如果是加密PDF文档,调用removeSecurity方法移除PDF文档的加密锁。
import { pdfService } from '@kit.PDFKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
// ...
@Entry
@Component
struct SecurityPage {
private context = this.getUIContext().getHostContext() as Context;
private loadResult: pdfService.ParseResult = pdfService.ParseResult.PARSE_ERROR_FORMAT;
private password: string = '123456';
aboutToAppear(): void {
let filePath = this.context.resourceDir + '/input.pdf';
(async () => {
let doc = new pdfService.PdfDocument();
this.loadResult = await doc.loadDocument(filePath);
doc.releaseDocument();
})()
}
build() {
Stack({ alignContent: Alignment.TopStart }) {
Column({ space: 10 }) {
// ...
Row({ space: 10 }) {
Button('isEncryptedAndRemoveSecurity')
.onClick(() => {
if (this.loadResult === pdfService.ParseResult.PARSE_SUCCESS) {
let inputPath = this.context.resourceDir + '/input.pdf';
let tmpPath = this.context.filesDir + '/tmp.pdf';
let tmp1Path = this.context.filesDir + '/tmp1.pdf';
let doc = new pdfService.PdfDocument();
// 加载原始PDF文档
doc.loadDocument(inputPath);
// 设置密码,对文档进行加密
doc.setPdfPassword(this.password);
// 保存加密后的文档到tmp.pdf
doc.saveDocument(tmpPath);
// 检查tmp.pdf是否加密
let isTmpEncrypted = doc.isEncrypted(tmpPath);
hilog.info(0x0000, 'testTag', 'isTmpEncrypted: %{public}s', isTmpEncrypted.toString());
doc.releaseDocument();
// 加载加密的tmp.pdf
doc.loadDocument(tmpPath, this.password);
// 移除密码
doc.removeSecurity();
// 保存无密码的文档到tmp1.pdf
doc.saveDocument(tmp1Path);
// 检查tmp1.pdf是否已移除密码
let isTmp1Encrypted = doc.isEncrypted(tmp1Path);
hilog.info(0x0000, 'testTag', 'isTmp1Encrypted: %{public}s', isTmp1Encrypted.toString());
}
})
}
}
.alignItems(HorizontalAlign.Start)
.padding(10)
}
.width('100%').height('100%')
}
}