Skip to content

Commit d966e06

Browse files
committed
992492: Added proper code for load and save changes.
1 parent 61be671 commit d966e06

File tree

31 files changed

+655
-1023
lines changed
  • Digital Signature
    • Adding-a-timestamp-to-PDF-document/.NET/Adding-a-timestamp-to-PDF-document
    • Adding-a-timestamp-to-an-existing-PDF/.NET/Adding-a-timestamp-to-an-existing-PDF
    • Adding-the-estimated-size-of-the-signature/.NET/Adding-the-estimated-size-of-the-signature
    • Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed
    • Create-LTV-when-signing-PDF-documents-externally/.NET/Create-LTV-when-signing-PDF-documents-externally
    • Customize-the-signed-date/.NET/Customize-the-signed-date
    • Customized-revocation-validation/.NET/Customized-revocation-validation
    • Deferred-signing-in-PDF-document/.NET/Deferred-signing-in-PDF-document
    • Draw-text-or-images-in-the-signature-appearance/.NET/Draw-text-or-images-in-the-signature-appearance
    • Drawing-Signature-Fields-in-Rotated-PDF-Documents/Drawing-Signature-Fields-in-Rotated-PDF-Documents
    • Enable-LTV-PDF-signature/.NET/Enable-LTV-PDF-signature
    • Externally-sign-a-PDF-document/.NET/Externally-sign-a-PDF-document
    • Externally-sign-the-PDF-document-using-IPdfExternalSigner/.NET/Externally-sign-the-PDF-document
    • Get-LTV-information/.NET/Get-LTV-information
    • Get-images-from-the-existing-signed-signature-field/.NET/Get-images-from-the-existing-signed-signature-field
    • Get-number-of-digital-signatures/.NET/Get-number-of-digital-signatures
    • Implementing-PdfFormFieldVisibility-types/.NET/Implementing-PdfFormFieldVisibility-types
    • Multiple-digital-signature/.NET/Multiple-digital-signature
    • Remove_existing_digital_signature_from_PDF/.NET/Remove_existing_digital_signature_from_PDF
    • Retrieve-certificate-details-from-an-existing-PDF/.NET/Retrieve-certificate-details-from-an-existing-PDF
    • Retrieve-digital-signature-information-from-PDF/.NET/Retrieve-digital-signature-information-from-PDF
    • Retrieve-revocation-certificate-information-from-digital-signature-embed-timestamp/.NET
    • Retrieve-revocation-certificate-information/.NET/Retrieve-revocation-certificate-information
    • Retrieve-signed-revision-information/.NET/Retrieve-signed-revision-information
    • Sign-PDF-without-showing-digital-signature/.NET/Sign-PDF-without-showing-digital-signature
    • Sign_PDF_Windows_Certificate/.NET/Sign_PDF_Windows_Certificate
    • Sign_PDF_with_LTA/.NET/Sign_PDF_with_LTA
    • Signing-an-existing-PDF-document/.NET/Signing-an-existing-PDF-document
    • TimestampingPDFwithExternalSigning/.NET/TimestampingPDFwithExternalSigning
    • Validate-all-signatures-in-digitally-signed-PDF/.NET/Validate-all-signatures-in-digitally-signed-PDF
    • Validate-the-digitally-signed-PDF-signature/.NET/Validate-the-digitally-signed-PDF-signature

31 files changed

+655
-1023
lines changed
Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Syncfusion.Pdf;
1+
using Syncfusion.Pdf;
42
using Syncfusion.Pdf.Security;
53

64
//Create a new pdf document.
7-
PdfDocument document = new PdfDocument();
8-
9-
//Adds a new page.
10-
PdfPage page = document.Pages.Add();
11-
12-
//Creates a digital signature.
13-
PdfSignature signature = new PdfSignature(page, "Signature");
14-
15-
//Add the time stamp by using the server URI.
16-
signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/"));
17-
18-
//Create file stream.
19-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
5+
using (PdfDocument document = new PdfDocument())
206
{
21-
//Save the PDF document to file stream.
22-
document.Save(outputFileStream);
7+
//Adds a new page.
8+
PdfPage page = document.Pages.Add();
9+
//Creates a digital signature.
10+
PdfSignature signature = new PdfSignature(page, "Signature");
11+
//Add the time stamp by using the server URI.
12+
signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/"));
13+
//Save the PDF document
14+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
2315
}
2416

25-
//Close the document.
26-
document.Close(true);
27-
Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,16 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Syncfusion.Pdf;
1+
using Syncfusion.Pdf;
42
using Syncfusion.Pdf.Parsing;
53
using Syncfusion.Pdf.Security;
64

7-
//Get stream from an existing PDF document.
8-
FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read);
9-
105
//Load the PDF document.
11-
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(docStream);
12-
13-
//Gets the page.
14-
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
15-
16-
//Creates a digital signature.
17-
PdfSignature signature = new PdfSignature(page, "Signature");
18-
19-
//Add the time stamp by using the server URI.
20-
signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/"));
21-
22-
//Create file stream.
23-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
6+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
247
{
25-
//Save the PDF document to file stream.
26-
loadedDocument.Save(outputFileStream);
8+
//Gets the page.
9+
PdfLoadedPage page = loadedDocument.Pages[0] as PdfLoadedPage;
10+
//Creates a digital signature.
11+
PdfSignature signature = new PdfSignature(page, "Signature");
12+
//Add the time stamp by using the server URI.
13+
signature.TimeStampServer = new TimeStampServer(new Uri("http://time.certum.pl/"));
14+
//Save the PDF document
15+
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
2716
}
28-
29-
//Close the document.
30-
loadedDocument.Close(true);
Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,20 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Syncfusion.Drawing;
1+
using Syncfusion.Drawing;
42
using Syncfusion.Pdf;
53
using Syncfusion.Pdf.Security;
64

7-
//Creating a new PDF Document.
8-
PdfDocument document = new PdfDocument();
9-
10-
//Adding a new page to the PDF document.
11-
PdfPageBase page = document.Pages.Add();
12-
13-
//Creates a certificate instance from the PFX file with a private key.
14-
FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read);
15-
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion");
16-
17-
//Add a new signature to the PDF page.
18-
PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature");
19-
signature.Bounds = new Rectangle(10, 20, 400, 200);
20-
21-
//Sets the estimated size of the signature.
22-
signature.EstimatedSignatureSize = 20000;
23-
24-
//Create file stream.
25-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
5+
//Create a new pdf document.
6+
using (PdfDocument document = new PdfDocument())
267
{
27-
//Save the PDF document to file stream.
28-
document.Save(outputFileStream);
8+
//Adding a new page to the PDF document.
9+
PdfPageBase page = document.Pages.Add();
10+
//Creates a certificate instance from the PFX file with a private key.
11+
FileStream certificateStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read);
12+
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "syncfusion");
13+
//Add a new signature to the PDF page.
14+
PdfSignature signature = new PdfSignature(document, page, pdfCert, "Signature");
15+
signature.Bounds = new RectangleF(10, 20, 400, 200);
16+
//Sets the estimated size of the signature.
17+
signature.EstimatedSignatureSize = 20000;
18+
//Save the PDF document
19+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
2920
}
30-
31-
//Close the document.
32-
document.Close(true);

Digital Signature/Check-If-PDF-Is-Signed/.NET/Check-If-PDF-Is-Signed/Program.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
1-
using System;
2-
using System.IO;
3-
using System.Reflection.Metadata;
4-
using Syncfusion.Pdf.Parsing;
1+
using Syncfusion.Pdf.Parsing;
52

6-
7-
// Open the signed PDF file for reading
8-
using (FileStream inputFileStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read))
3+
//Load the PDF document.
4+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
95
{
10-
// Load the PDF document
11-
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(inputFileStream);
12-
136
// Check if the document contains a form with fields
147
if (loadedDocument.Form == null || loadedDocument.Form.Fields.Count == 0)
158
{
@@ -32,6 +25,6 @@
3225
}
3326
}
3427
}
35-
//Close the document
36-
loadedDocument.Close(true);
28+
//Save the PDF document
29+
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
3730
}
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,47 @@
1-
// See https://aka.ms/new-console-template for more information
2-
3-
using Syncfusion.Pdf;
1+
using Syncfusion.Pdf;
42
using Syncfusion.Pdf.Parsing;
53
using Syncfusion.Pdf.Security;
64
using System.Security.Cryptography;
75
using System.Security.Cryptography.Pkcs;
86
using System.Security.Cryptography.X509Certificates;
97

10-
11-
//Get the stream from the document.
12-
FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read);
13-
148
//Load an existing PDF document.
15-
PdfLoadedDocument document = new PdfLoadedDocument(documentStream);
16-
17-
//Get the page of the existing PDF document.
18-
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
19-
20-
//Create a new PDF signature without PdfCertificate instance.
21-
PdfSignature signature = new PdfSignature(document, loadedPage, null, "Signature1");
22-
23-
//Hook up the ComputeHash event.
24-
signature.ComputeHash += Signature_ComputeHash;
25-
26-
//Save the document into stream
27-
MemoryStream stream = new MemoryStream();
28-
document.Save(stream);
29-
//Close the document
30-
document.Close(true);
31-
32-
33-
//Load an existing PDF stream..
34-
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
35-
36-
//Gets the first signature field of the PDF document
37-
PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
38-
PdfSignature pdfSignature = signatureField.Signature;
39-
40-
//Create X509Certificate2 from your certificate to create a long-term validity.
41-
X509Certificate2 x509 = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion");
42-
43-
//Create LTV with your public certificates.
44-
pdfSignature.CreateLongTermValidity(new List<X509Certificate2> { x509 });
45-
46-
//Create file stream.
47-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
9+
using (PdfLoadedDocument document = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
4810
{
49-
//Save the PDF document to file stream.
50-
loadedDocument.Save(outputFileStream);
11+
//Get the page of the existing PDF document.
12+
PdfLoadedPage loadedPage = document.Pages[0] as PdfLoadedPage;
13+
//Create a new PDF signature without PdfCertificate instance.
14+
PdfSignature signature = new PdfSignature(document, loadedPage, null, "Signature1");
15+
//Hook up the ComputeHash event.
16+
signature.ComputeHash += Signature_ComputeHash;
17+
//Save the document into stream
18+
MemoryStream stream = new MemoryStream();
19+
document.Save(stream);
20+
//Load an existing PDF stream..
21+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream))
22+
{
23+
//Gets the first signature field of the PDF document
24+
PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
25+
PdfSignature pdfSignature = signatureField.Signature;
26+
//Create X509Certificate2 from your certificate to create a long-term validity.
27+
X509Certificate2 x509 = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion");
28+
//Create LTV with your public certificates.
29+
pdfSignature.CreateLongTermValidity(new List<X509Certificate2> { x509 });
30+
//Save the PDF document.
31+
loadedDocument.Save(Path.GetFullPath(@"Output/Output.pdf"));
32+
}
5133
}
52-
53-
//Close the document.
54-
loadedDocument.Close(true);
55-
56-
5734
void Signature_ComputeHash(object sender, PdfSignatureEventArgs ars)
5835
{
5936
//Get the document bytes
6037
byte[] documentBytes = ars.Data;
61-
6238
SignedCms signedCms = new SignedCms(new ContentInfo(documentBytes), detached: true);
63-
6439
//Compute the signature using the specified digital ID file and the password
6540
X509Certificate2 certificate = new X509Certificate2(Path.GetFullPath(@"Data/PDF.pfx"), "syncfusion");
6641
CmsSigner cmsSigner = new CmsSigner(certificate);
67-
6842
//Set the digest algorithm SHA256
6943
cmsSigner.DigestAlgorithm = new Oid("2.16.840.1.101.3.4.2.1");
70-
7144
signedCms.ComputeSignature(cmsSigner);
72-
7345
//Embed the encoded digital signature to the PDF document
7446
ars.SignedData = signedCms.Encode();
7547
}

Digital Signature/Customize-the-signed-date/.NET/Customize-the-signed-date/Program.cs

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@
22
using Syncfusion.Pdf.Security;
33
using Syncfusion.Drawing;
44

5-
//Create a new PDF document instance.
6-
PdfDocument document = new PdfDocument();
7-
//Add a new page to the PDF document.
8-
PdfPage page = document.Pages.Add();
9-
//Create a digital signature and associate it with the added page.
10-
PdfSignature signature = new PdfSignature(page, "Signature", new DateTime(2020, 12, 24, 10, 50, 10));
11-
//Set the timestamp server for the digital signature.
12-
signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamp.digicert.com"));
13-
//Set the signed name for the signature.
14-
signature.SignedName = "Test";
15-
//Define the bounds for the signature (position and size on the page).
16-
signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(200, 100));
17-
//Save the document to a file using a file stream.
18-
using (FileStream fileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
5+
//Create a new pdf document.
6+
using (PdfDocument document = new PdfDocument())
197
{
20-
document.Save(fileStream);
21-
}
22-
//Close the document to release resources.
23-
document.Close(true);
8+
//Add a new page to the PDF document.
9+
PdfPage page = document.Pages.Add();
10+
//Create a digital signature and associate it with the added page.
11+
PdfSignature signature = new PdfSignature(page, "Signature", new DateTime(2020, 12, 24, 10, 50, 10));
12+
//Set the timestamp server for the digital signature.
13+
signature.TimeStampServer = new TimeStampServer(new Uri("http://timestamp.digicert.com"));
14+
//Set the signed name for the signature.
15+
signature.SignedName = "Test";
16+
//Define the bounds for the signature (position and size on the page).
17+
signature.Bounds = new RectangleF(new PointF(0, 0), new SizeF(200, 100));
18+
//Save the PDF document
19+
document.Save(Path.GetFullPath(@"Output/Output.pdf"));
20+
}
Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,24 @@
11
using Syncfusion.Pdf.Parsing;
22
using Syncfusion.Pdf.Security;
33

4-
//Gets the stream from the document
5-
FileStream documentStream = new FileStream(Path.GetFullPath(@"Data/Input.pdf"), FileMode.Open, FileAccess.Read);
6-
7-
//Loads an existing signed PDF document
8-
PdfLoadedDocument document = new PdfLoadedDocument(documentStream);
9-
10-
// Gets the signature field
11-
PdfLoadedSignatureField signatureField = document.Form.Fields[0] as PdfLoadedSignatureField;
12-
13-
// Signature validation options
14-
PdfSignatureValidationOptions options = new PdfSignatureValidationOptions();
15-
16-
// Sets the revocation type
17-
options.RevocationValidationType = RevocationValidationType.Crl;
18-
19-
// Validate signature and get validation result
20-
PdfSignatureValidationResult result = signatureField.ValidateSignature(options);
21-
22-
//Check whether the CRL is revoked
23-
if (result.RevocationResult.IsRevokedCRL)
4+
//Load the PDF document.
5+
using (PdfLoadedDocument loadedDocument = new PdfLoadedDocument(Path.GetFullPath(@"Data/Input.pdf")))
246
{
25-
Console.WriteLine("CRL is revoked");
26-
}
27-
else
28-
{
29-
Console.WriteLine("CRL is not revoked");
30-
}
31-
32-
// Closes the document
33-
document.Close(true);
7+
// Gets the signature field
8+
PdfLoadedSignatureField signatureField = loadedDocument.Form.Fields[0] as PdfLoadedSignatureField;
9+
// Signature validation options
10+
PdfSignatureValidationOptions options = new PdfSignatureValidationOptions();
11+
// Sets the revocation type
12+
options.RevocationValidationType = RevocationValidationType.Crl;
13+
// Validate signature and get validation result
14+
PdfSignatureValidationResult result = signatureField.ValidateSignature(options);
15+
//Check whether the CRL is revoked
16+
if (result.RevocationResult.IsRevokedCRL)
17+
{
18+
Console.WriteLine("CRL is revoked");
19+
}
20+
else
21+
{
22+
Console.WriteLine("CRL is not revoked");
23+
}
24+
}

0 commit comments

Comments
 (0)