Skip to content

Commit 99f85d6

Browse files
1010299: updated code
1 parent 3b1378f commit 99f85d6

1 file changed

Lines changed: 43 additions & 12 deletions

File tree

Document-Processing/PDF/PDF-Library/javascript/Redaction.md

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,11 @@ N> For redaction features, you need to install the `@syncfusion/ej2-pdf-data-ext
1414
## Removing sensitive content from the PDF document
1515

1616
Redaction permanently removes confidential or sensitive information from a PDF. The `PdfRedactor` and `PdfRedactionRegion` classes allow you to mark specific areas and apply irreversible redaction to the document.
17-
> **Note:**
18-
> When calling `redact(callback)`, the callback is executed to render each redaction region on a canvas before the content is permanently removed. This allows customizing the final appearance of the redacted area.
1917

2018
{% tabs %}
2119
{% highlight typescript tabtitle="TypeScript" %}
2220
import { PdfDocument } from '@syncfusion/ej2-pdf';
23-
import { PdfRedactor, PdfRedactionRegion } from '@syncfusion/ej2-pdf-data-extract';
21+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
2422

2523
// Load the document
2624
let document: PdfDocument = new PdfDocument(data);
@@ -30,8 +28,13 @@ let redactor: PdfRedactor = new PdfRedactor(document);
3028
let redactions: PdfRedactionRegion[] = [];
3129
redactions.push(new PdfRedactionRegion(0, {x: 10, y: 10, width: 100, height: 50}));
3230
redactor.add(redactions);
31+
// Define a canvas render callback that returns a canvas element and the application platform.
32+
const canvasRenderCallback = (): {canvas: any, applicationPlatform: ApplicationPlatform} => {
33+
const canvas = document.createElement('canvas');
34+
return { canvas: canvas, applicationPlatform: ApplicationPlatform.typescript };
35+
};
3336
// Apply redactions on the PDF document
34-
redactor.redactSync();
37+
await redactor.redact(callBack: canvasRenderCallback);
3538
// Save the document
3639
document.save('output.pdf');
3740
// Destroy the document
@@ -48,23 +51,30 @@ var redactor = new ej.pdfdataextract.PdfRedactor(document);
4851
var redactions = [];
4952
redactions.push(new PdfRedactionRegion(0, {x: 10, y: 10, width: 100, height: 50}));
5053
redactor.add(redactions);
54+
// Define a canvas render callback that returns a canvas element and the application platform.
55+
const canvasRenderCallback = (): {canvas, applicationPlatform} => {
56+
const canvas = document.createElement('canvas');
57+
return { canvas: canvas, applicationPlatform: ej.pdf.ApplicationPlatform.typescript };
58+
};
5159
// Apply redactions on the PDF document
52-
redactor.redactSync();
60+
await redactor.redact(canvasRenderCallback);
5361
// Save the document
5462
document.save('output.pdf');
5563
// Destroy the document
5664
document.destroy();
5765
{% endhighlight %}
5866
{% endtabs %}
5967

68+
N> The `PdfRedactor.redact(callback)` method allows you to customize redaction appearance through a canvas callback and supports removing both **text and images**. In contrast, `PdfRedactor.redactSync()` performs faster synchronous redaction but can remove **only text**, and does not support image redaction or custom drawing.
69+
6070
## Fill color on the redacted area
6171

6272
You can apply a solid fill color to cover the redacted content. This is the most common approach for redaction.
6373

6474
{% tabs %}
6575
{% highlight typescript tabtitle="TypeScript" %}
6676
import { PdfDocument } from '@syncfusion/ej2-pdf';
67-
import { PdfRedactor, PdfRedactionRegion} from '@syncfusion/ej2-pdf-data-extract';
77+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
6878

6979
// Load an existing PDF document
7080
let document: PdfDocument = new PdfDocument(data);
@@ -79,8 +89,13 @@ redaction.fillColor = {r: 255, g: 0, b: 0};
7989
redactions.push(redaction);
8090
// Add redactions with specified options.
8191
redactor.add(redactions);
92+
// Define a canvas render callback that returns a canvas element and the application platform.
93+
const canvasRenderCallback = (): {canvas: any, applicationPlatform: ApplicationPlatform} => {
94+
const canvas = document.createElement('canvas');
95+
return { canvas: canvas, applicationPlatform: ApplicationPlatform.typescript };
96+
};
8297
// Apply redactions on the PDF document
83-
redactor.redactSync();
98+
await redactor.redact(callBack: canvasRenderCallback);
8499
// Save the document
85100
document.save('output.pdf');
86101
// Destroy the document
@@ -102,8 +117,13 @@ redaction.fillColor = {r: 255, g: 0, b: 0};
102117
redactions.push(redaction);
103118
// Add redactions with specified options.
104119
redactor.add(redactions);
120+
// Define a canvas render callback that returns a canvas element and the application platform.
121+
const canvasRenderCallback = (): {canvas, applicationPlatform} => {
122+
const canvas = document.createElement('canvas');
123+
return { canvas: canvas, applicationPlatform: ej.pdf.ApplicationPlatform.typescript };
124+
};
105125
// Apply redactions on the PDF document
106-
redactor.redactSync();
126+
await redactor.redact(canvasRenderCallback);
107127
// Save the document
108128
document.save('output.pdf');
109129
// Destroy the document
@@ -118,7 +138,7 @@ Customize the redacted region by drawing text or graphics over it, using `PdfRed
118138
{% tabs %}
119139
{% highlight typescript tabtitle="TypeScript" %}
120140
import { PdfDocument } from '@syncfusion/ej2-pdf';
121-
import { PdfRedactor, PdfRedactionRegion } from '@syncfusion/ej2-pdf-data-extract';
141+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
122142

123143
// Load an existing PDF document
124144
let document = new PdfDocument(data);
@@ -141,8 +161,13 @@ redactions.push(redaction);
141161
let redactor = new PdfRedactor(document);
142162
// Add redactions with specified options
143163
redactor.add(redactions);
164+
// Define a canvas render callback that returns a canvas element and the application platform.
165+
const canvasRenderCallback = (): {canvas: any, applicationPlatform: ApplicationPlatform} => {
166+
const canvas = document.createElement('canvas');
167+
return { canvas: canvas, applicationPlatform: ApplicationPlatform.typescript };
168+
};
144169
// Apply redactions on the PDF document
145-
redactor.redactSync();
170+
await redactor.redact(callBack: canvasRenderCallback);
146171
// Save the document
147172
document.save('output.pdf');
148173
// Destroy the document
@@ -171,9 +196,15 @@ redaction = new ej.pdf.PdfRedactionRegion(0, { x: 40, y: 43, width: 80, height:
171196
redactions.push(redaction);
172197
// Initialize redactor
173198
var redactor = new ej.pdf.PdfRedactor(document);
174-
// Add redactions and apply them
199+
// Add redactions with specified options
175200
redactor.add(redactions);
176-
redactor.redactSync();
201+
// Define a canvas render callback that returns a canvas element and the application platform.
202+
const canvasRenderCallback = (): {canvas, applicationPlatform} => {
203+
const canvas = document.createElement('canvas');
204+
return { canvas: canvas, applicationPlatform: ej.pdf.ApplicationPlatform.typescript };
205+
};
206+
// Apply redactions on the PDF document
207+
await redactor.redact(canvasRenderCallback);
177208
// Save and dispose
178209
document.save('output.pdf');
179210
document.destroy();

0 commit comments

Comments
 (0)