涂黑
PDF Oxide 支持遵循 PDF 规范的两阶段涂黑工作流:首先使用涂黑注释标记要涂黑的区域,然后应用涂黑以绘制遮挡内容的彩色覆盖层。这种方式让你在永久遮盖敏感信息之前有一个审查步骤。
涂黑工作流
标准涂黑流程有三个步骤:
- 标记 – 添加涂黑注释以标识要隐藏的内容
- 审查 – 在应用之前检查标记区域(可选)
- 应用 – 绘制覆盖层并移除涂黑注释
- 保存 – 将涂黑后的 PDF 写入磁盘
步骤 1:添加涂黑注释
使用 PdfPage 注释 API 添加涂黑注释,标记要移除的区域。
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("confidential.pdf")
page = doc.page(0)
# Find sensitive text and mark it for redaction
for t in page.find_text_containing("SSN"):
bbox = t.bbox # (x, y, width, height)
page.add_highlight(bbox[0], bbox[1], bbox[2], bbox[3], (0.0, 0.0, 0.0))
doc.save_page(page)
Rust
use pdf_oxide::api::Pdf;
use pdf_oxide::writer::RedactAnnotation;
use pdf_oxide::geometry::Rect;
let mut doc = Pdf::open("confidential.pdf")?;
let mut page = doc.page(0)?;
// Mark a specific region for redaction
let redact = RedactAnnotation::new(
Rect::new(100.0, 700.0, 200.0, 14.0)
);
page.add_annotation(redact);
// Mark all text containing "SSN" for redaction
let sensitive = page.find_text_containing("SSN");
for t in &sensitive {
let redact = RedactAnnotation::new(t.bbox());
page.add_annotation(redact);
}
doc.save_page(page)?;
步骤 2:应用涂黑
涂黑注释就位后,应用它们以在标记区域上绘制彩色覆盖层。此步骤查找所有涂黑注释、渲染覆盖层并移除涂黑注释。
在单个页面上应用
doc = PdfDocument("marked.pdf")
doc.apply_page_redactions(0)
doc.save("redacted.pdf")
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
doc.applyPageRedactions(0);
const output = doc.save();
doc.free();
use pdf_oxide::editor::DocumentEditor;
let mut editor = DocumentEditor::open("marked.pdf")?;
editor.apply_page_redactions(0)?;
editor.save("redacted.pdf")?;
在所有页面上应用
doc = PdfDocument("marked.pdf")
doc.apply_all_redactions()
doc.save("redacted.pdf")
import { WasmPdfDocument } from "pdf-oxide-wasm";
const doc = new WasmPdfDocument(bytes);
doc.applyAllRedactions();
const output = doc.save();
doc.free();
let mut editor = DocumentEditor::open("marked.pdf")?;
editor.apply_all_redactions()?;
editor.save("redacted.pdf")?;
检查涂黑状态
检查页面是否已标记
doc = PdfDocument("input.pdf")
doc.apply_page_redactions(0)
print(doc.is_page_marked_for_redaction(0)) # True (before save)
let mut editor = DocumentEditor::open("input.pdf")?;
editor.apply_page_redactions(0)?;
assert!(editor.is_page_marked_for_redaction(0));
取消待处理的涂黑
如果在保存之前改变主意,可以取消标记一个页面来撤销待处理的涂黑。
doc.unmark_page_for_redaction(0)
print(doc.is_page_marked_for_redaction(0)) # False
editor.unmark_page_for_redaction(0);
assert!(!editor.is_page_marked_for_redaction(0));
完整涂黑工作流
Python
from pdf_oxide import PdfDocument
doc = PdfDocument("sensitive-report.pdf")
# Step 1: Add redaction annotations via the DOM
for i in range(doc.page_count()):
page = doc.page(i)
# Mark SSN patterns
for t in page.find_text_containing("SSN"):
bbox = t.bbox
page.add_highlight(bbox[0], bbox[1], bbox[2], bbox[3], (0.0, 0.0, 0.0))
# Mark email addresses
for t in page.find_text_containing("@"):
bbox = t.bbox
page.add_highlight(bbox[0], bbox[1], bbox[2], bbox[3], (0.0, 0.0, 0.0))
doc.save_page(page)
# Step 2: Apply all redactions
doc.apply_all_redactions()
# Step 3: Save the redacted document
doc.save("report-redacted.pdf")
Rust
use pdf_oxide::api::Pdf;
use pdf_oxide::writer::RedactAnnotation;
let mut doc = Pdf::open("sensitive-report.pdf")?;
let count = doc.page_count()?;
// Step 1: Mark regions for redaction
for i in 0..count {
let mut page = doc.page(i)?;
// Find and mark sensitive text
let ssn_matches = page.find_text_containing("SSN");
for t in &ssn_matches {
let redact = RedactAnnotation::new(t.bbox());
page.add_annotation(redact);
}
let email_matches = page.find_text_containing("@");
for t in &email_matches {
let redact = RedactAnnotation::new(t.bbox());
page.add_annotation(redact);
}
doc.save_page(page)?;
}
// Step 2: Apply redactions
let editor = doc.editor().unwrap();
editor.apply_all_redactions()?;
// Step 3: Save
doc.save("report-redacted.pdf")?;
完整 API 参考
DocumentEditor 涂黑方法
| 方法 | 返回值 | 描述 |
|---|---|---|
apply_page_redactions(page) |
Result<()> |
在单个页面上应用涂黑 |
apply_all_redactions() |
Result<()> |
在所有页面上应用涂黑 |
is_page_marked_for_redaction(page) |
bool |
检查页面是否有待处理的涂黑 |
unmark_page_for_redaction(page) |
() |
取消页面的待处理涂黑 |
Python (PdfDocument) 方法
| 方法 | 参数 | 描述 |
|---|---|---|
apply_page_redactions(page) |
page: int |
在单个页面上应用涂黑 |
apply_all_redactions() |
– | 在所有页面上应用涂黑 |
is_page_marked_for_redaction(page) |
page: int |
检查涂黑状态 |
unmark_page_for_redaction(page) |
page: int |
取消待处理的涂黑 |
重要说明
- 视觉覆盖:涂黑会在标记区域上绘制彩色矩形。底层内容流数据在视觉上被隐藏,但可能仍存在于文件中。如需完全移除,建议将涂黑与完整重写保存结合使用。
- 两阶段流程:始终先添加涂黑注释,然后调用
apply_page_redactions()或apply_all_redactions()。在没有涂黑注释的情况下调用 apply 无效。 - 不可逆:一旦保存,视觉覆盖层就是永久的。始终在原始文档的副本上操作。
- 颜色:默认情况下,涂黑使用黑色覆盖层。使用
RedactAnnotation的颜色选项自定义覆盖层颜色。