Skip to content

Commit 16c3af5

Browse files
authored
Merge branch 'release/26.4.0' into examples/2cab5d23-facades-extract-images-and-tex
2 parents 9ca8977 + 32d413d commit 16c3af5

83 files changed

Lines changed: 6666 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
using System;
2+
using System.Drawing;
3+
using System.IO;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main(string[] args)
9+
{
10+
// Expect two arguments: input PDF path and signature image path
11+
if (args.Length < 2)
12+
{
13+
Console.Error.WriteLine("Usage: <app> <input-pdf> <signature-image>");
14+
return;
15+
}
16+
17+
string pdfPath = args[0];
18+
string imagePath = args[1];
19+
20+
if (!File.Exists(pdfPath))
21+
{
22+
Console.Error.WriteLine($"Error: PDF file not found: {pdfPath}");
23+
return;
24+
}
25+
26+
if (!File.Exists(imagePath))
27+
{
28+
Console.Error.WriteLine($"Error: Signature image not found: {imagePath}");
29+
return;
30+
}
31+
32+
// Output file will be placed alongside the input file with "_signed.pdf" suffix
33+
string outputPath = Path.Combine(
34+
Path.GetDirectoryName(pdfPath) ?? string.Empty,
35+
Path.GetFileNameWithoutExtension(pdfPath) + "_signed.pdf");
36+
37+
try
38+
{
39+
// Initialize the PdfFileSignature facade
40+
using (PdfFileSignature pdfSign = new PdfFileSignature())
41+
{
42+
// Bind the source PDF
43+
pdfSign.BindPdf(pdfPath);
44+
45+
// Set the visual appearance of the signature (image)
46+
pdfSign.SignatureAppearance = imagePath;
47+
48+
// Define signature properties
49+
int pageNumber = 1; // first page (1‑based indexing)
50+
string reason = "Document signed";
51+
string contact = "contact@example.com";
52+
string location = "Location";
53+
bool visible = true;
54+
55+
// Define the rectangle where the signature will appear
56+
// Rectangle(x, y, width, height) – coordinates are in points (1/72 inch)
57+
Rectangle rect = new Rectangle(100, 100, 200, 100);
58+
59+
// Apply the signature
60+
pdfSign.Sign(pageNumber, reason, contact, location, visible, rect);
61+
62+
// Save the signed PDF
63+
pdfSign.Save(outputPath);
64+
}
65+
66+
Console.WriteLine($"Signed PDF saved to: {outputPath}");
67+
}
68+
catch (Exception ex)
69+
{
70+
Console.Error.WriteLine($"Error during signing: {ex.Message}");
71+
}
72+
}
73+
}
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.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
const string inputPdf = "input.pdf"; // source PDF (already contains a signature)
10+
const string outputPdf = "signed_output.pdf"; // result PDF with the second signature
11+
const string certPath = "certificate.pfx"; // signing certificate
12+
const string certPassword = "password"; // certificate password
13+
const string appearanceImg = "signature.png"; // optional visual appearance for the signature
14+
15+
// Validate required files
16+
if (!File.Exists(inputPdf))
17+
{
18+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
19+
return;
20+
}
21+
if (!File.Exists(certPath))
22+
{
23+
Console.Error.WriteLine($"Certificate file not found: {certPath}");
24+
return;
25+
}
26+
27+
// Use PdfFileSignature facade to add a second digital signature on page 3
28+
using (PdfFileSignature pdfSign = new PdfFileSignature())
29+
{
30+
// Bind the existing PDF document
31+
pdfSign.BindPdf(inputPdf);
32+
33+
// Configure signing certificate and optional appearance image
34+
pdfSign.SetCertificate(certPath, certPassword);
35+
pdfSign.SignatureAppearance = appearanceImg; // can be omitted if no visual stamp is needed
36+
37+
// Define the rectangle area for the new signature (x, y, width, height) in points
38+
System.Drawing.Rectangle rect = new System.Drawing.Rectangle(100, 100, 200, 50);
39+
40+
// Add the second signature on page 3
41+
pdfSign.Sign(
42+
page: 3, // target page (1‑based indexing)
43+
SigReason: "Second signature",
44+
SigContact: "contact@example.com",
45+
SigLocation: "New York",
46+
visible: true, // make the signature visible
47+
annotRect: rect); // rectangle where the signature will appear
48+
49+
// Save the signed PDF to a new file
50+
pdfSign.Save(outputPdf);
51+
}
52+
53+
Console.WriteLine($"Second digital signature added and saved to '{outputPdf}'.");
54+
}
55+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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+
// Input PDF, output PDF and certificate details
12+
const string inputPdf = "input.pdf";
13+
const string outputPdf = "signed_output.pdf";
14+
const string certPath = "certificate.pfx";
15+
const string certPass = "password";
16+
17+
// Verify input files exist
18+
if (!File.Exists(inputPdf))
19+
{
20+
Console.Error.WriteLine($"Input file not found: {inputPdf}");
21+
return;
22+
}
23+
if (!File.Exists(certPath))
24+
{
25+
Console.Error.WriteLine($"Certificate file not found: {certPath}");
26+
return;
27+
}
28+
29+
// Define the signature field rectangle (llx, lly, urx, ury)
30+
// Adjust these coordinates to the desired location on the page
31+
Aspose.Pdf.Rectangle sigRect = new Aspose.Pdf.Rectangle(100, 100, 300, 150);
32+
33+
// Name of the signature field
34+
const string fieldName = "Signature1";
35+
36+
// Load the PDF, add a signature field, and sign it
37+
using (Document doc = new Document(inputPdf))
38+
{
39+
// Create a signature field on the first page (page index is 1‑based)
40+
SignatureField sigField = new SignatureField(doc, sigRect);
41+
sigField.PartialName = fieldName; // set the field name
42+
43+
// Add the field to the document's form collection
44+
doc.Form.Add(sigField);
45+
46+
// Prepare the signature object (PKCS#1 in this example)
47+
PKCS1 signature = new PKCS1(certPath, certPass);
48+
signature.Reason = "Approved";
49+
signature.ContactInfo = "signer@example.com";
50+
signature.Location = "New York";
51+
52+
// Use PdfFileSignature facade to apply the signature to the created field
53+
PdfFileSignature pdfSigner = new PdfFileSignature();
54+
pdfSigner.BindPdf(doc); // bind the in‑memory document
55+
pdfSigner.SetCertificate(certPath, certPass);
56+
// Optional: set an image to appear as the visual signature
57+
// pdfSigner.SignatureAppearance = "signature_image.jpg";
58+
59+
// Sign the document using the field name; rectangle is taken from the field itself
60+
pdfSigner.Sign(fieldName, signature);
61+
62+
// Save the signed PDF
63+
pdfSigner.Save(outputPdf);
64+
}
65+
66+
Console.WriteLine($"Signed PDF saved to '{outputPdf}'.");
67+
}
68+
}

facades-sign-documents/agents.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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;` (34/34 files) ← category-specific
25+
- `using Aspose.Pdf.Forms;` (12/34 files)
26+
- `using Aspose.Pdf;` (10/34 files)
27+
- `using Aspose.Pdf.Text;` (1/34 files)
28+
- `using System;` (34/34 files)
29+
- `using System.IO;` (30/34 files)
30+
- `using System.Drawing;` (11/34 files)
31+
- `using System.Collections.Generic;` (3/34 files)
32+
- `using System.Globalization;` (2/34 files)
33+
- `using System.Security.Cryptography.X509Certificates;` (2/34 files)
34+
- `using System.Drawing.Imaging;` (1/34 files)
35+
- `using System.Linq;` (1/34 files)
36+
- `using System.Text;` (1/34 files)
37+
- `using System.Text.Json;` (1/34 files)
38+
- `using System.Threading;` (1/34 files)
39+
40+
## Files in this folder
41+
42+
| File | Title | Key APIs | Description |
43+
|------|-------|----------|-------------|
44+
| [add-image-signature-to-pdf](./add-image-signature-to-pdf.cs) | Add Image Signature to PDF | `PdfFileSignature`, `BindPdf`, `SignatureAppearance` | Demonstrates how to sign a PDF document with a visible image signature using Aspose.Pdf.Facades.P... |
45+
| [add-second-digital-signature-to-pdf](./add-second-digital-signature-to-pdf.cs) | Add a Second Digital Signature to a PDF Page | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Shows how to apply a second digital signature on page three of an existing PDF using Aspose.Pdf.F... |
46+
| [add-signature-field-and-sign-pdf](./add-signature-field-and-sign-pdf.cs) | Add Signature Field and Sign PDF | `Document`, `SignatureField`, `PKCS1` | Demonstrates how to create a signature field on a specific page of a PDF and then digitally sign ... |
47+
| [check-absence-of-signatures-in-pdf](./check-absence-of-signatures-in-pdf.cs) | Check for Absence of Signatures in PDF | `PdfFileSignature`, `BindPdf`, `ContainsSignature` | Demonstrates using Aspose.Pdf.Facades.PdfFileSignature to confirm that a PDF contains no signatur... |
48+
| [custom-pdf-signature-appearance-no-caption](./custom-pdf-signature-appearance-no-caption.cs) | Create Custom PDF Signature Appearance Without Default Capti... | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to sign a PDF with a visible signature using Aspose.Pdf and hide the default "Di... |
49+
| [digitally-sign-first-page-pdf](./digitally-sign-first-page-pdf.cs) | Digitally Sign First Page of PDF | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Shows how to create a PdfFileSignature, bind a PDF, set a certificate, add a visible digital sign... |
50+
| [extract-certificate-details-from-pdf-signature-fie...](./extract-certificate-details-from-pdf-signature-fields.cs) | Extract Certificate Details from PDF Signature Fields | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Shows how to load a signed PDF, enumerate its signature fields, and retrieve the associated X.509... |
51+
| [extract-signature-certificate-serial-number](./extract-signature-certificate-serial-number.cs) | Extract and Log Signature Certificate Serial Number | `PdfFileSignature`, `BindPdf`, `ContainsSignature` | Shows how to open a PDF, verify it contains digital signatures, retrieve each signature's X.509 c... |
52+
| [extract-signature-images-html-report](./extract-signature-images-html-report.cs) | Extract Signature Images from PDF and Embed in HTML Report | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Shows how to use Aspose.Pdf.Facades.PdfFileSignature to extract digital signature images from a P... |
53+
| [extract-signing-certificate-from-pdf](./extract-signing-certificate-from-pdf.cs) | Extract Signing Certificate from PDF Signature Field | `Document`, `PdfFileSignature`, `BindPdf` | Shows how to load a PDF, bind it to PdfFileSignature, extract the signing certificate from the 'L... |
54+
| [extract-witness-signature-to-png](./extract-witness-signature-to-png.cs) | Extract Signature Image and Save as PNG | `PdfFileSignature`, `BindPdf`, `ExtractImage` | Demonstrates how to extract the image from a PDF signature field using Aspose.Pdf.Facades and con... |
55+
| [list-pdf-signature-reason-location](./list-pdf-signature-reason-location.cs) | List PDF Signature Reason and Location | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Shows how to open a PDF with Aspose.Pdf.Facades, retrieve all signature names, and extract each s... |
56+
| [list-signature-names-in-pdf](./list-signature-names-in-pdf.cs) | List Signature Names in a PDF | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Shows how to bind a PDF document to the PdfFileSignature facade and retrieve the names of all act... |
57+
| [locale-specific-pdf-signature](./locale-specific-pdf-signature.cs) | Locale‑Specific PDF Signature Appearance | `PdfFileSignature`, `PKCS7`, `SignatureCustomAppearance` | Demonstrates signing a PDF with Aspose.Pdf using a PKCS7 signature and customizing the visual app... |
58+
| [log-pdf-signature-verification-to-json](./log-pdf-signature-verification-to-json.cs) | Log PDF Signature Verification Results to JSON | `PdfFileSignature`, `BindPdf`, `ContainsSignature` | Demonstrates how to verify digital signatures in a PDF using Aspose.Pdf and write each signature'... |
59+
| [pdf-signature-report](./pdf-signature-report.cs) | Generate PDF Signature Verification Report | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Demonstrates how to read digital signatures from a PDF, verify each signature, and create a new P... |
60+
| [remove-all-signatures-from-pdf](./remove-all-signatures-from-pdf.cs) | Remove All Signatures from a PDF | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Demonstrates how to use Aspose.Pdf's PdfFileSignature facade to enumerate and delete every digita... |
61+
| [remove-all-signatures-from-pdfs](./remove-all-signatures-from-pdfs.cs) | Remove All Signatures from PDFs Recursively | `PdfFileSignature`, `BindPdf`, `RemoveSignatures` | Shows how to recursively find PDF files in a directory and use Aspose.Pdf.Facades to strip all di... |
62+
| [remove-pdf-signature-missing-field](./remove-pdf-signature-missing-field.cs) | Remove PDF Signature with Missing-Field Handling | `PdfFileSignature`, `BindPdf`, `GetSignatureNames` | Demonstrates how to remove a specific digital signature from a PDF using Aspose.Pdf, while gracef... |
63+
| [remove-signature-from-pdf](./remove-signature-from-pdf.cs) | Remove Signature from PDF | `PdfFileSignature`, `BindPdf`, `RemoveSignature` | Demonstrates how to load a PDF, remove a signature named 'ApprovalSignature' using Aspose.Pdf, an... |
64+
| [remove-signatures-re-sign-pdf](./remove-signatures-re-sign-pdf.cs) | Remove Existing Signatures and Re‑sign PDF with a New Certif... | `PdfFileSignature`, `BindPdf`, `RemoveSignatures` | Demonstrates how to clear all existing signatures from a PDF and apply a new digital signature us... |
65+
| [retry-pdf-signing-when-file-locked](./retry-pdf-signing-when-file-locked.cs) | Retry PDF Signing When File Is Locked | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to sign a PDF using Aspose.Pdf.Facades with a retry mechanism that handles file‑... |
66+
| [sign-and-verify-pdf-using-pdffilesignature](./sign-and-verify-pdf-using-pdffilesignature.cs) | Sign and Verify PDF Using PdfFileSignature | `PdfFileSignature`, `BindPdf`, `Sign` | Demonstrates how to digitally sign a PDF with a PKCS#1 certificate using PdfFileSignature and the... |
67+
| [sign-pdf-french-caption](./sign-pdf-french-caption.cs) | Sign PDF with French Caption Using SignatureCustomAppearance | `PdfFileSignature`, `PKCS1`, `SignatureCustomAppearance` | Demonstrates how to digitally sign a PDF and set the signature appearance caption to French by co... |
68+
| [sign-pdf-hidden-signature](./sign-pdf-hidden-signature.cs) | Sign PDF with Hidden Signature Appearance | `PdfFileSignature`, `BindPdf`, `Sign` | Demonstrates digitally signing a PDF using Aspose.Pdf while completely hiding the signature appea... |
69+
| [sign-pdf-semi-transparent-background](./sign-pdf-semi-transparent-background.cs) | Sign PDF with Semi-Transparent Background Appearance | `PdfFileSignature`, `PKCS7`, `SignatureCustomAppearance` | Demonstrates how to digitally sign a PDF using Aspose.Pdf and configure a custom signature appear... |
70+
| [sign-pdf-suppress-signature-text](./sign-pdf-suppress-signature-text.cs) | Sign PDF and Suppress Signature Text | `PdfFileSignature`, `BindPdf`, `SetCertificate` | Demonstrates how to digitally sign a PDF using Aspose.Pdf.Facades and hide the default location, ... |
71+
| [sign-pdf-visible-signature-last-page](./sign-pdf-visible-signature-last-page.cs) | Sign PDF with Visible Signature on Last Page | `Document`, `Page`, `PdfFileSignature` | Shows how to apply a visible digital signature to the bottom‑right corner of the last page of a P... |
72+
| [sign-pdf-with-custom-appearance](./sign-pdf-with-custom-appearance.cs) | Sign PDF with Custom Appearance (Foreground Image & Backgrou... | `PdfFileSignature`, `PKCS1`, `SignatureCustomAppearance` | Demonstrates how to configure a SignatureCustomAppearance with a foreground image and background ... |
73+
| [sign-pdf-with-pfx-certificate](./sign-pdf-with-pfx-certificate.cs) | Sign PDF with PFX Certificate using Aspose.Pdf | `Document`, `PdfFileSignature`, `SetCertificate` | Demonstrates how to digitally sign a PDF document using a password‑protected PFX certificate with... |
74+
| ... | | | *and 4 more files* |
75+
76+
## Category Statistics
77+
- Total examples: 34
78+
79+
## Category-Specific Tips
80+
81+
### Key API Surface
82+
- `Aspose.Pdf.Document`
83+
- `Aspose.Pdf.Facades.Algorithm`
84+
- `Aspose.Pdf.Facades.DocMDPAccessPermissions`
85+
- `Aspose.Pdf.Facades.DocMDPSignature`
86+
- `Aspose.Pdf.Facades.DocumentPrivilege`
87+
- `Aspose.Pdf.Facades.KeySize`
88+
- `Aspose.Pdf.Facades.PKCS7`
89+
- `Aspose.Pdf.Facades.PdfFileInfo`
90+
- `Aspose.Pdf.Facades.PdfFileSecurity`
91+
- `Aspose.Pdf.Facades.PdfFileSecurity.BindPdf`
92+
- `Aspose.Pdf.Facades.PdfFileSecurity.EncryptFile`
93+
- `Aspose.Pdf.Facades.PdfFileSecurity.Save`
94+
- `Aspose.Pdf.Facades.PdfFileSignature`
95+
- `Aspose.Pdf.Facades.PdfFileSignature.BindPdf`
96+
- `Aspose.Pdf.Facades.PdfFileSignature.Certify`
97+
98+
### Rules
99+
- Create a PdfFileInfo for {input_pdf} and read its IsEncrypted property to obtain a {bool} indicating whether the PDF is encrypted (password‑protected).
100+
- Use PdfFileInfo instead of loading a full Document when only document metadata such as encryption status is needed.
101+
- Instantiate PdfFileSecurity, call BindPdf({input_pdf}) to load the encrypted PDF, then invoke DecryptFile({string_literal}) with the owner password to decrypt it.
102+
- After decryption, call Save({output_pdf}) on the PdfFileSecurity instance to write the unprotected PDF to disk.
103+
- 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.
104+
105+
### Warnings
106+
- DecryptFile requires the owner password; decryption with only a user password is not covered by this example.
107+
- 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.
108+
- 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.
109+
- A valid PKCS#7 certificate file and correct password are required; otherwise signing will fail.
110+
- Custom appearance may not be rendered identically across all PDF viewers.
111+
112+
## General Tips
113+
- See parent [agents.md](../agents.md) for:
114+
- **Boundaries** — Always / Ask First / Never rules for all examples
115+
- **Common Mistakes** — verified anti-patterns that cause build failures
116+
- **Domain Knowledge** — cross-cutting API-specific gotchas
117+
- **Testing Guide** — build and run verification steps
118+
- Review code examples in this folder for facades-sign-documents patterns
119+
120+
<!-- AUTOGENERATED:START -->
121+
Updated: 2026-05-08 | Run: `20260508_144637_f72c91`
122+
<!-- AUTOGENERATED:END -->

0 commit comments

Comments
 (0)