Skip to content

密文脱敏

密文脱敏注释(Redact)用于永久性地遮盖或移除 PDF 中的敏感内容。与其他注释不同,密文注释有"标记"和"应用"两个阶段:标记阶段创建脱敏区域并预览效果;应用后将被覆盖区域的内容从文档中永久移除。

注意

调用 apply 后,被脱敏区域的原始内容(文本、图片等)将被永久删除且不可恢复。建议在应用前保存文档备份。

工作流程

  1. 创建脱敏注释 — 在目标区域添加 Redact 注释
  2. 配置外观 — 设置覆盖颜色、叠加文字等
  3. 预览 — 此时注释仅作标记,原始内容仍保留
  4. 应用(Apply) — 永久移除被标记区域的内容,注释本身也被移除

核心方法

方法说明
getQuadPoints / setQuadPoints:获取/设置脱敏区域(用于文本区域的精确标记)
getFillColor / setFillColor:获取/设置标记阶段的填充颜色(预览时显示)
getApplyFillColor / setApplyFillColor:获取/设置应用后的填充颜色
getOverlayText / setOverlayText:获取/设置叠加文字(应用后显示在脱敏区域上)
isOverlayTextRepeated / enableRepeatOverlayText:获取/设置叠加文字是否重复填满区域
getOverlayTextAlignment / setOverlayTextAlignment:获取/设置叠加文字对齐方式
getDefaultAppearance / setDefaultAppearance:获取/设置叠加文字的字体外观(字体、字号、颜色)
enableAutoFontSize自动调整叠加文字字号以适应区域大小
apply应用脱敏 — 永久移除被标记区域的内容

示例:创建并应用密文脱敏

objc
#import <FoxitRDK/FSPDFObjC.h>

FSPDFDoc *doc = [[FSPDFDoc alloc] initWithPath:@"path/to/Sample.pdf"];
[doc load:nil];
FSPDFPage *page = [doc getPage:0];

// 步骤 1:创建脱敏注释
FSRectF *rect = [[FSRectF alloc] initWithLeft1:100 bottom1:700 right1:350 top1:720];
FSAnnot *annot = [page addAnnot:FSAnnotRedact rect:rect];
FSRedact *redact = [[FSRedact alloc] initWithAnnot:annot];

// 步骤 2:设置外观
[redact setFillColor:0xFFFF0000];            // 标记阶段显示红色
[redact setApplyFillColor:0xFF000000];       // 应用后显示黑色

// 设置叠加文字
[redact setOverlayText:@"已脱敏"];
[redact setOverlayTextAlignment:1];           // 居中

FSDefaultAppearance *da = [[FSDefaultAppearance alloc] init];
[da setText_size:12];
[da setText_color:0xFFFFFFFF];                // 白色文字
[redact setDefaultAppearance:da];

[redact resetAppearanceStream];

// 步骤 3:预览效果后应用
// 此时可以保存文档供审阅者预览脱敏标记
[doc saveAs:@"path/to/preview.pdf" saveFlags:FSPDFDocSaveFlagNormal];

// 步骤 4:应用脱敏(永久移除内容)
[redact apply];
[doc saveAs:@"path/to/redacted.pdf" saveFlags:FSPDFDocSaveFlagNormal];

示例:对文本区域进行精确脱敏

对于需要精确标记文本区域的场景,可结合文本搜索和 QuadPoints 使用:

objc
#import <FoxitRDK/FSPDFObjC.h>

FSTextSearch *textSearch = [[FSTextSearch alloc] initWithDocument:doc cancel_callback:nil];
[textSearch setPattern:@"机密信息"];

if ([textSearch findNext]) {
    FSRectFArray *rects = [textSearch getMatchRects];

    FSQuadPointsArray *quadPointsArray = [[FSQuadPointsArray alloc] init];
    for (int i = 0; i < [rects getSize]; i++) {
        FSRectF *r = [rects getAt:i];
        FSQuadPoints *qp = [[FSQuadPoints alloc] init];
        FSPointF *first = [[FSPointF alloc] init];
        [first set:[r getLeft] y:[r getTop]];
        FSPointF *second = [[FSPointF alloc] init];
        [second set:[r getRight] y:[r getTop]];
        FSPointF *third = [[FSPointF alloc] init];
        [third set:[r getLeft] y:[r getBottom]];
        FSPointF *fourth = [[FSPointF alloc] init];
        [fourth set:[r getRight] y:[r getBottom]];
        [qp setFirst:first];
        [qp setSecond:second];
        [qp setThird:third];
        [qp setFourth:fourth];
        [quadPointsArray add:qp];
    }

    // 创建 Redact 注释并设置 QuadPoints
    FSRectF *emptyRect = [[FSRectF alloc] initWithLeft1:0 bottom1:0 right1:0 top1:0];
    FSAnnot *annot = [page addAnnot:FSAnnotRedact rect:emptyRect];
    FSRedact *redact = [[FSRedact alloc] initWithAnnot:annot];
    [redact setQuadPoints:quadPointsArray];
    [redact setApplyFillColor:0xFF000000];
    [redact resetAppearanceStream];

    // 应用脱敏
    [redact apply];
}