Skip to content

Commit e3844c2

Browse files
authored
Merge pull request #2375 from syncfusion-content/1010299-JS-Update
1010299: JavaScript PDF Library Features adding on UG documentations
2 parents 0bf6211 + 07da6cf commit e3844c2

2 files changed

Lines changed: 109 additions & 10 deletions

File tree

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,69 @@ ldocument.destroy();
521521
{% endhighlight %}
522522
{% endtabs %}
523523

524+
## Adding a timestamp in digital signature
525+
526+
This example shows how to apply a digital signature with a trusted timestamp, ensuring the signature remains valid even after the certificate expires. A timestamp callback contacts a Time Stamping Authority (TSA) to add an official time record to the signature. This provides long‑term proof of when the document was signed.
527+
528+
{% tabs %}
529+
{% highlight typescript tabtitle="TypeScript" %}
530+
531+
import { PdfDocument, PdfPage, PdfForm, PdfSignatureField, PdfSignature, CryptographicStandard, DigestAlgorithm } from '@syncfusion/ej2-pdf';
532+
533+
// Load the document
534+
let document: PdfDocument = new PdfDocument(data);
535+
// Gets the first page of the document
536+
let page: PdfPage = document.getPage(0);
537+
// Access the PDF form
538+
let form: PdfForm = document.form;
539+
// Create a new signature field
540+
let field: PdfSignatureField = new PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
541+
// Create a timestamp callback
542+
async function timestampCallback(request: Uint8Array): Promise<{ response: Uint8Array }> {
543+
// Implement timestamp response logic here
544+
return { response: new Uint8Array() }; // Placeholder return
545+
}
546+
// Create a new signature using PFX data, private key and call back function for timestamp
547+
const signature: PdfSignature = PdfSignature.create(certData, password, { cryptographicStandard: CryptographicStandard.cms, digestAlgorithm: DigestAlgorithm.sha256 }, timestampCallback);
548+
// Sets the signature to the field
549+
field.setSignature(signature);
550+
// Add the field into PDF form
551+
form.add(field);
552+
// Save the document
553+
await document.saveAsync('output.pdf');
554+
// Destroy the document
555+
document.destroy();
556+
557+
{% endhighlight %}
558+
{% highlight javascript tabtitle="JavaScript" %}
559+
560+
// Load the document
561+
var document = new ej.pdf.PdfDocument(data);
562+
// Gets the first page of the document
563+
var page = document.getPage(0);
564+
// Access the PDF form
565+
var form = document.form;
566+
// Create a new signature field
567+
var field = new ej.pdf.PdfSignatureField(page, 'Signature', {x: 10, y: 10, width: 100, height: 50});
568+
// Create a timestamp callback
569+
async function timestampCallback(request) {
570+
// Implement timestamp response logic here
571+
return { response: new Uint8Array() }; // Placeholder return
572+
}
573+
// Create a new signature using PFX data, private key and call back function for timestamp
574+
const signature = PdfSignature.create(certData, password, { cryptographicStandard: ej.pdf.CryptographicStandard.cms, digestAlgorithm: ej.pdf.DigestAlgorithm.sha256 }, timestampCallback);
575+
// Sets the signature to the field
576+
field.setSignature(signature);
577+
// Add the field into PDF form
578+
form.add(field);
579+
// Save the document
580+
await document.saveAsync('output.pdf');
581+
// Destroy the document
582+
document.destroy();
583+
584+
{% endhighlight %}
585+
{% endtabs %}
586+
524587
## Drawing text/image in the Signature Appearance
525588

526589
This example demonstrates how to create a visible signature field, apply a CMS (SHA-256) digital signature with signer information, customize the signature appearance using a base64-encoded image, and save the signed PDF document.

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

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Redaction permanently removes confidential or sensitive information from a PDF.
1818
{% tabs %}
1919
{% highlight typescript tabtitle="TypeScript" %}
2020
import { PdfDocument } from '@syncfusion/ej2-pdf';
21-
import { PdfRedactor, PdfRedactionRegion } from '@syncfusion/ej2-pdf-data-extract';
21+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
2222

2323
// Load the document
2424
let document: PdfDocument = new PdfDocument(data);
@@ -28,15 +28,21 @@ let redactor: PdfRedactor = new PdfRedactor(document);
2828
let redactions: PdfRedactionRegion[] = [];
2929
redactions.push(new PdfRedactionRegion(0, {x: 10, y: 10, width: 100, height: 50}));
3030
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+
};
3136
// Apply redactions on the PDF document
32-
redactor.redact();
37+
await redactor.redact(callBack: canvasRenderCallback);
3338
// Save the document
3439
document.save('output.pdf');
3540
// Destroy the document
3641
document.destroy();
3742

3843
{% endhighlight %}
3944
{% highlight javascript tabtitle="JavaScript" %}
45+
4046
// Load the document
4147
var document = new ej.pdf.PdfDocument(data);
4248
// Create a new text extractor
@@ -45,23 +51,30 @@ var redactor = new ej.pdfdataextract.PdfRedactor(document);
4551
var redactions = [];
4652
redactions.push(new PdfRedactionRegion(0, {x: 10, y: 10, width: 100, height: 50}));
4753
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+
};
4859
// Apply redactions on the PDF document
49-
redactor.redact();
60+
await redactor.redact(canvasRenderCallback);
5061
// Save the document
5162
document.save('output.pdf');
5263
// Destroy the document
5364
document.destroy();
5465
{% endhighlight %}
5566
{% endtabs %}
5667

68+
N> Use PdfRedactor.redact(callback) when you need to redact images along with other PDF content. In contrast, PdfRedactor.redactSync() is faster because it runs synchronously, but it cannot redact images—only text and other non‑image elements.
69+
5770
## Fill color on the redacted area
5871

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

6174
{% tabs %}
6275
{% highlight typescript tabtitle="TypeScript" %}
6376
import { PdfDocument } from '@syncfusion/ej2-pdf';
64-
import { PdfRedactor, PdfRedactionRegion} from '@syncfusion/ej2-pdf-data-extract';
77+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
6578

6679
// Load an existing PDF document
6780
let document: PdfDocument = new PdfDocument(data);
@@ -76,14 +89,21 @@ redaction.fillColor = {r: 255, g: 0, b: 0};
7689
redactions.push(redaction);
7790
// Add redactions with specified options.
7891
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+
};
7997
// Apply redactions on the PDF document
80-
redactor.redact();
98+
await redactor.redact(callBack: canvasRenderCallback);
8199
// Save the document
82100
document.save('output.pdf');
83101
// Destroy the document
84102
document.destroy();
103+
85104
{% endhighlight %}
86105
{% highlight javascript tabtitle="JavaScript" %}
106+
87107
// Load an existing PDF document
88108
var document = new ej.pdf.PdfDocument(data);
89109
// Add redactions to the collection
@@ -97,8 +117,13 @@ redaction.fillColor = {r: 255, g: 0, b: 0};
97117
redactions.push(redaction);
98118
// Add redactions with specified options.
99119
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+
};
100125
// Apply redactions on the PDF document
101-
redactor.redact();
126+
await redactor.redact(canvasRenderCallback);
102127
// Save the document
103128
document.save('output.pdf');
104129
// Destroy the document
@@ -113,7 +138,7 @@ Customize the redacted region by drawing text or graphics over it, using `PdfRed
113138
{% tabs %}
114139
{% highlight typescript tabtitle="TypeScript" %}
115140
import { PdfDocument } from '@syncfusion/ej2-pdf';
116-
import { PdfRedactor, PdfRedactionRegion } from '@syncfusion/ej2-pdf-data-extract';
141+
import { PdfRedactor, PdfRedactionRegion, ApplicationPlatform } from '@syncfusion/ej2-pdf-data-extract';
117142

118143
// Load an existing PDF document
119144
let document = new PdfDocument(data);
@@ -136,8 +161,13 @@ redactions.push(redaction);
136161
let redactor = new PdfRedactor(document);
137162
// Add redactions with specified options
138163
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+
};
139169
// Apply redactions on the PDF document
140-
redactor.redact();
170+
await redactor.redact(callBack: canvasRenderCallback);
141171
// Save the document
142172
document.save('output.pdf');
143173
// Destroy the document
@@ -166,9 +196,15 @@ redaction = new ej.pdf.PdfRedactionRegion(0, { x: 40, y: 43, width: 80, height:
166196
redactions.push(redaction);
167197
// Initialize redactor
168198
var redactor = new ej.pdf.PdfRedactor(document);
169-
// Add redactions and apply them
199+
// Add redactions with specified options
170200
redactor.add(redactions);
171-
redactor.redact();
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);
172208
// Save and dispose
173209
document.save('output.pdf');
174210
document.destroy();

0 commit comments

Comments
 (0)