Extract Font and Retain Data on Server in ASP.NET MVC PDF Viewer.
This article details the steps to extract the font size from form fields in a ASP.NET MVC PDF Viewer and persist the extracted information on a server. It covers client-side operations to retrieve and update form fields, and server-side logic to store and manage this data effectively.
Solution:
To achieve this, you will first retrieve form fields from the PDF Viewer and extract the font size of each field using JavaScript on the client-side. This information is then added as custom data to the form field. On the server-side, you will handle incoming data, parse the custom data, and store or use it as required. This process ensures all relevant font size data from PDF form fields is securely retained and managed on the server.
Prerequisites:
Before beginning the implementation process, ensure that the following prerequisites are completed:
- Syncfusion PDF Viewer Setup for ASP.NET MVC: Make sure the Syncfusion PDF Viewer is installed and set up in your ASP.NET MVC project. Follow the Getting Started with Syncfusion PDF Viewer for ASP.NET MVC guide if you haven’t already.
- Basic Knowledge of ASP.NET MVC: Familiarity with ASP.NET MVC development, including the components and basic setup of MVC projects, will help you follow along with the implementation.
Implementation Steps:
Step 1: Retrieve and Update Font Size on the Client-Side
// Retrieve all form fields from the PDF Viewer.
var formFields = viewer.retrieveFormFields();
var fontSize;
// Iterate over each form field.
for (var i = 0; i < formFields.length; i++) {
// Check for the specific form field by name.
if (formFields[i].name === "name") {
// Extract and store the font size.
fontSize = formFields[i].fontSize;
// Update the form field with custom data.
viewer.formDesignerModule.updateFormField(formFields[i], {
customData: { FontSize: fontSize }
});
}
}
Step 2: Handle Data on the Server-Side
[System.Web.Mvc.HttpPost]
public ActionResult GetCustomData(jsonObjects jsonObject)
{
PdfRenderer pdfviewer = new PdfRenderer();
var jsonData = JsonConverter(jsonObject);
var formFieldsJsonValue = jsonData["formDesigner"];
var settings = new JsonSerializerSettings
{
Error = (sender, args) =>
{
args.ErrorContext.Handled = true;
}
};
// Deserialize form fields data.
var data = JsonConvert.DeserializeObject<List<formFieldCollection>>(formFieldsJsonValue, settings);
for (var i = 0; i < data.Count; i++)
{
var formfield = data[i].FormField;
if (formfield.name == "name")
{
var fontSize = formfield.customData["FontSize"];
// Store or use the fontSize as needed.
}
}
// Return processed document if necessary.
string documentBase = pdfviewer.GetDocumentAsBase64(jsonData);
return Content(documentBase);
}
Key Features Used in the Code:
- Form Field Retrieval: Retrieve form fields from the PDF using the PDF Viewer.
- Form Designer Module: Update form fields with custom data for additional context.
- JsonConverter and Serializer: Used to handle JSON data integrity and error handling.
Sample Link:
You can find the full sample in our GitHub repository.
Conclusion:
I hope you enjoyed learning how to extract font and retain data on the server in ASP.NET MVC PDF Viewer.
You can refer to ASP.NET MVC PDF Viewer Feature Tour page to learn about its other groundbreaking feature representations and documentation, and how to quickly get started with configuration specifications.
You can also explore our ASP.NET MVC PDF Viewer Example to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums or feedback portal. We are always happy to assist you!