Skip to content

Commit cf248de

Browse files
authored
Merge pull request #52 from SyncfusionExamples/EJ2-973011-eSignPdf
897552: Sample on How to Create User Based eSign PDF Forms
2 parents 96a577a + d205f23 commit cf248de

File tree

80 files changed

+75568
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+75568
-0
lines changed
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Hosting;
8+
using Microsoft.Extensions.Caching.Memory;
9+
using Syncfusion.EJ2.PdfViewer;
10+
using System.IO;
11+
using Newtonsoft.Json;
12+
using Syncfusion.Pdf.Parsing;
13+
using System.Security.Cryptography.X509Certificates;
14+
using Syncfusion.Pdf.Security;
15+
using Syncfusion.Pdf;
16+
using System.Net;
17+
#if REDIS
18+
using Microsoft.Extensions.Caching.Distributed;
19+
#endif
20+
21+
namespace ESigningSimpleSample.Controllers
22+
{
23+
public partial class HomeController : Controller
24+
{
25+
private IMemoryCache _cache;
26+
private readonly IWebHostEnvironment _hostingEnvironment;
27+
#if REDIS
28+
private IDistributedCache _distributedCache;
29+
public PdfViewerController(IMemoryCache memoryCache, IDistributedCache distributedCache, IWebHostEnvironment hostingEnvironment)
30+
#else
31+
public HomeController(IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
32+
#endif
33+
{
34+
_cache = memoryCache;
35+
#if REDIS
36+
_distributedCache = distributedCache;
37+
#endif
38+
_hostingEnvironment = hostingEnvironment;
39+
}
40+
// GET: Default
41+
public ActionResult Index()
42+
{
43+
return View();
44+
}
45+
public ActionResult ESigningPdfForms()
46+
{
47+
return View();
48+
}
49+
50+
[AcceptVerbs("Post")]
51+
[HttpPost]
52+
[Route("api/[controller]/FlattenDownload")]
53+
public IActionResult FlattenDownload([FromBody] Dictionary<string, string> jsonObject)
54+
{
55+
try
56+
{
57+
string documentBase = "";
58+
59+
// Check if the request contains base64String (from custom JavaScript call)
60+
if (jsonObject != null && jsonObject.ContainsKey("base64String"))
61+
{
62+
documentBase = jsonObject["base64String"];
63+
}
64+
65+
// Handle base64 string processing safely
66+
string base64String = "";
67+
if (documentBase.Contains("data:application/pdf;base64,"))
68+
{
69+
base64String = documentBase.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1];
70+
}
71+
else
72+
{
73+
// Assume it's already just the base64 data without the prefix
74+
base64String = documentBase;
75+
}
76+
77+
byte[] byteArray = Convert.FromBase64String(base64String);
78+
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray);
79+
80+
if (loadedDocument.Form != null)
81+
{
82+
loadedDocument.FlattenAnnotations();
83+
loadedDocument.Form.Flatten = true;
84+
}
85+
86+
//Save the PDF document.
87+
MemoryStream stream = new MemoryStream();
88+
//Save the PDF document
89+
loadedDocument.Save(stream);
90+
stream.Position = 0;
91+
//Close the document
92+
loadedDocument.Close(true);
93+
94+
string updatedDocumentBase = Convert.ToBase64String(stream.ToArray());
95+
documentBase = "data:application/pdf;base64," + updatedDocumentBase;
96+
return Content(documentBase);
97+
}
98+
catch (Exception ex)
99+
{
100+
return BadRequest($"Error processing PDF: {ex.Message}");
101+
}
102+
}
103+
104+
}
105+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
10+
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" Condition="$(DefineConstants.Contains('REDIS'))" />
11+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
12+
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="27.1.57" />
13+
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows" Version="27.1.57" />
14+
<PackageReference Include="Syncfusion.SkiaSharpHelper.Net.Core" Version="27.1.57" />
15+
<PackageReference Include="System.Net.Http" Version="4.3.4" />
16+
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
17+
</ItemGroup>
18+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
5+
</PropertyGroup>
6+
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
7+
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
8+
</PropertyGroup>
9+
</Project>
Lines changed: 25 additions & 0 deletions
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.10.34818.151
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ESigningSimpleSample", "ESigningSimpleSample.csproj", "{B3F357A0-E249-4330-A8BE-C50E7248CF9F}"
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+
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.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 = {7B65EB19-C66B-4322-ABAB-679E7F0E9DE4}
24+
EndGlobalSection
25+
EndGlobal
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace ESigningSimpleSample.Models
2+
{
3+
public class ErrorViewModel
4+
{
5+
public string? RequestId { get; set; }
6+
7+
public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
8+
}
9+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<packageSources>
4+
<clear />
5+
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
6+
</packageSources>
7+
<fallbackPackageFolders>
8+
<clear />
9+
</fallbackPackageFolders>
10+
<disabledPackageSources>
11+
<clear />
12+
</disabledPackageSources>
13+
</configuration>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using Newtonsoft.Json.Serialization;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
builder.Services.AddControllersWithViews();
7+
builder.Services.AddControllers().AddNewtonsoftJson(options =>
8+
{
9+
// Use the default property (Pascal) casing
10+
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
11+
});
12+
var app = builder.Build();
13+
14+
// Configure the HTTP request pipeline.
15+
if (!app.Environment.IsDevelopment())
16+
{
17+
app.UseExceptionHandler("/Home/Error");
18+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
19+
app.UseHsts();
20+
}
21+
22+
app.UseHttpsRedirection();
23+
app.UseStaticFiles();
24+
25+
app.UseRouting();
26+
27+
app.UseAuthorization();
28+
29+
app.MapControllerRoute(
30+
name: "default",
31+
pattern: "{controller=Home}/{action=Index}/{id?}");
32+
33+
app.Run();
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"$schema": "http://json.schemastore.org/launchsettings.json",
3+
"iisSettings": {
4+
"windowsAuthentication": false,
5+
"anonymousAuthentication": true,
6+
"iisExpress": {
7+
"applicationUrl": "http://localhost:59435",
8+
"sslPort": 44320
9+
}
10+
},
11+
"profiles": {
12+
"http": {
13+
"commandName": "Project",
14+
"dotnetRunMessages": true,
15+
"launchBrowser": true,
16+
"applicationUrl": "http://localhost:5229",
17+
"environmentVariables": {
18+
"ASPNETCORE_ENVIRONMENT": "Development"
19+
}
20+
},
21+
"https": {
22+
"commandName": "Project",
23+
"dotnetRunMessages": true,
24+
"launchBrowser": true,
25+
"applicationUrl": "https://localhost:7252;http://localhost:5229",
26+
"environmentVariables": {
27+
"ASPNETCORE_ENVIRONMENT": "Development"
28+
}
29+
},
30+
"IIS Express": {
31+
"commandName": "IISExpress",
32+
"launchBrowser": true,
33+
"environmentVariables": {
34+
"ASPNETCORE_ENVIRONMENT": "Development"
35+
}
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)