How to create digital signature appearances in rotated PDF documents using C#
The Syncfusion® PDF library is a feature-rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This article explains how to insert a signature field into a rotated PDF document and apply a signature appearance to that field using Syncfusion’s PDF library.
Prerequisites
- Create a new console application project.
- Install the Syncfusion.Pdf.Net.Core NuGet package as a reference to your console application from Nuget.org.
Steps to add and sign a signature field in a rotated PDF document
- Include the following namespaces in your Program.cs file:
using System.IO;
using Syncfusion.Drawing;
using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Security;
- Use the following code in your Main method to draw a signature field in a rotated PDF document:
static void Main(string[] args)
{
// Open the PDF document file stream
using (FileStream inputStream = new FileStream("Input.pdf", FileMode.Open, FileAccess.Read))
{
PdfLoadedDocument pdfLoadedDocument = new PdfLoadedDocument(inputStream);
PdfLoadedPage ldPage = pdfLoadedDocument.Pages[0] as PdfLoadedPage;
// Create a certificate instance from a PFX file with a private key
FileStream certificateStream = new FileStream("PDF.pfx", FileMode.Open, FileAccess.Read);
PdfCertificate pdfCert = new PdfCertificate(certificateStream, "password123");
// Create a signature field
PdfSignature signature = new PdfSignature(pdfLoadedDocument, ldPage, pdfCert, "Signature1");
RectangleF bounds = new RectangleF(new PointF(20, 20), new SizeF(240, 70));
//Get the relative bounds based on the page rotation and set to the signature
signature.Bounds = GetRelativeBounds(ldPage, bounds);
// Draw the appearance of the signature
PdfGraphics graphics = signature.Appearance.Normal.Graphics;
FileStream imageStream = new FileStream("TestImage.png", FileMode.Open, FileAccess.Read);
PdfBitmap signatureImage = new PdfBitmap(imageStream);
//Draw the appearance based on the page rotation angle
RotateSignatureAppearance(signatureImage, signature.Appearance.Normal.Graphics, ldPage.Rotation, signature.Bounds);
// Save the document into the disk stream
Using(FileStream fileStream = new FileStream("Output.pdf", FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
pdfLoadedDocument.Save(fileStream );
}
// Close the document
pdfLoadedDocument.Close(true);
}
}
- Implement the
GetRelativeBounds
method to adjust the signature field bounds based on page rotation:
private static RectangleF GetRelativeBounds(PdfLoadedPage page, RectangleF bounds)
{
SizeF pagesize = page.Size;
RectangleF rectangle = bounds;
if (page.Rotation == PdfPageRotateAngle.RotateAngle90)
{
rectangle.X = bounds.Y;
rectangle.Y = pagesize.Height - ((bounds.X + bounds.Width));
rectangle.Width = bounds.Height;
rectangle.Height = bounds.Width;
}
else if (page.Rotation == PdfPageRotateAngle.RotateAngle270)
{
rectangle.Y = bounds.X;
rectangle.X = pagesize.Width - (bounds.Y + bounds.Height);
rectangle.Width = bounds.Height;
rectangle.Height = bounds.Width;
}
else if (page.Rotation == PdfPageRotateAngle.RotateAngle180)
{
rectangle.X = pagesize.Width - (bounds.X + bounds.Width);
rectangle.Y = pagesize.Height - (bounds.Y + bounds.Height);
}
return rectangle;
}
- Implement the
RotateSignatureAppearance
method to rotate the signature image based on page rotation:
private static void RotateSignatureAppearance(PdfImage image, PdfGraphics graphics, PdfPageRotateAngle angle, RectangleF bounds)
{
graphics.Save();
if (angle == PdfPageRotateAngle.RotateAngle90)
{
graphics.TranslateTransform(0, bounds.Height);
graphics.RotateTransform(-90);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
}
else if (angle == PdfPageRotateAngle.RotateAngle180)
{
graphics.TranslateTransform(bounds.Width, bounds.Height);
graphics.RotateTransform(-180);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
}
else if (angle == PdfPageRotateAngle.RotateAngle270)
{
graphics.TranslateTransform(bounds.Width, 0);
graphics.RotateTransform(-270);
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Height, bounds.Width));
}
else
{
graphics.DrawImage(image, new RectangleF(0, 0, bounds.Width, bounds.Height));
}
graphics.Restore();
}
A complete working sample can be downloaded from GitHub.
By executing the program, you will generate the following PDF document.
Conclusion
This article demonstrated how to draw a signature field in a rotated PDF document using Syncfusion’s PDF library. The code handles different page rotations and adjusts the signature field and image accordingly.
Take a moment to peruse the documentation. here you will find other options such as adding of digital signature, externally sign a PDF document, adding a signature validation, adding timestamp to PDF document, retrieve certificate details from existing PDF, enable LTV signature, digital signature with customization and digital signature validation.
You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications.
You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!