Skip to content

Commit f62a70a

Browse files
Merge pull request #497 from telerik/new-kb-pdfprocessing-sign-an-unsigned-pdf-30dc6a51b9964a5da00ac0e5ca6a77fa
Added new kb article pdfprocessing-sign-an-unsigned-pdf
2 parents b67cfec + 39838ef commit f62a70a

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
108 KB
Loading
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: Signing an Unsigned PDF Document that Contains a Signature Field with RadPdfProcessing
3+
description: This article provides a guide on how to sign an empty signature field by using text and image programmatically using RadPdfProcessing.
4+
type: how-to
5+
page_title: Signing a PDF Document that Contains a Signature Field with RadPdfProcessing
6+
slug: pdfprocessing-sign-an-unsigned-pdf
7+
tags: processing, pdf, sign, image, document, digital, signature, field, empty
8+
res_type: kb
9+
ticketid: 1676495
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| ---- | ---- | ---- |
16+
| 2024.4.1106| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
This tutorial demonstrates how to import an unsigned PDF containing a [signature field]({%slug radpdfprocessing-model-interactive-forms-form-fields-signaturefield%}) and sign it using [RadPdfProcessing]({%slug radpdfprocessing-overview%}).
21+
22+
![Sign an Unsigned PDF](images/sign-an-unsigned-pdf.png)
23+
24+
## Solution
25+
To add signatures and images to PDF documents and ensure the signed version correctly overwrites an existing file, follow these steps:
26+
27+
1. **Check if the Document is Already Signed**: Before signing the signature field, it's essential to check if the document is already signed. This can be done by iterating through the form fields and checking for signature fields.
28+
29+
2. **Prepare the Document for Signing**: Load the document into a `RadFixedDocument` object using the `PdfFormatProvider.Import` method. If the document already contains an empty signature field, you will need to access this field and sign it.
30+
31+
3. **Add the Signature**: Use a certificate to sign the document. The `SignatureField.Signature` property allows you to assign a new `Signature` object, which is created using the certificate.
32+
33+
4. **Add an Image**: To insert an image, such as a signature graphic, use a `FixedContentEditor` on the desired **FormSource** and use the `DrawBlock` method. The image can be loaded from a file using a `FileStream` and added to a `Block` object.
34+
35+
5. **Export the Signed Document**: Before exporting the signed document, delete the previous version of the file if it exists. This step is crucial to avoid permission issues or structure mismatches in the PDF file. Use the `PdfFormatProvider.Export` method to save the signed document.
36+
37+
Below is a sample code snippet demonstrating these steps:
38+
39+
```csharp
40+
static void Main(string[] args)
41+
{
42+
PdfFormatProvider provider = new PdfFormatProvider();
43+
RadFixedDocument document = provider.Import(File.ReadAllBytes("unsigned.pdf"), TimeSpan.FromSeconds(10));
44+
45+
bool isSigned = CheckSignedDocument(document);
46+
Debug.WriteLine(isSigned.ToString());
47+
FormSource formSource = new FormSource();
48+
formSource.Size=new Size(420, 150);
49+
50+
X509Certificate2 certificate = new X509Certificate2("JohnDoe.pfx", "johndoe");
51+
SignatureField signatureField = document.AcroForm.FormFields.Where(f => f.FieldType == FormFieldType.Signature).FirstOrDefault() as SignatureField;
52+
if (signatureField != null)
53+
{
54+
signatureField.Signature = new Signature(certificate);
55+
SignatureWidget widget = signatureField.Widgets.FirstOrDefault();
56+
if (widget != null)
57+
{
58+
formSource = widget.Content.NormalContentSource;
59+
FixedContentEditor fixedContentEditor = new FixedContentEditor(formSource);
60+
fixedContentEditor.TextProperties.FontSize = 30;
61+
fixedContentEditor.Position.Translate(30, 0);
62+
fixedContentEditor.DrawText("John Doe");
63+
fixedContentEditor.Position.Translate(200, 5);
64+
65+
FileStream fileStream = new FileStream("ProgressNinjas.png", FileMode.Open);
66+
ImageSource _imageSource = new ImageSource(fileStream);
67+
Block imageBlock = new Block();
68+
imageBlock.InsertImage(_imageSource);
69+
fixedContentEditor.DrawBlock(imageBlock);
70+
71+
72+
fixedContentEditor.Position.Translate(0, 90);
73+
fixedContentEditor.TextProperties.FontSize = 20;
74+
fixedContentEditor.DrawText("Digitally signed on: " + DateTime.Now.ToString());
75+
fixedContentEditor.Position.Translate(40, 120);
76+
fixedContentEditor.TextProperties.FontSize = 20;
77+
fixedContentEditor.DrawText("(Click here to view the signature info)");
78+
}
79+
80+
document.Pages[0].Annotations.Add(widget);
81+
82+
string signedDocumentFilePath = "Signed.pdf";
83+
File.Delete(signedDocumentFilePath);
84+
using (Stream output = new FileStream(signedDocumentFilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
85+
{
86+
provider.Export(document, output, TimeSpan.FromSeconds(10));
87+
}
88+
89+
isSigned = CheckSignedDocument(document);
90+
Debug.WriteLine(isSigned.ToString());
91+
Process.Start(new ProcessStartInfo() { FileName = signedDocumentFilePath, UseShellExecute = true });
92+
}
93+
94+
}
95+
private static bool CheckSignedDocument(RadFixedDocument document)
96+
{
97+
bool isSigned = false;
98+
var signatureFields = document.AcroForm.FormFields.Where(field => field.FieldType == FormFieldType.Signature).ToList();
99+
if (signatureFields != null)
100+
{
101+
foreach (var signatureField in signatureFields)
102+
{
103+
SignatureField field = (SignatureField)signatureField;
104+
105+
if (field != null && field.Signature != null)
106+
{
107+
if (field.Signature == null)
108+
{
109+
isSigned = false;
110+
break;
111+
}
112+
SignatureDataProperties properties = field.Signature.Properties;
113+
114+
Debug.WriteLine("Signed on: " + properties.TimeOfSigning.ToString());
115+
isSigned = true;
116+
break;
117+
118+
}
119+
}
120+
}
121+
return isSigned;
122+
}
123+
```
124+
125+
Remember to adjust the file paths, certificate details, and specific document requirements according to your application's context.
126+
127+
## See Also
128+
- [RadPdfProcessing]({%slug radpdfprocessing-overview%})
129+
- [Form Fields concept in RadPdfProcessing]({%slug radpdfprocessing-model-interactive-forms-form-fields%})
130+
- [Digital Signature in RadPdfProcessing]({%slug radpdfprocessing-features-digital-signature%})

libraries/radpdfprocessing/features/digital-signature/overview.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ The following example shows a full code snippet for a simple signing of a newly
109109
* [How to Create Invisible Signatures for PDF Documents]({%slug pdf-invisible-signatures%})
110110
* [Signing a PDF Document with a SignatureWidget]({%slug sign-pdf-with-signature-widget%})
111111
* [Verifying If Digital Signatures Exist in PDF Documents]({%slug verify-digital-signatures-radpdfprocessing%})
112+
* [Signing an Unsigned PDF Document that Contains a Digital Signature with RadPdfProcessing]({%slug pdfprocessing-sign-an-unsigned-pdf%})

0 commit comments

Comments
 (0)