By using Aspose.Total for .NET you can parse JSON to DOC within any .NET, C#, ASP.NET and VB.NET application in two simple steps. Firstly, by using Aspose.Cells for .NET , you can export JSON to PDF. After that, by using Aspose.Words for .NET , you can convert PDF to DOC.
Convert JSON Format to DOC via C#
- Create a new Workbook object and read valid JSON data from file
- Import JSON file to worksheet using JsonUtility class and Save it as PDF
- Load PDF document by using Document class
- Save the document to DOC format using Save method
Conversion Requirements
Install from the command line as nuget install Aspose.Total
or via Package Manager Console of Visual Studio.
Alternatively, get the offline MSI installer or DLLs in a ZIP file from downloads .
// create a Workbook object | |
var workbook = new Workbook(); | |
var worksheet = workbook.Worksheets[0]; | |
// read JSON data from file | |
string jsonInput = File.ReadAllText("input.json"); | |
// import JSON data to worksheet starting at cell A1 | |
Utility.JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, new JsonLayoutOptions()); | |
// save resultant file in PDF format | |
workbook.Save("output.pdf", SaveFormat.Pdf); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.Doc | |
document.Save("output.doc", Aspose.Words.SaveFormat.Doc); |
Set Layout and Convert JSON Format to DOC via C#
While parsing JSON to DOC, you can also set layout options for your JSON using JsonLayoutOptions . It allows you to process Array as a table, ignore nulls, ignore array title, ignore object title, convert string to number or date, set date and number format, and set title style. All of these options allow you to present your data as per your needs. The following code snippet shows you how to set the layout options.
// create a Workbook object | |
var workbook = new Workbook(); | |
var worksheet = workbook.Worksheets[0]; | |
// read JSON data from file | |
string jsonInput = File.ReadAllText("input.json"); | |
// set JsonLayoutOptions to treat Arrays as Table | |
var options = new Aspose.Cells.Utility.JsonLayoutOptions(); | |
options.ArrayAsTable = true; | |
options.IgnoreNull = true; | |
options.IgnoreObjectTitle = true; | |
options.IgnoreArrayTitle = true; | |
// import JSON data to worksheet starting at cell A1 | |
Utility.JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, options); | |
// save resultant file in PDF format | |
workbook.Save("output.pdf", SaveFormat.Pdf); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.Doc | |
document.Save("output.doc", Aspose.Words.SaveFormat.Doc); |
Parse JSON Format to DOC with Watermark
Using the API, you can also convert JSON to DOC with watermark. In order to add a watermark to your DOC document, you can first parse JSON file to PDF and add a watermark to it. In order to add a watermark, load the newly created PDF file using the Document class, create an instance of TextWatermarkOptions and set its properties, Call Watermark.SetText method and pass watermark text & object of TextWatermarkOptions. After adding the watermark, you can save the document to DOC.
// create a Workbook object | |
var workbook = new Workbook(); | |
var worksheet = workbook.Worksheets[0]; | |
// read JSON data from file | |
string jsonInput = File.ReadAllText("input.json"); | |
// import JSON data to worksheet starting at cell A1 | |
Utility.JsonUtility.ImportData(jsonInput, worksheet.Cells, 0, 0, new JsonLayoutOptions()); | |
// save resultant file in PDF format | |
workbook.Save("output.pdf", SaveFormat.Pdf); | |
// load PDF with an instance of Document | |
var document = new Document("output.pdf"); | |
// create an instance of TextWatermarkOptions and set its properties | |
var waterMakroptions = new TextWatermarkOptions() | |
{ | |
FontFamily = "Arial", | |
FontSize = 36, | |
Layout = WatermarkLayout.Horizontal, | |
IsSemitrasparent = false | |
}; | |
// set watermark text with the object of TextWatermarkOptions | |
document.Watermark.SetText("CONFIDENTIAL", waterMakroptions); | |
// supports DOC, DOT, DOCM, DOTX, DOTX, FLATOPC, RTF, WordML, MOBI, CHM, ODT, OTT, PS, PCL, EPUB file formats | |
// call Save method while passing SaveFormat.Doc | |
document.Save("output.doc", Aspose.Words.SaveFormat.Doc); |
Transforming JSON File to DOC Programmatically : Use Cases
JSON (JavaScript Object Notation) files are used to store data in a structured and easily readable format, making them ideal for creating dynamic web applications and APIs. However, when working with document-based formats, Microsoft Word documents (.doc) become essential for content creation and editing.
The conversion of JSON files into .doc formats is necessary to unlock the full potential of your content creation and editing capabilities. This conversion enables you to:
Use Cases:
- Document Automation: Convert JSON files to create dynamic document templates, automate report generation, and reduce manual data entry.
- Content Integration: Use .doc to integrate content from various sources, such as web scraping or API integrations, into a cohesive document format.
- Collaboration Tools: Convert JSON files to enable real-time collaboration and commenting on documents using tools like Microsoft Word Online or Google Docs.
- Data-Driven Content: Use .doc to create data-driven content, such as personalized emails or newsletters, that can be easily updated and customized.
- Accessibility Features: Convert JSON files to add accessibility features to documents, such as text-to-speech functionality or high contrast mode.