Skip to content

Redacao

O PDF Oxide suporta um fluxo de trabalho de redação em duas fases seguindo a especificação PDF: primeiro, marcar regioes para redacao usando anotações de redacao, depois aplicar as redacoes para desenhar sobreposicoes coloridas que ocultam o conteúdo. Esta abordagem oferece uma etapa de revisao antes de ocultar permanentemente informacoes sensiveis.

Fluxo de Trabalho de Redação

O processo padrão de redação tem três etapas:

  1. Marcar – Adicionar anotações de redação para identificar conteúdo a ocultar
  2. Revisar – Inspecionar regiões marcadas antes de aplicar (opcional)
  3. Apply – Draw overlays and remove redaction annotations
  4. Save – Write the redacted PDF to disk

Step 1: Add Redaction Annotations

Use the PdfPage annotation API to add redaction annotations that mark regions for removal.

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

Se você mudar de ideia antes de salvar, desmarque uma página para cancelar a redação pendente.

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: Aplicar todas as redações
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")?;

Referência Completa da API

DocumentEditor Redaction Methods

Method Retorna Descrição
apply_page_redactions(page) Result<()> Apply redactions on a single página
apply_all_redactions() Result<()> Apply redactions on todas as páginas
is_page_marked_for_redaction(page) bool Check if página has pending redactions
unmark_page_for_redaction(page) () Cancel pending redactions for a página

Python (PdfDocument) Methods

Método Parâmetros Descrição
apply_page_redactions(page) page: int Apply redactions on a single página
apply_all_redactions() Apply redactions on todas as páginas
is_page_marked_for_redaction(page) page: int Check redaction status
unmark_page_for_redaction(page) page: int Cancel pending redactions

Observacoes Importantes

  • 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. Para 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.

Páginas Relacionadas