Skip to content

Commit 1037c39

Browse files
Merge pull request #346 from SyncfusionExamples/ES-944926-Add-signature-in-PDF-from-Word
ES-944926- Add-signature-in-PDF-from-Word
2 parents a2186e5 + abeb259 commit 1037c39

File tree

5 files changed

+112
-0
lines changed

5 files changed

+112
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35527.113 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Add-signature-in-PDF-from-Word", "Add-signature-in-PDF-from-Word\Add-signature-in-PDF-from-Word.csproj", "{A854FD4A-9DB1-440B-92DB-0E7C33CABDA3}"
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+
{A854FD4A-9DB1-440B-92DB-0E7C33CABDA3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{A854FD4A-9DB1-440B-92DB-0E7C33CABDA3}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{A854FD4A-9DB1-440B-92DB-0E7C33CABDA3}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{A854FD4A-9DB1-440B-92DB-0E7C33CABDA3}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Add_signature_in_PDF_from_Word</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.DocIORenderer.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Data\Template.docx">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="Output\.gitkeep">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
</ItemGroup>
23+
24+
</Project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
using Syncfusion.DocIO.DLS;
2+
using Syncfusion.DocIO;
3+
using Syncfusion.DocIORenderer;
4+
using Syncfusion.Pdf;
5+
using Syncfusion.Pdf.Interactive;
6+
using Syncfusion.Pdf.Parsing;
7+
using Syncfusion.Drawing;
8+
9+
using (FileStream docStream = new FileStream(Path.GetFullPath(@"Data/Template.docx"), FileMode.Open, FileAccess.Read))
10+
{
11+
using (WordDocument document = new WordDocument(docStream, FormatType.Docx))
12+
{
13+
using (DocIORenderer renderer = new DocIORenderer())
14+
{
15+
//Sets true to preserve the Word document form field as editable PDF form field in PDF document.
16+
renderer.Settings.PreserveFormFields = true;
17+
18+
using (PdfDocument pdfDocument = renderer.ConvertToPDF(document))
19+
{
20+
using (MemoryStream stream = new MemoryStream())
21+
{
22+
pdfDocument.Save(stream);
23+
stream.Position = 0;
24+
//Add signature field in PDF.
25+
AddSignature(stream);
26+
}
27+
}
28+
}
29+
}
30+
}
31+
32+
/// <summary>
33+
/// Adds signature field in PDF
34+
/// </summary>
35+
/// <param name="stream"></param>
36+
static void AddSignature(MemoryStream stream)
37+
{
38+
//Load the PDF document.
39+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(stream);
40+
//Get the loaded form.
41+
PdfLoadedForm loadedForm = loadedDocument.Form;
42+
for (int i = 0; i < loadedForm.Fields.Count; i++)
43+
{
44+
if (loadedForm.Fields[i] is PdfLoadedTextBoxField)
45+
{
46+
//Get the loaded text box field and fill it.
47+
PdfLoadedTextBoxField loadedTextBoxField = loadedForm.Fields[i] as PdfLoadedTextBoxField;
48+
//Get bounds from an existing textbox field.
49+
RectangleF bounds = loadedTextBoxField.Bounds;
50+
//Get page.
51+
PdfPageBase loadedPage = loadedTextBoxField.Page;
52+
//Create PDF Signature field.
53+
PdfSignatureField signatureField = new PdfSignatureField(loadedPage, loadedTextBoxField.Text.Trim());
54+
//Set properties to the signature field.
55+
signatureField.Bounds = bounds;
56+
//Add the form field to the document.
57+
loadedDocument.Form.Fields.Add(signatureField);
58+
}
59+
}
60+
//Save the document.
61+
using (FileStream outputStream1 = new FileStream(Path.GetFullPath(@"Output/Result.pdf"), FileMode.OpenOrCreate, FileAccess.ReadWrite))
62+
{
63+
loadedDocument.Save(outputStream1);
64+
}
65+
}

0 commit comments

Comments
 (0)