Skip to content

Редакція

PDF Oxide підтримує двофазний робочий процес редагування відповідно до специфікації PDF: спочатку позначте області для редагування за допомогою анотацій редагування, потім застосуйте редагування для малювання кольорових накладок, що приховують вміст. Цей підхід дає вам крок перевірки перед постійним приховуванням конфіденційної інформації.

Redaction Workflow

Стандартний процес редагування складається з трьох кроків:

  1. Позначення – Додайте анотації редагування для ідентифікації вмісту, що потрібно приховати
  2. Перевірка – Перегляньте позначені області перед застосуванням (опціонально)
  3. Застосування – Намалюйте накладки та видаліть анотації редагування
  4. Збереження – Запишіть відредагований PDF на диск

Step 1: Add Redaction Annotations

Використовуйте API анотацій PdfPage для додавання анотацій редагування, що позначають області для видалення.

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)?;

Step 2: Apply Redactions

Once redaction annotations are in place, apply them to draw colored overlays over the marked regions. This step finds all redaction annotations, renders overlays, and removes the redaction annotations.

Apply on a Single Page

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")?;

Apply on All Pages

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")?;

Checking Redaction Status

Check if a Page is Marked

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));

Cancel Pending Redactions

Якщо ви передумали перед збереженням, зніміть позначку зі сторінки для скасування очікуваного редагування.

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));

Complete Redaction Workflow

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")?;

Full API Reference

DocumentEditor Redaction Methods

Метод Повертає Опис
apply_page_redactions(page) Result<()> Apply redactions on a single page
apply_all_redactions() Result<()> Apply redactions on all pages
is_page_marked_for_redaction(page) bool Перевірити чи сторінка має незавершені затирання
unmark_page_for_redaction(page) () Cancel pending redactions for a page

Python (PdfDocument) Methods

Method Parameters Description
apply_page_redactions(page) page: int Apply redactions on a single page
apply_all_redactions() Apply redactions on all pages
is_page_marked_for_redaction(page) page: int Check redaction status
unmark_page_for_redaction(page) page: int Cancel pending redactions

Important Notes

  • Visual overlay: Redaction draws a colored rectangle over the marked area. The underlying content stream data is visually hidden but may still be present in the file. For complete removal, consider combining redaction with a full rewrite save.
  • Two-phase process: Always add redaction annotations first, then call apply_page_redactions() or apply_all_redactions(). Calling apply without redaction annotations has no effect.
  • Irreversible: Once saved, the visual overlay is permanent. Always work on a copy of the original document.
  • Color: By default, redactions use a black overlay. Use RedactAnnotation with color options for custom overlay colors.

Пов’язані сторінки