From b01a7a8968dd05846bfac7da89d85f925dd0e84f Mon Sep 17 00:00:00 2001 From: Srihariharan Date: Wed, 26 Jun 2024 11:58:16 +0530 Subject: [PATCH] 893607: UG documentation for LTV and Timestamp - Flutter PDF. --- Flutter/pdf/working-with-digital-signature.md | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) diff --git a/Flutter/pdf/working-with-digital-signature.md b/Flutter/pdf/working-with-digital-signature.md index 89c4fe276..cd082a2ed 100644 --- a/Flutter/pdf/working-with-digital-signature.md +++ b/Flutter/pdf/working-with-digital-signature.md @@ -255,3 +255,173 @@ File('Output.pdf').writeAsBytes(await document.save()); document.dispose(); {% endhighlight %} + +## Long Term Validation (LTV) PDF signature + +The Syncfusion Flutter PDF supports creating long term signature validation for the signed PDF document. The LTV signature allows you to check the validity of a signature long after the document has been signed. To achieve long term validation, all the required elements for signature validation must be embedded in the signed PDF. + +N> The resulting PDF document size will be substantial because all the necessary signature information, Certificate Revocation List (CRL), and Online Certificate Status Protocol (OCSP) are embedded. + +The following code example shows how to enable LTV for a signed PDF document using createLongTermValidity method in [PdfSignature](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfSignature-class.html) class. + +{% highlight dart %} + + // Load the existing PDF document. + PdfDocument document = + PdfDocument(inputBytes: File('input.pdf').readAsBytesSync()); + // Load the existing signature field. + PdfSignatureField field = document.form.fields[0] as PdfSignatureField; + // Create LTV for loaded signed signature. + bool isLTVAdded = await field.signature!.createLongTermValidity(); + // Save the document. + File('output.pdf').writeAsBytesSync(await document.save()); + // Dispose the document. + document.dispose(); + +{% endhighlight %} + +## Create Long Term Validation (LTV) with public certificates data + +The following code example shows how to create an LTV for a signed PDF document using public certificates with the createLongTermValidity method in [PdfSignature](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfSignature-class.html) class. + +{% highlight dart %} + + // Load the existing PDF document. + PdfDocument document = + PdfDocument(inputBytes: File('input.pdf').readAsBytesSync()); + // Load the existing signature field. + PdfSignatureField field = document.form.fields[0] as PdfSignatureField; + // Load the certificate from the PFX file. + PdfCertificate certificate = + PdfCertificate(File('PDF.pfx').readAsBytesSync(), 'syncfusion'); + // Get the public certificates data. + List>? publicCertificatesData = certificate.getCertificateChain(); + // Create LTV with your public certificates. + await field.signature!.createLongTermValidity( + publicCertificatesData: publicCertificatesData, + includePublicCertificates: true); + // Save the document. + File('output.pdf').writeAsBytesSync(await document.save()); + // Dispose the document. + document.dispose(); + +{% endhighlight %} + +## Adding a timestamp in digital signature + +The Syncfusion Flutter PDF allows you to add timestamp in the digital signature of the PDF document using timestampServer property in [PdfSignature](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfSignature-class.html) class. The following code example explains the same. + +N> Signing using TimestampServer only works when the document is saved using asynchronous [save](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/save.html). It is not supported in synchronous [saveSync](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/saveSync.html). + +{% highlight dart %} + + // Create a new PDF document. + PdfDocument document = PdfDocument(); + // Add a new page to the document. + PdfPage page = document.pages.add(); + // Create a new timestamp server. + TimestampServer server = TimestampServer( + Uri.parse('http://syncfusion.digistamp.com'), + userName: 'user', + password: '123456', + timeOut: const Duration(milliseconds: 5000)); + // Check whether the timestamp server is valid. + bool isValid = await server.isValid; + if (isValid) { + // Add a new signature field to the page. + PdfSignatureField field = PdfSignatureField(page, 'signature', + bounds: const Rect.fromLTWH(0, 0, 200, 100), + signature: PdfSignature( + certificate: + PdfCertificate(File('PDF.pfx').readAsBytesSync(), 'syncfusion'), + contactInfo: 'johndoe@owned.us', + locationInfo: 'Honolulu, Hawaii', + reason: 'I am author of this document.', + )); + // Add the timestamp server to the signature. + field.signature!.timestampServer = server; + // Get the graphics of the signature field. + final PdfGraphics? graphics = field.appearance.normal.graphics; + // Draw an image to the signature field. + graphics!.drawImage(PdfBitmap(File('picture.png').readAsBytesSync()), + Rect.fromLTWH(0, 0, field.bounds.width, field.bounds.height)); + // Add the signature field to the form fields collection. + document.form.fields.add(field); + } + // Save the document. + File('output.pdf').writeAsBytesSync(await document.save()); + // Dispose the document. + document.dispose(); + +{% endhighlight %} + +## Adding a timestamp in the PDF document + +You can add timestamp to the PDF document using timestampServer property in [PdfSignature](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfSignature-class.html) class. The following code example explains the same. + +N> Signing using TimestampServer only works when the document is saved using asynchronous [save](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/save.html). It is not supported in synchronous [saveSync](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/saveSync.html). + + +{% highlight dart %} + + // Create a new PDF document. + PdfDocument document = PdfDocument(); + // Add a new page to the document. + PdfPage page = document.pages.add(); + // Create a new timestamp server. + TimestampServer server = TimestampServer( + Uri.parse('http://syncfusion.digistamp.com'), + userName: 'user', + password: '123456', + timeOut: const Duration(milliseconds: 5000)); + // Check whether the timestamp server is valid. + bool isValid = await server.isValid; + if (isValid) { + // Add a new signature field to the page. + PdfSignatureField field = + PdfSignatureField(page, 'signature', signature: PdfSignature()); + // Add the timestamp server to the signature. + field.signature!.timestampServer = server; + // Add the signature field to the form fields collection. + document.form.fields.add(field); + } + // Save the document. + File('output.pdf').writeAsBytesSync(await document.save()); + // Dispose the document. + document.dispose(); + +{% endhighlight %} + +## Adding a timestamp in an existing PDF document + +You can add timestamp to the existing PDF document using timestampServer property in [PdfSignature](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfSignature-class.html) class. The following code example explains the same. + +N> Signing using TimestampServer only works when the document is saved using asynchronous [save](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/save.html). It is not supported in synchronous [saveSync](https://pub.dev/documentation/syncfusion_flutter_pdf/latest/pdf/PdfDocument/saveSync.html). + +{% highlight dart %} + + // Create a new PDF document. + PdfDocument document = + PdfDocument(inputBytes: File('input.pdf').readAsBytesSync()); + // Load the existing signature field. + PdfSignatureField field = document.form.fields[0] as PdfSignatureField; + // Create a new timestamp server. + TimestampServer server = TimestampServer( + Uri.parse('http://syncfusion.digistamp.com'), + userName: 'user', + password: '123456', + timeOut: const Duration(milliseconds: 5000)); + // Check whether the timestamp server is valid. + bool isValid = await server.isValid; + if (isValid) { + // Add new signature to the existing signature field. + field.signature = PdfSignature(); + // Add the timestamp server to the signature. + field.signature!.timestampServer = server; + } + // Save the document. + File('output.pdf').writeAsBytesSync(await document.save()); + // Dispose the document. + document.dispose(); + +{% endhighlight %} \ No newline at end of file