Text Redactor
TextRedactor draws opaque rectangles over sensitive content. It supports both coordinate-based area redaction and text-search-based redaction. Redactions must be explicitly applied before saving.
Note: This is visual redaction — the underlying text bytes remain in the PDF stream data. The redaction rectangles visually obscure content but do not remove it from the file.
Opening a PDF
Section titled “Opening a PDF”use Phpdftk\Pdf\Toolkit\TextRedactor;
// From file$redactor = TextRedactor::open('contract.pdf');
// From string$redactor = TextRedactor::openString($pdfBytes);
// Encrypted PDF$redactor = TextRedactor::open('secured.pdf', password: 'secret');Area redaction
Section titled “Area redaction”Specify a rectangle by page number (1-based), x, y, width, and height in points:
$redactor->redactArea(1, 72, 700, 200, 20); // page 1$redactor->redactArea(2, 100, 500, 150, 14); // page 2Text redaction
Section titled “Text redaction”Literal string
Section titled “Literal string”Search for a literal string and redact all occurrences:
$redactor->redactText('John Smith');Restrict to specific pages:
use Phpdftk\Pdf\Toolkit\PageSelector;
$redactor->redactText('SSN: 123-45-6789', PageSelector::pages(1));Regex pattern
Section titled “Regex pattern”$redactor->redactPattern('/\d{3}-\d{2}-\d{4}/'); // SSN pattern$redactor->redactPattern('/\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b/i'); // emailRedaction color
Section titled “Redaction color”The default color is black. Change it before applying:
$redactor->setRedactionColor(1.0, 1.0, 1.0); // white$redactor->setRedactionColor(0.5, 0.5, 0.5); // grayApplying and saving
Section titled “Applying and saving”Redactions are staged until apply() is called. This two-step process lets you review the redaction count before committing:
$redactor ->redactText('CONFIDENTIAL') ->redactArea(1, 72, 700, 200, 20) ->apply();
echo $redactor->getRedactionCount(); // number of redaction rectangles
$redactor->save('redacted.pdf');Calling save() or toBytes() without first calling apply() will throw a RuntimeException.
Complete example
Section titled “Complete example”TextRedactor::open('contract.pdf') ->redactText('Jane Doe') ->redactPattern('/\d{3}-\d{2}-\d{4}/') ->redactArea(3, 50, 600, 300, 30) ->setRedactionColor(0.0, 0.0, 0.0) ->apply() ->save('redacted.pdf');Document info
Section titled “Document info”$redactor->getPageCount(); // int$redactor->getRedactionCount(); // int (after apply)Escape hatch
Section titled “Escape hatch”$reader = $redactor->getReader(); // PdfReader