Skip to content

Commit 0520b73

Browse files
committed
968996: Added proper code sample.
1 parent 9cb20a8 commit 0520b73

File tree

4 files changed

+98
-157
lines changed

4 files changed

+98
-157
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,51 @@
1-
<!-- template.html -->
1+
<!DOCTYPE html>
22
<html>
33
<head>
4-
<meta charset="utf-8">
5-
<style>
6-
body {
7-
font-size: 18px; /* Increase base font size */
8-
min-height: 100vh;
9-
margin: 0;
10-
display: flex;
11-
align-items: center;
12-
justify-content: center;
13-
background: #fff; /* Set background to white */
14-
font-family: Arial, sans-serif;
15-
}
16-
.center-rectangle {
17-
background: #fff;
18-
border: 2.5px solid #313163;
19-
border-radius: 12px;
20-
padding: 48px 62px 46px 62px;
21-
min-width: 420px;
22-
max-width: 650px;
23-
box-shadow: 0 4px 30px rgba(49,49,99,0.10);
24-
display: flex;
25-
flex-direction: column;
26-
align-items: center;
27-
}
28-
.center-rectangle h1 {
29-
font-size: 2.8em; /* Big headline */
30-
font-weight: 800;
31-
margin-top: 0;
32-
margin-bottom: 38px;
33-
color: #313163;
34-
letter-spacing: 1.4px;
35-
}
36-
.center-rectangle p {
37-
font-size: 1.5em; /* Larger paragraph text */
38-
margin: 22px 0 0 0;
39-
width: 100%;
40-
text-align: left;
41-
color: #222;
42-
font-weight: 500;
43-
letter-spacing: 0.7px;
44-
}
45-
.center-rectangle p:last-child {
46-
margin-top: 56px;
47-
text-align: center;
48-
color: #757575;
49-
font-size: 1.23em;
50-
font-weight: 400;
51-
}
52-
</style>
4+
<title>Fillable PDF Form</title>
5+
<style>
6+
body {
7+
font-family: Arial, sans-serif;
8+
margin: 0;
9+
padding: 0;
10+
}
11+
.form-container {
12+
width: 400px;
13+
margin: 100px auto;
14+
padding: 30px;
15+
border-radius: 10px;
16+
box-shadow: 0 0 10px rgba(0,0,0,0.1);
17+
}
18+
label {
19+
display: block;
20+
margin-top: 15px;
21+
}
22+
input, select {
23+
width: 100%;
24+
padding: 8px;
25+
margin-top: 5px;
26+
box-sizing: border-box;
27+
}
28+
</style>
5329
</head>
5430
<body>
55-
<div class="center-rectangle">
56-
<h1>Welcome, {{name}}</h1>
57-
<p>Date: {{date}}</p>
58-
<p>Signature: {{signature}}</p>
59-
<p>Thank you for using our service.</p>
60-
</div>
31+
<div class="form-container">
32+
<h2>Job Application Form</h2>
33+
<form>
34+
<label for="name">Name:</label>
35+
<input type="text" name="name" />
36+
37+
<label for="email">Email:</label>
38+
<input type="email" name="email" />
39+
40+
<label for="gender">Gender:</label>
41+
<select name="gender">
42+
<option value="male">Male</option>
43+
<option value="female">Female</option>
44+
</select>
45+
46+
<label for="signature">Signature:</label>
47+
<input type="text" name="signature" />
48+
</form>
49+
</div>
6150
</body>
6251
</html>
Lines changed: 53 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,64 @@
1-
using Syncfusion.HtmlConverter;
1+
using Syncfusion.Drawing;
2+
using Syncfusion.HtmlConverter;
23
using Syncfusion.Pdf;
3-
using Syncfusion.Pdf.Graphics;
44
using Syncfusion.Pdf.Interactive;
55
using Syncfusion.Pdf.Parsing;
6-
using Syncfusion.Pdf.Redaction;
7-
using Syncfusion.Pdf.Security;
86

9-
class Program
7+
// Initialize the HTML to PDF converter
8+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
9+
// Enable form conversion
10+
BlinkConverterSettings settings = new BlinkConverterSettings
1011
{
11-
static void Main(string[] args)
12-
{
13-
// Initialize HTML to PDF converter and load HTML
14-
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
15-
string htmlFilePath = Path.GetFullPath(@"Data/Input.html");
16-
PdfDocument document = htmlConverter.Convert(htmlFilePath);
12+
EnableForm = true
13+
};
14+
htmlConverter.ConverterSettings = settings;
15+
// Convert the HTML file to a PDF document
16+
using (PdfDocument document = htmlConverter.Convert(Path.GetFullPath(@"Data/Input.html")))
17+
{
18+
// Set default appearance for form fields
19+
document.Form.SetDefaultAppearance(false);
1720

18-
// Save the PDF to a memory stream
19-
using (MemoryStream memoryStream = new MemoryStream())
21+
// Save the PDF document to a memory stream
22+
using (MemoryStream stream = new MemoryStream())
23+
{
24+
// Load the saved PDF document
25+
document.Save(stream);
26+
// Load the PDF document containing form fields
27+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
28+
// Fill the form fields
29+
PdfLoadedForm form = loadedDocument.Form;
30+
// Fill the "name" field
31+
if (form.Fields["name"] is PdfLoadedTextBoxField nameField)
2032
{
21-
document.Save(memoryStream);
22-
document.Close(true);
23-
24-
// Load back the PDF for further processing
25-
memoryStream.Position = 0;
26-
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(memoryStream);
27-
28-
// This will collect (pageIndex, word) for each form field
29-
List<(int pageIdx, TextWord word)> fieldData = new List<(int pageIdx, TextWord word)>();
30-
31-
// Pass 1: Locate each placeholder and add a redaction on its bound
32-
for (int i = 0; i < loadedDocument.Pages.Count; i++)
33-
{
34-
PdfLoadedPage page = loadedDocument.Pages[i] as PdfLoadedPage;
35-
page.ExtractText(out TextLineCollection lines);
36-
if (lines == null) continue;
37-
foreach (TextLine line in lines.TextLine)
38-
{
39-
foreach (TextWord word in line.WordCollection)
40-
{
41-
if (word == null) continue;
42-
if (word.Text == "{{name}}" ||
43-
word.Text == "{{date}}" ||
44-
word.Text == "{{signature}}")
45-
{
46-
page.AddRedaction(new PdfRedaction(word.Bounds));
47-
fieldData.Add((i, word));
48-
}
49-
}
50-
}
51-
}
52-
loadedDocument.Redact();
53-
54-
// Pass 2: Add form fields exactly over the bounds
55-
foreach (var (pageIdx, word) in fieldData)
56-
{
57-
PdfPageBase page = loadedDocument.Pages[pageIdx];
58-
59-
if (word.Text == "{{name}}")
60-
{
61-
PdfTextBoxField textBox = new PdfTextBoxField(page, "FirstName")
62-
{
63-
Bounds = word.Bounds,
64-
ToolTip = "First Name",
65-
Text = "John"
66-
};
67-
loadedDocument.Form.Fields.Add(textBox);
68-
}
69-
else if (word.Text == "{{date}}")
70-
{
71-
PdfTextBoxField dateField = new PdfTextBoxField(page, "DateField")
72-
{
73-
Bounds = word.Bounds
74-
};
75-
dateField.Actions.KeyPressed = new PdfJavaScriptAction("AFDate_KeystrokeEx(\"m/d/yy\")");
76-
dateField.Actions.Format = new PdfJavaScriptAction("AFDate_FormatEx(\"m/d/yy\")");
77-
loadedDocument.Form.Fields.Add(dateField);
78-
}
79-
else if (word.Text == "{{signature}}")
80-
{
81-
PdfSignatureField sigField = new PdfSignatureField(page, "SignatureField")
82-
{
83-
Bounds = word.Bounds,
84-
Signature = new PdfSignature()
85-
};
86-
// Optionally draw a signature image in the field area
87-
FileStream imageStream = new FileStream(Path.GetFullPath("Data/signature.png"), FileMode.Open, FileAccess.Read);
88-
PdfBitmap image = new PdfBitmap(imageStream);
89-
(page as PdfLoadedPage).Graphics.DrawImage(image, word.Bounds);
90-
imageStream.Dispose();
91-
92-
// Optional: add digital certificate
93-
using (FileStream certStream = new FileStream(Path.GetFullPath(@"Data/PDF.pfx"), FileMode.Open, FileAccess.Read))
94-
{
95-
sigField.Signature.Certificate = new PdfCertificate(certStream, "syncfusion");
96-
sigField.Signature.Reason = "I am author of this document";
97-
}
98-
loadedDocument.Form.Fields.Add(sigField);
99-
}
100-
}
101-
//Create file stream.
102-
using (FileStream outputFileStream = new FileStream(Path.GetFullPath(@"Output/Output.pdf"), FileMode.Create, FileAccess.ReadWrite))
33+
nameField.Text = "XYZ";
34+
}
35+
// Fill the "email" field
36+
if (form.Fields["email"] is PdfLoadedTextBoxField emailField)
37+
{
38+
emailField.Text = "[email protected]";
39+
}
40+
// Select "Male" in the "gender" dropdown
41+
if (form.Fields["gender"] is PdfLoadedComboBoxField genderField)
42+
{
43+
genderField.SelectedValue = "Male";
44+
}
45+
// Fill the "signature" field
46+
if (form.Fields["signature"] is PdfLoadedTextBoxField signatureTextBox)
47+
{
48+
// Get the original field's position and page
49+
RectangleF bounds = signatureTextBox.Bounds;
50+
PdfPageBase page = signatureTextBox.Page;
51+
// Remove the original textbox field
52+
form.Fields.Remove(signatureTextBox);
53+
// Create a new signature field at the same location
54+
PdfSignatureField signatureField = new PdfSignatureField(page, "ClientSignature")
10355
{
104-
//Save the PDF document to file stream.
105-
loadedDocument.Save(outputFileStream);
106-
}
107-
108-
//Close the document.
109-
loadedDocument.Close(true);
56+
Bounds = bounds
57+
};
58+
// Add the new signature field to the form
59+
form.Fields.Add(signatureField);
11060
}
61+
// Save the PDF document
62+
loadedDocument.Save(Path.GetFullPath(@"Output/Output2.pdf"));
11163
}
11264
}

0 commit comments

Comments
 (0)