How to add Security, a Watermark, and Digital Signatures to PDFs on .NET Core

It's usually not enough to create a PDF with an API--most users also need to add security features, watermarks, and digital signatures. This blog walks you through how to do that in .NET Core with Documents for PDF.

Get the sample for adding security to PDFs

Why add security, watermarks, and digital signatures?

Today's digital world constantly creates and shares numerous documents everyday. Whether it is sharing of information, or asking people to collaborate, or sharing templates to fill in, documents are being shared on big and small scales. Whether the audience is within the company or outside, security is a key aspect when sharing confidential information. Another aspect is, being able to collect inputs from authorized people and signing off the documents with digital signatures, moving away from manual signing of paper documents and spending hours on it. Many organisations prefer PDF as a means for document sharing, since being a popular format, PDF takes care of the portability, security, maintaining the originality of the content and also that it is non-editable, so people cannot tamper with the original content.

A timesheet use case: Sign, watermark, and share documents securely by email

A software company deals with sharing documents�securely by email in a way that prevents data leaks among unauthorized people and also maintains the originality of the documents. The team needs:

1. A secure solution that would add password protection to the documents, so that only authorized people can open the documents

2. Watermarking that represents the document in a standard format specific to the company

3. Digital signatures in the documents

In this particular case, a manager wants to send Timesheet to all his employees who should fill it, sign it and send it back. This timesheet should be opened by only the manager (owner) and the employee (user). The timesheet should have a watermark so that no other timesheet format can be used by employee. The manager prefers PDF Format for the timesheet.

Once these features are applied, the manager sends the PDF to their employee. After filling the timesheet and digitally signing it, the employee sends it back to the manager, who then also digitally signs the document and sends it to the HR department.

For the scope of this blog,�we'll use an existing PDF with a blank timesheet. To get a sample of this timesheet, visit the sample demo.

Sample timesheet

Step 1: Import namespaces

Create a new .NET Core Console application and import following namespaces:

using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Pdf;
using GrapeCity.Documents.Pdf.Annotations;
using GrapeCity.Documents.Pdf.AcroForms;
using System.Security.Cryptography.X509Certificates;
using GrapeCity.Documents.Text;

Step 2: Load the timesheet pdf document

In order to add features to an existing PDF, you need to load the PDF in the GcPdfDocument object and initialize the graphics object.

var doc = new GcPdfDocument();
using (FileStream stream = new FileStream(@"ModifiedPDF.pdf", FileMode.OpenOrCreate, FileAccess.Write))
using (var fs = new FileStream("TimeSheetForm.pdf", FileMode.Open, FileAccess.Read))
   var g = doc.Pages[0].Graphics;
   var page = doc.Pages.Last;

Step 3: Add passwords

With Documents for PDF, you can set the following security features on the document:

  • UserPassword
  • OwnerPassword
  • AllowCopyContent
  • AllowEditAnnotations
  • AllowEditContent
  • AllowPrint
  • Add Digital Signature

The manager would want to set a personal password in addition to a password for the employee. They may also want to protect the content from copying or printing. Password protection and content protection can be added with Documents for PDF using the following code:

var std = new GrapeCity.Documents.Pdf.Security.StandardSecurityHandlerRev2();
std.OwnerPassword = "MA&3424";
std.UserPassword = "EMP&7644";
std.AllowPrint = false;
std.AllowCopyContent = false;

On opening the PDF, the document asks for a password (owner or user):

Password prompt

Step 4: Add a watermark

The manager now wants to add a standard watermark for confidential documents.The watermark can be added to the document using any image. If the image needs to be rotated on the page, it can be done so using the graphics.Transform method:

g.Transform = System.Numerics.Matrix3x2.CreateRotation((float)(-55 * Math.PI) / 180f,
    new System.Numerics.Vector2(page.Bounds.Width / 2, page.Bounds.Height / 2));
var image = Image.FromFile("top-secret77.png");
var align = ImageAlign.CenterImage;
g.DrawImage(image, page.Bounds, null, align);


Step 5: Add a digital signature

Note that in the TimeSheetForm.pdf, there are two Signature Fields at the bottom - one for Manager and the other for Employee. Using an instance of SignatureProperties class, an electronic signature can be added to the document and associated with a signature field already present in TimeSheetForm.pdf:

var pfxPath = Path.Combine('<your pfx file>');
X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxPath), "<your pfx password>",
X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);
SignatureProperties sp = new SignatureProperties();
sp.Certificate = cert;
sp.SignerName = "Jane Donahue";
sp.SigningDateTime = DateTime.Now;

//look for the Signature Field that needs signing, supSign in this case (if the timesheet is created using GcPdf)
SignatureField supSign = doc.AcroForm.Fields.First(f_ => f_.Name == "supSign") as SignatureField;
sp.SignatureField = supSign;
supSign.Widget.ButtonAppearance.Caption = "Jane Donahue";
supSign.Widget.Rect = supSign.Widget.Rect;
doc.Sign(sp, stream);

The timesheet is ready to be submitted to HR.

Final timesheet with digital signature

Get the sample for adding security to PDFs

In order to see how to fill this timesheet and sign it by employee, refer to this sample.�

Try Documents for PDF 30-day trial

Shilpa Sharma

Product Manager
comments powered by Disqus