PDF Generation Template Editor
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
Open-source visual PDF generation engine with customizable templates and developer-friendly APIs.
The examples use a reader whose document has finished loading. Applying a redaction permanently removes content in the marked area and cannot be undone. Keep a copy of the source file first.
import { CPDFReaderView, CPDFRedactAnnotation, CPDFAlignment }
from '@compdfkit_pdf_sdk/react_native';
async function addRedaction(reader: CPDFReaderView) {
await reader._pdfDocument.addAnnotations([
new CPDFRedactAnnotation({
page: 0,
rect: { left: 60, top: 520, right: 320, bottom: 470 },
overlayText: 'REDACTED', fillColor: '#000000',
outlineColor: '#FF0000', alignment: CPDFAlignment.CENTER,
textAttribute: { color: '#FFFFFF', familyName: 'Helvetica',
styleName: '', fontSize: 14 },
}),
]);
}page starts at 0. rect uses PDF page coordinates, with the origin at the bottom-left and top greater than bottom. outlineColor sets the border before application. fillColor, overlayText, textAttribute, and alignment control the applied appearance.
The original content still exists at this stage. Retrieve marks with pageAtIndex().getAnnotations(), then call updateAnnotation() or removeAnnotation() to change or delete them.
import { CPDFReaderView, CPDFRedactAnnotation }
from '@compdfkit_pdf_sdk/react_native';
async function applyFirstRedaction(reader: CPDFReaderView) {
const document = reader._pdfDocument;
const annotations = await document.pageAtIndex(0).getAnnotations();
const redaction = annotations.find(
(item): item is CPDFRedactAnnotation => item instanceof CPDFRedactAnnotation,
);
if (!redaction) return;
if (!await document.applyRedaction(redaction)) {
throw new Error('No redaction was applied');
}
if (!await reader.save()) throw new Error('Failed to save the document');
}The annotation must come from the current document and contain a valid page and native uuid. Do not pass a TypeScript object that has not been added to the document directly to the apply method.
import { CPDFReaderView } from '@compdfkit_pdf_sdk/react_native';
async function applyAllRedactions(reader: CPDFReaderView) {
const applied = await reader._pdfDocument.applyRedactions();
if (!applied) return false;
if (!await reader.save()) throw new Error('Failed to save the document');
return true;
}Both Android and iOS provide the apply methods. applyRedactions() returns true when at least one mark was applied, or false when there were no applicable marks. Distinguish an unapplied result from success, and catch rejected Promises.