Skip to content

Commit fc1a468

Browse files
authored
Merge pull request #136 from aspose-pdf/examples/ad26a8da-facades---sign-documents
Add 32 new Facades - Sign Documents code examples
2 parents 8972894 + 496e8dc commit fc1a468

34 files changed

Lines changed: 2401 additions & 0 deletions
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
using System.Drawing;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPath = "input.pdf";
11+
const string outputPath = "signed.pdf";
12+
const string certPath = "certificate.pfx";
13+
const string certPassword = "password";
14+
15+
if (!File.Exists(inputPath))
16+
{
17+
Console.Error.WriteLine($"Input file not found: {inputPath}");
18+
return;
19+
}
20+
21+
// Create the signature facade and bind the PDF
22+
PdfFileSignature pdfSignature = new PdfFileSignature();
23+
pdfSignature.BindPdf(inputPath);
24+
25+
// Set the signing certificate
26+
pdfSignature.SetCertificate(certPath, certPassword);
27+
28+
// Optional: set a visual appearance image for the signature
29+
// pdfSignature.SignatureAppearance = "signature.png";
30+
31+
// Define the rectangle where the visible signature will appear
32+
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 100);
33+
34+
// Sign page 1 with reason and location
35+
pdfSignature.Sign(1, "Approved for release", "contact@example.com", "New York", true, rect);
36+
37+
// Save the signed PDF
38+
pdfSignature.Save(outputPath);
39+
40+
Console.WriteLine($"Signed PDF saved as '{outputPath}'.");
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPath = "input.pdf";
10+
const string outputPath = "signed_output.pdf";
11+
const string certPath = "certificate.pfx";
12+
const string certPassword = "password";
13+
14+
if (!File.Exists(inputPath))
15+
{
16+
Console.Error.WriteLine($"Input file not found: {inputPath}");
17+
return;
18+
}
19+
if (!File.Exists(certPath))
20+
{
21+
Console.Error.WriteLine($"Certificate file not found: {certPath}");
22+
return;
23+
}
24+
25+
PdfFileSignature pdfSign = new PdfFileSignature();
26+
pdfSign.BindPdf(inputPath);
27+
pdfSign.SetCertificate(certPath, certPassword);
28+
// Optional: set an image to appear as the signature appearance
29+
// pdfSign.SignatureAppearance = "signature.png";
30+
31+
System.Drawing.Rectangle rectFirst = new System.Drawing.Rectangle(100, 100, 200, 100);
32+
pdfSign.Sign(2, "First signature", "alice@example.com", "New York", true, rectFirst);
33+
34+
System.Drawing.Rectangle rectSecond = new System.Drawing.Rectangle(300, 400, 150, 80);
35+
pdfSign.Sign(3, "Second signature", "bob@example.com", "London", true, rectSecond);
36+
37+
pdfSign.Save(outputPath);
38+
Console.WriteLine($"Signed PDF saved to '{outputPath}'.");
39+
}
40+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Forms;
5+
using Aspose.Pdf.Facades;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPath = "input.pdf";
12+
const string outputPath = "signed.pdf";
13+
const string certificatePath = "certificate.pfx";
14+
const string certificatePassword = "password";
15+
16+
if (!File.Exists(inputPath))
17+
{
18+
Console.Error.WriteLine($"Input file not found: {inputPath}");
19+
return;
20+
}
21+
if (!File.Exists(certificatePath))
22+
{
23+
Console.Error.WriteLine($"Certificate file not found: {certificatePath}");
24+
return;
25+
}
26+
27+
using (Document document = new Document(inputPath))
28+
{
29+
// Create a signature field on page 1
30+
Page page = document.Pages[1];
31+
Aspose.Pdf.Rectangle fieldRect = new Aspose.Pdf.Rectangle(100, 100, 300, 150);
32+
SignatureField signatureField = new SignatureField(page, fieldRect)
33+
{
34+
PartialName = "Signature1"
35+
};
36+
// Add the field to the form collection – use Document.Form.Add(field, pageNumber)
37+
document.Form.Add(signatureField, 1);
38+
39+
// Sign the document using the created signature field
40+
PdfFileSignature pdfSigner = new PdfFileSignature();
41+
pdfSigner.BindPdf(document);
42+
pdfSigner.SetCertificate(certificatePath, certificatePassword);
43+
PKCS7 pkcs7Signature = new PKCS7(certificatePath, certificatePassword)
44+
{
45+
Reason = "I agree",
46+
ContactInfo = "email@example.com",
47+
Location = "Office"
48+
};
49+
pdfSigner.Sign("Signature1", pkcs7Signature);
50+
pdfSigner.Save(outputPath);
51+
}
52+
53+
Console.WriteLine($"Signed PDF saved to '{outputPath}'.");
54+
}
55+
}

facades-sign-documents/agents.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
name: Facades - Sign Documents
3+
description: C# examples for Facades - Sign Documents using Aspose.PDF for .NET
4+
language: csharp
5+
framework: net10.0
6+
parent: ../agents.md
7+
---
8+
9+
# AGENTS - Facades - Sign Documents
10+
11+
## Persona
12+
13+
You are a C# developer specializing in PDF processing using Aspose.PDF for .NET,
14+
working within the **Facades - Sign Documents** category.
15+
This folder contains standalone C# examples for Facades - Sign Documents operations.
16+
See the root [agents.md](../agents.md) for repository-wide conventions and boundaries.
17+
18+
## Scope
19+
- This folder contains examples for **Facades - Sign Documents**.
20+
- Files are standalone `.cs` examples stored directly in this folder.
21+
22+
## Required Namespaces
23+
24+
- `using Aspose.Pdf.Facades;` (30/32 files) ← category-specific
25+
- `using Aspose.Pdf;` (15/32 files)
26+
- `using Aspose.Pdf.Forms;` (13/32 files)
27+
- `using Aspose.Pdf.Text;` (1/32 files)
28+
- `using System;` (32/32 files)
29+
- `using System.IO;` (29/32 files)
30+
- `using System.Collections.Generic;` (8/32 files)
31+
- `using System.Drawing;` (8/32 files)
32+
- `using System.Globalization;` (2/32 files)
33+
- `using System.Security.Cryptography.X509Certificates;` (2/32 files)
34+
- `using System.Linq;` (1/32 files)
35+
- `using System.Text;` (1/32 files)
36+
- `using System.Text.Json;` (1/32 files)
37+
38+
## Common Code Pattern
39+
40+
Most files follow this pattern:
41+
42+
```csharp
43+
using (Document doc = new Document("input.pdf"))
44+
{
45+
// ... operations ...
46+
doc.Save("output.pdf");
47+
}
48+
```
49+
50+
## Files in this folder
51+
52+
| File | Title | Key APIs | Description |
53+
|------|-------|----------|-------------|
54+
| [add-digital-signature](./add-digital-signature.cs) | Add Digital Signature with Reason and Location | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to add a visible digital signature to a PDF and set the signature reason and loc... |
55+
| [add-second-digital-signature-page-three](./add-second-digital-signature-page-three.cs) | Add a second digital signature on page three of a PDF | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates adding two digital signatures to a PDF, with the second placed on page three using a... |
56+
| [add-signature-field](./add-signature-field.cs) | Add Signature Field and Sign PDF | `Document`, `Form`, `SignatureField` | Demonstrates adding a signature field to a specific page and then signing the PDF using a certifi... |
57+
| [custom-signature-appearance](./custom-signature-appearance.cs) | Create Custom Signature Appearance without Default Caption | `Document`, `Page`, `Rectangle` | Demonstrates how to create a PDF signature field with a custom appearance that hides the default ... |
58+
| [digitally-sign-pdf-page-one](./digitally-sign-pdf-page-one.cs) | Digitally Sign PDF on First Page | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to bind a PDF with PdfFileSignature, set a certificate, and add a digital signat... |
59+
| [extract-certificate-details](./extract-certificate-details.cs) | Extract Certificate Issuer and Expiration from Signed PDF | `PdfFileSignature`, `GetSignatureNames`, `TryExtractCertificate` | Demonstrates how to read a signed PDF, retrieve each signature's X.509 certificate, and display i... |
60+
| [extract-certificate-serial-number](./extract-certificate-serial-number.cs) | Extract Signing Certificate Serial Number from PDF | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Loads a signed PDF, extracts the signing certificate from the first signature field, and logs its... |
61+
| [extract-signature-image](./extract-signature-image.cs) | Extract Signature Field Image to PNG | `Document`, `Form`, `ExtractImage` | Demonstrates extracting the image from a signature form field named 'WitnessSignature' and saving... |
62+
| [extract-signature-images-html](./extract-signature-images-html.cs) | Extract Signature Images and Generate HTML Report | `Document`, `PdfFileSignature`, `SignatureName` | Extracts signature images from a signed PDF and creates an HTML file that displays each signature... |
63+
| [extract-signing-certificate](./extract-signing-certificate.cs) | Extract Signing Certificate from PDF Signature Field | `PdfFileSignature`, `ExtractCertificate`, `SignatureName` | Demonstrates how to extract the X.509 certificate from a PDF signature field named 'LegalSignatur... |
64+
| [list-pdf-signature-names](./list-pdf-signature-names.cs) | List Signature Names in PDF | `PdfFileSignature`, `BindPdf`, `GetSignNames` | Demonstrates how to retrieve and display all digital signature names from a PDF using Aspose.Pdf. |
65+
| [list-signature-reason-location](./list-signature-reason-location.cs) | List Signature Reasons and Locations in PDF | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Demonstrates how to enumerate digital signatures in a PDF and output each signature's reason and ... |
66+
| [locale-specific-signature](./locale-specific-signature.cs) | Locale-Specific Signature Text in PDF | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Adds a visible digital signature to a PDF with signature text adapted to German, Spanish, or Japa... |
67+
| [pdf-signature-report](./pdf-signature-report.cs) | Generate PDF Signature Report | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Creates a PDF summarizing each digital signature's name, signer, and verification result from an ... |
68+
| [remove-all-signatures](./remove-all-signatures.cs) | Remove All Signatures from PDF | `PdfFileSignature`, `BindPdf`, `RemoveSignatures` | Demonstrates how to remove every digital signature from a PDF file using Aspose.Pdf's PdfFileSign... |
69+
| [remove-and-resign-pdf](./remove-and-resign-pdf.cs) | Remove Existing Signatures and Re‑sign PDF with New Certific... | `PdfFileSignature`, `BindPdf`, `RemoveSignatures` | Demonstrates how to remove all signatures from a PDF and then apply a new digital signature using... |
70+
| [remove-pdf-signature](./remove-pdf-signature.cs) | Remove Specific Signature from PDF | `PdfFileSignature`, `BindPdf`, `RemoveSignature` | Demonstrates how to remove a digital signature named 'ApprovalSignature' from a PDF using Aspose.... |
71+
| [remove-pdf-signatures](./remove-pdf-signatures.cs) | Remove All Signatures from PDFs in a Directory | `PdfFileSignature`, `BindPdf`, `RemoveSignatures` | Recursively scans a directory for PDF files, removes all digital signatures from each PDF, and sa... |
72+
| [remove-signature-with-error-handling](./remove-signature-with-error-handling.cs) | Remove Specific Signature with Error Handling | `PdfFileSignature`, `SignatureName`, `GetSignatureNames` | Shows how to safely remove a named digital signature from a PDF, checking for its existence and h... |
73+
| [set-french-language-signature](./set-french-language-signature.cs) | Set French Language for Digital Signature Caption | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to change the language of a digital signature caption to French by setting the S... |
74+
| [set-signature-background](./set-signature-background.cs) | Set Semi-Transparent Background for PDF Signature Appearance | `PdfFileSignature`, `SignatureCustomAppearance`, `Color` | Demonstrates how to configure a PDF signature's custom appearance with a semi‑transparent backgro... |
75+
| [sign-pdf-empty-signature-fields](./sign-pdf-empty-signature-fields.cs) | Sign PDF with Empty Reason, Contact, and Location | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to sign a PDF using Aspose.Pdf while suppressing the reason, contact, and locati... |
76+
| [sign-pdf-hidden-appearance](./sign-pdf-hidden-appearance.cs) | Sign PDF with Hidden Signature Appearance | `PdfFileSignature`, `PKCS7`, `BindPdf` | Demonstrates how to apply a cryptographically valid digital signature to a PDF while completely h... |
77+
| [sign-pdf-pfx](./sign-pdf-pfx.cs) | Sign PDF with PFX Certificate | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to digitally sign a PDF using a password‑protected PFX certificate. |
78+
| [sign-pdf-visible-last-page](./sign-pdf-visible-last-page.cs) | Sign PDF with Visible Signature on Last Page | `PdfFileSignature`, `Document`, `Page` | Demonstrates how to add a visible digital signature to the bottom‑right corner of the last page o... |
79+
| [sign-pdf-with-image](./sign-pdf-with-image.cs) | Sign PDF with Image Signature | `PdfFileSignature`, `Document`, `BindPdf` | Demonstrates how to apply an image signature to a PDF using Aspose.Pdf's PdfFileSignature facade. |
80+
| [signature-custom-appearance](./signature-custom-appearance.cs) | Configure SignatureCustomAppearance with Foreground Image an... | `PdfFileSignature`, `SignatureCustomAppearance`, `Document` | Demonstrates how to set a background color and draw the signature image as a foreground image usi... |
81+
| [validate-pdf-signature](./validate-pdf-signature.cs) | Validate PDF Signature Integrity | `PdfFileSignature`, `VerifySignature` | Demonstrates how to verify the cryptographic integrity of a PDF signature field named 'ManagerSig... |
82+
| [verify-multiple-signatures](./verify-multiple-signatures.cs) | Verify Multiple Signatures in PDF | `PdfFileSignature`, `BindPdf`, `GetSignNames` | Demonstrates how to list all signature names in a PDF and verify each signature using PdfFileSign... |
83+
| [verify-pdf-no-signatures](./verify-pdf-no-signatures.cs) | Verify PDF Without Signatures Returns False | `PdfFileSignature`, `BindPdf`, `ContainsSignature` | Demonstrates checking a PDF that has no digital signatures using PdfFileSignature.VerifySigned, w... |
84+
| ... | | | *and 2 more files* |
85+
86+
## Category Statistics
87+
- Total examples: 32
88+
89+
## Category-Specific Tips
90+
91+
### Key API Surface
92+
- `Aspose.Pdf.Document`
93+
- `Aspose.Pdf.Facades.Algorithm`
94+
- `Aspose.Pdf.Facades.DocMDPAccessPermissions`
95+
- `Aspose.Pdf.Facades.DocMDPSignature`
96+
- `Aspose.Pdf.Facades.DocumentPrivilege`
97+
- `Aspose.Pdf.Facades.KeySize`
98+
- `Aspose.Pdf.Facades.PKCS7`
99+
- `Aspose.Pdf.Facades.PdfFileInfo`
100+
- `Aspose.Pdf.Facades.PdfFileSecurity`
101+
- `Aspose.Pdf.Facades.PdfFileSecurity.BindPdf`
102+
- `Aspose.Pdf.Facades.PdfFileSecurity.EncryptFile`
103+
- `Aspose.Pdf.Facades.PdfFileSecurity.Save`
104+
- `Aspose.Pdf.Facades.PdfFileSignature`
105+
- `Aspose.Pdf.Facades.PdfFileSignature.BindPdf`
106+
- `Aspose.Pdf.Facades.PdfFileSignature.Certify`
107+
108+
### Rules
109+
- Create a PdfFileInfo for {input_pdf} and read its IsEncrypted property to obtain a {bool} indicating whether the PDF is encrypted (password‑protected).
110+
- Use PdfFileInfo instead of loading a full Document when only document metadata such as encryption status is needed.
111+
- Instantiate PdfFileSecurity, call BindPdf({input_pdf}) to load the encrypted PDF, then invoke DecryptFile({string_literal}) with the owner password to decrypt it.
112+
- After decryption, call Save({output_pdf}) on the PdfFileSecurity instance to write the unprotected PDF to disk.
113+
- Instantiate {class} (PdfFileSecurity), call BindPdf({input_pdf}) to load the document, then invoke ChangePassword({owner_password}, {new_user_password}, {new_owner_password}) to set new passwords, and finally Save({output_pdf}) to write the protected file.
114+
115+
### Warnings
116+
- DecryptFile requires the owner password; decryption with only a user password is not covered by this example.
117+
- The example assumes the PDF is protected with an owner password; if the PDF has no password, an empty string may be required for the current owner password.
118+
- PdfFileSecurity belongs to the Aspose.Pdf.Facades namespace, which may be deprecated in future releases; consider using the newer Aspose.Pdf.Security namespace if available.
119+
- A valid PKCS#7 certificate file and correct password are required; otherwise signing will fail.
120+
- Custom appearance may not be rendered identically across all PDF viewers.
121+
122+
## General Tips
123+
- See parent [agents.md](../agents.md) for:
124+
- **Boundaries** — Always / Ask First / Never rules for all examples
125+
- **Common Mistakes** — verified anti-patterns that cause build failures
126+
- **Domain Knowledge** — cross-cutting API-specific gotchas
127+
- **Testing Guide** — build and run verification steps
128+
- Review code examples in this folder for Facades - Sign Documents patterns
129+
130+
<!-- AUTOGENERATED:START -->
131+
Updated: 2026-04-01 | Run: `20260401_103902_81d716`
132+
<!-- AUTOGENERATED:END -->
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.IO;
3+
using System.Drawing;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Forms;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
const string inputPdf = "input.pdf";
12+
const string outputPdf = "signed.pdf";
13+
const string certificatePath = "certificate.pfx";
14+
const string certificatePassword = "password";
15+
16+
if (!File.Exists(inputPdf))
17+
{
18+
Console.Error.WriteLine("Input PDF not found: " + inputPdf);
19+
return;
20+
}
21+
if (!File.Exists(certificatePath))
22+
{
23+
Console.Error.WriteLine("Certificate file not found: " + certificatePath);
24+
return;
25+
}
26+
27+
using (PdfFileSignature pdfSignature = new PdfFileSignature())
28+
{
29+
pdfSignature.BindPdf(inputPdf);
30+
31+
PKCS7 pkcs7 = new PKCS7(certificatePath, certificatePassword);
32+
SignatureCustomAppearance appearance = new SignatureCustomAppearance();
33+
appearance.DigitalSignedLabel = ""; // hide the default caption
34+
appearance.ShowContactInfo = false;
35+
appearance.ShowLocation = false;
36+
appearance.ShowReason = false;
37+
38+
pkcs7.CustomAppearance = appearance;
39+
40+
System.Drawing.Rectangle signatureRect = new System.Drawing.Rectangle(100, 100, 200, 50);
41+
pdfSignature.Sign(1, true, signatureRect, pkcs7);
42+
pdfSignature.Save(outputPdf);
43+
}
44+
45+
Console.WriteLine("PDF signed and saved to '" + outputPdf + "'.");
46+
}
47+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf.Facades;
4+
using Aspose.Pdf.Forms;
5+
using System.Drawing;
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
string inputPdf = "input.pdf";
12+
string outputPdf = "signed_output.pdf";
13+
string certificatePath = "certificate.pfx";
14+
string certificatePassword = "password";
15+
16+
if (!File.Exists(inputPdf))
17+
{
18+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
19+
return;
20+
}
21+
if (!File.Exists(certificatePath))
22+
{
23+
Console.Error.WriteLine($"Certificate file not found: {certificatePath}");
24+
return;
25+
}
26+
27+
try
28+
{
29+
PdfFileSignature signer = new PdfFileSignature();
30+
signer.BindPdf(inputPdf);
31+
32+
// Optional: set a visual appearance image for the signature
33+
// signer.SignatureAppearance = "signature_image.png";
34+
35+
PKCS7 pkcs7 = new PKCS7(certificatePath, certificatePassword);
36+
pkcs7.Reason = "Document approved";
37+
pkcs7.ContactInfo = "contact@example.com";
38+
pkcs7.Location = "Office";
39+
40+
System.Drawing.Rectangle signatureRect = new System.Drawing.Rectangle(100, 100, 200, 50);
41+
signer.Sign(1, true, signatureRect, pkcs7);
42+
signer.Save(outputPdf);
43+
44+
Console.WriteLine($"Signed PDF saved to '{outputPdf}'.");
45+
}
46+
catch (Exception ex)
47+
{
48+
Console.Error.WriteLine($"Error: {ex.Message}");
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)