Skip to content

PDF 安全 (Security)

福昕 PDF SDK 提供了一系列加密和解密功能,以满足不同级别的文档安全保护。用户可以使用常规密码加密和证书驱动加密,或使用自己的安全处理机制来自定义安全实现。

如何使用密码加密 PDF 文件

js
import { FoxitRDKNative } from 'foxit_rdk';

class SecurityUnit {
  public encryPDF(pdfDoc: FoxitRDKNative.pdf.PDFDoc, ownerPassword: string, userPassword: string,
    savePth: string): boolean {

    // 设置加密数据。
    const encryptData = new FoxitRDKNative.pdf.StdEncryptData(true,
      FoxitRDKNative.pdf.PDFDoc.e_PermModify | FoxitRDKNative.pdf.PDFDoc.e_PermAssemble |
      FoxitRDKNative.pdf.PDFDoc.e_PermFillForm,
      FoxitRDKNative.pdf.SecurityHandler.e_CipherAES, 16);

    const securityHandler = new FoxitRDKNative.pdf.StdSecurityHandler();
    try {
      if (!securityHandler.Initialize(encryptData, userPassword, ownerPassword)) {
        return false;
      }
      pdfDoc.SetSecurityHandler(securityHandler);
      if (!pdfDoc.SaveAs(savePth, FoxitRDKNative.pdf.PDFDoc.e_SaveFlagNormal)) {
        return false;
      }
      return true;
    } catch (e) {
      console.error(e);
    }
    return false;
  }
}