Skip to content

Commit 06d309d

Browse files
Merge pull request #189 from SyncfusionExamples/968996
968996: Added filled form fields from HTML to PDF.
2 parents 3155ab5 + 0520b73 commit 06d309d

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.14.36221.1 d17.14
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Fillable-form-fields-from-HTML-to-PDF-Converter", "Fillable-form-fields-from-HTML-to-PDF-Converter\Fillable-form-fields-from-HTML-to-PDF-Converter.csproj", "{70A501A7-093A-4E77-AE84-1EDFE2A5243F}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{70A501A7-093A-4E77-AE84-1EDFE2A5243F}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {FC5888A1-82B1-4C5C-AA60-36BBCD924490}
24+
EndGlobalSection
25+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
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>
29+
</head>
30+
<body>
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>
50+
</body>
51+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Fillable_form_fields_from_HTML_to_PDF_Converter</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.HtmlToPdfConverter.Net.Windows" Version="*" />
13+
<PackageReference Include="Syncfusion.Pdf.Imaging.Net.Core" Version="*" />
14+
</ItemGroup>
15+
16+
</Project>

HTML to PDF/Blink/Fillable-form-fields-from-HTML-to-PDF-Converter/.NET/Fillable-form-fields-from-HTML-to-PDF-Converter/Output/gitkeep.txt

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
using Syncfusion.Drawing;
2+
using Syncfusion.HtmlConverter;
3+
using Syncfusion.Pdf;
4+
using Syncfusion.Pdf.Interactive;
5+
using Syncfusion.Pdf.Parsing;
6+
7+
// Initialize the HTML to PDF converter
8+
HtmlToPdfConverter htmlConverter = new HtmlToPdfConverter();
9+
// Enable form conversion
10+
BlinkConverterSettings settings = new BlinkConverterSettings
11+
{
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);
20+
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)
32+
{
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")
55+
{
56+
Bounds = bounds
57+
};
58+
// Add the new signature field to the form
59+
form.Fields.Add(signatureField);
60+
}
61+
// Save the PDF document
62+
loadedDocument.Save(Path.GetFullPath(@"Output/Output2.pdf"));
63+
}
64+
}

0 commit comments

Comments
 (0)