Skip to content

Commit 4dbd9eb

Browse files
Add 90 example(s) for facades-forms
1 parent 9c614e3 commit 4dbd9eb

92 files changed

Lines changed: 6313 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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Forms; // for FieldType, PKCS1
6+
7+
class Program
8+
{
9+
static void Main()
10+
{
11+
// Input and output files
12+
const string inputPdf = "input.pdf";
13+
const string outputPdf = "signed_output.pdf";
14+
const string certFile = "certificate.pfx";
15+
const string certPass = "password";
16+
const string appearance = "signature_appearance.png";
17+
18+
// Verify that required files exist
19+
if (!File.Exists(inputPdf))
20+
{
21+
Console.Error.WriteLine($"Input PDF not found: {inputPdf}");
22+
return;
23+
}
24+
if (!File.Exists(certFile))
25+
{
26+
Console.Error.WriteLine($"Certificate file not found: {certFile}");
27+
return;
28+
}
29+
if (!File.Exists(appearance))
30+
{
31+
Console.Error.WriteLine($"Signature appearance image not found: {appearance}");
32+
return;
33+
}
34+
35+
// Load the PDF document
36+
using (Document doc = new Document(inputPdf))
37+
{
38+
// Add a signature field named "DigitalSignature" on page 1
39+
// Rectangle coordinates: lower‑left (100,100), upper‑right (200,150)
40+
using (FormEditor formEditor = new FormEditor(doc))
41+
{
42+
bool fieldAdded = formEditor.AddField(
43+
FieldType.Signature, // field type
44+
"DigitalSignature", // field name
45+
1, // page number (1‑based)
46+
100f, 100f, // llx, lly
47+
200f, 150f); // urx, ury
48+
49+
if (!fieldAdded)
50+
{
51+
Console.Error.WriteLine("Failed to add signature field.");
52+
return;
53+
}
54+
55+
// Persist the added field into the document
56+
formEditor.Save();
57+
}
58+
59+
// Prepare the signature object (PKCS1)
60+
PKCS1 pkcs1Signature = new PKCS1(certFile, certPass)
61+
{
62+
Reason = "Document approved",
63+
ContactInfo = "contact@example.com",
64+
Location = "Office"
65+
};
66+
67+
// Sign the document using the newly created field
68+
using (PdfFileSignature pdfSigner = new PdfFileSignature())
69+
{
70+
// Bind the in‑memory document to the signer
71+
pdfSigner.BindPdf(doc);
72+
73+
// Set visual appearance (image) for the signature
74+
pdfSigner.SignatureAppearance = appearance;
75+
76+
// Set the certificate (required for signing)
77+
pdfSigner.SetCertificate(certFile, certPass);
78+
79+
// Sign the field by its name
80+
pdfSigner.Sign("DigitalSignature", pkcs1Signature);
81+
82+
// Save the signed PDF
83+
pdfSigner.Save(outputPdf);
84+
}
85+
86+
Console.WriteLine($"Signed PDF saved to '{outputPdf}'.");
87+
}
88+
}
89+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Forms;
6+
using Aspose.Pdf.Annotations;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
const string inputPdf = "input.pdf"; // PDF containing a text field named "Email"
13+
const string outputPdf = "output.pdf";
14+
15+
if (!File.Exists(inputPdf))
16+
{
17+
Console.Error.WriteLine($"File not found: {inputPdf}");
18+
return;
19+
}
20+
21+
// JavaScript that validates the email format on the blur (focus loss) event.
22+
// It shows an alert if the entered value does not match a simple email pattern.
23+
string jsCode = @"
24+
if (event.value.match(/^\\S+@\\S+\\.\\S+$/) == null) {
25+
app.alert('Invalid email address');
26+
}";
27+
28+
// FormEditor is a Facade class used to edit form fields.
29+
using (FormEditor formEditor = new FormEditor())
30+
{
31+
// Bind the existing PDF document.
32+
formEditor.BindPdf(inputPdf);
33+
34+
// Retrieve the field named "Email". The Document property gives access to the core PDF object.
35+
// The Form collection is indexed by the full field name.
36+
// The collection returns a WidgetAnnotation, which can be cast to Field.
37+
Field emailField = formEditor.Document.Form["Email"] as Field;
38+
if (emailField == null)
39+
{
40+
Console.Error.WriteLine("Field 'Email' not found in the PDF.");
41+
return;
42+
}
43+
44+
// Assign the JavaScript to the OnLostFocus (blur) action of the field.
45+
emailField.Actions.OnLostFocus = new JavascriptAction(jsCode);
46+
47+
// Save the modified PDF.
48+
formEditor.Save(outputPdf);
49+
}
50+
51+
Console.WriteLine($"PDF saved with JavaScript validation: {outputPdf}");
52+
}
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
6+
class Program
7+
{
8+
static void Main()
9+
{
10+
const string inputPdf = "input.pdf";
11+
const string outputPdf = "output.pdf";
12+
13+
if (!File.Exists(inputPdf))
14+
{
15+
Console.Error.WriteLine($"File not found: {inputPdf}");
16+
return;
17+
}
18+
19+
// Load the PDF document first – FormEditor expects a Document instance, not a file path.
20+
using (Document pdfDocument = new Document(inputPdf))
21+
using (FormEditor formEditor = new FormEditor(pdfDocument))
22+
{
23+
// JavaScript that validates the field value against a simple e‑mail regex.
24+
// If the value does not match, an alert is shown and the validation fails (event.rc = false).
25+
string emailValidationScript = @"
26+
if (!/^[\w\.\-]+@([\w\-]+\.)+[\w\-]{2,4}$/.test(event.value)) {
27+
app.alert('Invalid email address.');
28+
event.rc = false;
29+
}";
30+
// Attach the script to the "Email" field. This script runs on the field's Validate event.
31+
formEditor.SetFieldScript("Email", emailValidationScript);
32+
33+
// Save the modified PDF
34+
formEditor.Save(outputPdf);
35+
}
36+
37+
Console.WriteLine($"Email field validation added. Saved to '{outputPdf}'.");
38+
}
39+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using Aspose.Pdf;
3+
using Aspose.Pdf.Facades;
4+
5+
class Program
6+
{
7+
static void Main()
8+
{
9+
// Paths for the source PDF (must exist) and the resulting PDF
10+
const string inputPdf = "input.pdf";
11+
const string outputPdf = "output.pdf";
12+
13+
// Ensure the source file exists
14+
if (!System.IO.File.Exists(inputPdf))
15+
{
16+
Console.Error.WriteLine($"Source PDF not found: {inputPdf}");
17+
return;
18+
}
19+
20+
// FormEditor loads the source PDF and prepares the output file
21+
using (FormEditor formEditor = new FormEditor(inputPdf, outputPdf))
22+
{
23+
// Define the radio button options
24+
formEditor.Items = new string[] { "Male", "Female", "Other" };
25+
26+
// Arrange the radios horizontally (default is true, set explicitly for clarity)
27+
formEditor.RadioHoriz = true;
28+
29+
// Optional: set the gap between radio buttons (pixels)
30+
formEditor.RadioGap = 10;
31+
32+
// Add the radio button group:
33+
// - FieldType.Radio indicates a radio button group
34+
// - "Gender" is the field name
35+
// - "Male" is the default selected option
36+
// - Page number is 1 (Aspose.Pdf uses 1‑based indexing)
37+
// - llx, lly, urx, ury define the rectangle that contains the group
38+
formEditor.AddField(FieldType.Radio, "Gender", "Male", 1, 100, 700, 200, 720);
39+
40+
// Persist the changes to the output PDF
41+
formEditor.Save();
42+
}
43+
44+
Console.WriteLine($"Radio button group 'Gender' added and saved to '{outputPdf}'.");
45+
}
46+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
using System;
2+
using System.IO;
3+
using System.Security.Cryptography;
4+
using Aspose.Pdf;
5+
using Aspose.Pdf.Facades;
6+
using Aspose.Pdf.Forms;
7+
8+
class Program
9+
{
10+
static void Main()
11+
{
12+
const string inputPath = "input.pdf";
13+
const string outputPath = "output_with_token.pdf";
14+
15+
if (!File.Exists(inputPath))
16+
{
17+
Console.Error.WriteLine($"Input file not found: {inputPath}");
18+
return;
19+
}
20+
21+
// Generate a secure random token (32 bytes, Base64 encoded)
22+
string authToken;
23+
using (RandomNumberGenerator rng = RandomNumberGenerator.Create())
24+
{
25+
byte[] tokenBytes = new byte[32];
26+
rng.GetBytes(tokenBytes);
27+
authToken = Convert.ToBase64String(tokenBytes);
28+
}
29+
30+
// Load the PDF using the Facades Form class (fully qualified to avoid ambiguity)
31+
using (Aspose.Pdf.Facades.Form pdfForm = new Aspose.Pdf.Facades.Form())
32+
{
33+
pdfForm.BindPdf(inputPath); // Initialize the facade with the source PDF
34+
Document doc = pdfForm.Document; // Access the underlying Document object
35+
36+
// Ensure the document has at least one page
37+
if (doc.Pages.Count == 0)
38+
{
39+
Console.Error.WriteLine("The PDF has no pages.");
40+
return;
41+
}
42+
43+
// Create a hidden text field named "AuthToken" on the first page
44+
Page firstPage = doc.Pages[1]; // 1‑based indexing
45+
// Rectangle with zero size; field will not be visible
46+
Aspose.Pdf.Rectangle rect = new Aspose.Pdf.Rectangle(0, 0, 0, 0);
47+
TextBoxField hiddenField = new TextBoxField(firstPage, rect)
48+
{
49+
PartialName = "AuthToken",
50+
Value = authToken
51+
// No need to set Flags; zero‑size rectangle makes the field invisible
52+
};
53+
54+
// Add the field to the document's form
55+
doc.Form.Add(hiddenField);
56+
57+
// Save the modified PDF via the Form facade
58+
pdfForm.Save(outputPath);
59+
}
60+
61+
Console.WriteLine($"Hidden field \"AuthToken\" added and saved to '{outputPath}'.");
62+
}
63+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 = "output.pdf";
11+
12+
if (!File.Exists(inputPath))
13+
{
14+
Console.Error.WriteLine($"Input file not found: {inputPath}");
15+
return;
16+
}
17+
18+
// Initialize FormEditor and bind the existing PDF
19+
using (FormEditor formEditor = new FormEditor())
20+
{
21+
formEditor.BindPdf(inputPath);
22+
23+
// Add a hidden numeric field named "Version" with initial value "2"
24+
// Placed at a zero‑size rectangle on page 1 (effectively invisible)
25+
formEditor.AddField(FieldType.Numeric, "Version", "2", 1, 0, 0, 0, 0);
26+
27+
// No need to set a hidden flag – the zero‑size rectangle makes the field invisible.
28+
29+
// Save the modified PDF
30+
formEditor.Save(outputPath);
31+
}
32+
33+
Console.WriteLine($"Hidden field added and saved to '{outputPath}'.");
34+
}
35+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using System;
2+
using System.IO;
3+
using Aspose.Pdf;
4+
using Aspose.Pdf.Facades;
5+
using Aspose.Pdf.Annotations;
6+
7+
class BatchAddHiddenSessionId
8+
{
9+
static void Main()
10+
{
11+
// Base directory of the running application (works on Windows, Linux, macOS)
12+
string baseDir = AppDomain.CurrentDomain.BaseDirectory;
13+
14+
// Resolve input and output folders relative to the base directory
15+
string inputFolder = Path.Combine(baseDir, "InputPdfs");
16+
string outputFolder = Path.Combine(baseDir, "OutputPdfs");
17+
18+
// Ensure the folders exist (creates them if they are missing)
19+
Directory.CreateDirectory(inputFolder);
20+
Directory.CreateDirectory(outputFolder);
21+
22+
// Process each PDF file in the input folder
23+
foreach (string inputPath in Directory.GetFiles(inputFolder, "*.pdf"))
24+
{
25+
string fileName = Path.GetFileName(inputPath);
26+
string outputPath = Path.Combine(outputFolder, fileName);
27+
28+
// Load the PDF document
29+
using (Document doc = new Document(inputPath))
30+
{
31+
// -----------------------------------------------------------------
32+
// 1. Add a hidden text field named "SessionId" on the first page.
33+
// The field rectangle is set to zero size (0,0,0,0) to keep it
34+
// invisible, and the AnnotationFlags.Hidden flag is applied.
35+
// -----------------------------------------------------------------
36+
using (FormEditor editor = new FormEditor(doc))
37+
{
38+
// Add a text field with zero‑size rectangle on page 1
39+
editor.AddField(FieldType.Text, "SessionId", 1, 0, 0, 0, 0);
40+
41+
// Mark the field as hidden
42+
editor.SetFieldAppearance("SessionId", AnnotationFlags.Hidden);
43+
}
44+
45+
// -----------------------------------------------------------------
46+
// 2. Fill the newly created field with a generated GUID value.
47+
// -----------------------------------------------------------------
48+
using (Form form = new Form(doc))
49+
{
50+
string guidValue = Guid.NewGuid().ToString();
51+
form.FillField("SessionId", guidValue);
52+
}
53+
54+
// -----------------------------------------------------------------
55+
// 3. Save the modified document to the output folder.
56+
// -----------------------------------------------------------------
57+
doc.Save(outputPath);
58+
Console.WriteLine($"Processed: {fileName}");
59+
}
60+
}
61+
62+
Console.WriteLine("Batch processing completed.");
63+
}
64+
}

0 commit comments

Comments
 (0)