Skip to content

897552: Sample on How to Create User Based eSign PDF Forms #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 105 additions & 0 deletions How to/Create User Based eSign PDF Forms/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Caching.Memory;
using Syncfusion.EJ2.PdfViewer;
using System.IO;
using Newtonsoft.Json;
using Syncfusion.Pdf.Parsing;
using System.Security.Cryptography.X509Certificates;
using Syncfusion.Pdf.Security;
using Syncfusion.Pdf;
using System.Net;
#if REDIS
using Microsoft.Extensions.Caching.Distributed;
#endif

namespace ESigningSimpleSample.Controllers
{
public partial class HomeController : Controller
{
private IMemoryCache _cache;
private readonly IWebHostEnvironment _hostingEnvironment;
#if REDIS
private IDistributedCache _distributedCache;
public PdfViewerController(IMemoryCache memoryCache, IDistributedCache distributedCache, IWebHostEnvironment hostingEnvironment)
#else
public HomeController(IMemoryCache memoryCache, IWebHostEnvironment hostingEnvironment)
#endif
{
_cache = memoryCache;
#if REDIS
_distributedCache = distributedCache;
#endif
_hostingEnvironment = hostingEnvironment;
}
// GET: Default
public ActionResult Index()
{
return View();
}
public ActionResult ESigningPdfForms()
{
return View();
}

[AcceptVerbs("Post")]
[HttpPost]
[Route("api/[controller]/FlattenDownload")]
public IActionResult FlattenDownload([FromBody] Dictionary<string, string> jsonObject)
{
try
{
string documentBase = "";

// Check if the request contains base64String (from custom JavaScript call)
if (jsonObject != null && jsonObject.ContainsKey("base64String"))
{
documentBase = jsonObject["base64String"];
}

// Handle base64 string processing safely
string base64String = "";
if (documentBase.Contains("data:application/pdf;base64,"))
{
base64String = documentBase.Split(new string[] { "data:application/pdf;base64," }, StringSplitOptions.None)[1];
}
else
{
// Assume it's already just the base64 data without the prefix
base64String = documentBase;
}

byte[] byteArray = Convert.FromBase64String(base64String);
PdfLoadedDocument loadedDocument = new PdfLoadedDocument(byteArray);

if (loadedDocument.Form != null)
{
loadedDocument.FlattenAnnotations();
loadedDocument.Form.Flatten = true;
}

//Save the PDF document.
MemoryStream stream = new MemoryStream();
//Save the PDF document
loadedDocument.Save(stream);
stream.Position = 0;
//Close the document
loadedDocument.Close(true);

string updatedDocumentBase = Convert.ToBase64String(stream.ToArray());
documentBase = "data:application/pdf;base64," + updatedDocumentBase;
return Content(documentBase);
}
catch (Exception ex)
{
return BadRequest($"Error processing PDF: {ex.Message}");
}
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation" Version="8.0.0" />
<PackageReference Include="Microsoft.Extensions.Caching.Redis" Version="2.2.0" Condition="$(DefineConstants.Contains('REDIS'))" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="8.0.0" />
<PackageReference Include="Syncfusion.EJ2.AspNet.Core" Version="27.1.57" />
<PackageReference Include="Syncfusion.EJ2.PdfViewer.AspNet.Core.Windows" Version="27.1.57" />
<PackageReference Include="Syncfusion.SkiaSharpHelper.Net.Core" Version="27.1.57" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
<PackageReference Include="System.Text.RegularExpressions" Version="4.3.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ActiveDebugProfile>IIS Express</ActiveDebugProfile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DebuggerFlavor>ProjectDebugger</DebuggerFlavor>
</PropertyGroup>
</Project>
25 changes: 25 additions & 0 deletions How to/Create User Based eSign PDF Forms/ESigningSimpleSample.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.10.34818.151
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ESigningSimpleSample", "ESigningSimpleSample.csproj", "{B3F357A0-E249-4330-A8BE-C50E7248CF9F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3F357A0-E249-4330-A8BE-C50E7248CF9F}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7B65EB19-C66B-4322-ABAB-679E7F0E9DE4}
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace ESigningSimpleSample.Models
{
public class ErrorViewModel
{
public string? RequestId { get; set; }

public bool ShowRequestId => !string.IsNullOrEmpty(RequestId);
}
}
13 changes: 13 additions & 0 deletions How to/Create User Based eSign PDF Forms/NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<fallbackPackageFolders>
<clear />
</fallbackPackageFolders>
<disabledPackageSources>
<clear />
</disabledPackageSources>
</configuration>
33 changes: 33 additions & 0 deletions How to/Create User Based eSign PDF Forms/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using Newtonsoft.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddControllers().AddNewtonsoftJson(options =>
{
// Use the default property (Pascal) casing
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
});
var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseHttpsRedirection();
app.UseStaticFiles();

app.UseRouting();

app.UseAuthorization();

app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");

app.Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"$schema": "http://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59435",
"sslPort": 44320
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5229",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7252;http://localhost:5229",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Loading