How to Convert JSON to PDF, CSV, HTML, Image Using .NET C#
Tags: webdev, tutorial, java, dotnet
Published: 2023-02-01 20:58:37

---
canonical_url: https://www.grapecity.com/blogs/how-to-convert-json-to-pdf-csv-html-image-using-net-c-sharp
description: Learn how to convert a JSON file to a number of different extensions including PDF, CSV, HTML, and image using a .NET C# Excel API Library. See more from GrapeCity Documents today.
---

JSON is one of the essential technologies in this era of modern software programming. First introduced in the 2000s, it has gradually expanded its market to become the most common medium for exchanging and transmitting data across web applications (e.g., sending some data from the server to the client).

The exchanged data can be used to meet several programming needs, including displaying information on a web page, storing data in a local or cloud location, or printing. However, this process requires writing explicit codes, first to convert the JSON data from its native form to a structured layout and then to export it to a format suitable for the described data need.

![](https://files.grapecity.com/gc-website-media/cqmp42sh/json-to-export.png?width=500&height=158.13953488372093)

GrapeCity Documents for Excel ([.NET Edition](https://www.grapecity.com/documents-api-excel) | [Java Edition](https://www.grapecity.com/documents-api-excel-java)) offers simple and easy-to-use APIs to meet these programming needs. To see how to convert native JSON to a structured layout, you can follow [this article](https://www.grapecity.com/blogs/programmatically-get-json-in-excel-xlsx-using-c-sharp-net-java-document-apis). Here, we will see how to export it to popular document formats - [PDF](#pdf), [HTML](#html), [CSV](#csv), and [Image](#image).

### <a id="pdf"></a>Convert JSON to PDF (Portable Document Format)

PDF is a popular format when you want to save files that cannot be modified and are easily shared and printed. This format is suitable for structured documents such as invoices, Medical Test reports, etc.

With GrapeCity Documents for Excel, you can use the [Excel template](https://www.grapecity.com/documents-api-excel/net-excel-xlsx-report-templates) to create a structured document and [fetch the JSON data](https://www.grapecity.com/blogs/programmatically-get-json-in-excel-xlsx-using-c-sharp-net-java-document-apis#fetching) using the **JsonDataSource** class and **IWorkbook.AddDataSource** method or **IWorksheet.DataSource** property. Once the data is added to the document, use **IWorkbook**.**Save** method and **SaveFileFormat.PDF** enum to export it to PDF as shown in the code below:

**.NET**

```
    var datasource = new JsonDataSource(jsonText);
    workbook.AddDataSource("ds", datasource);

    . . .

    workbook.Save("Invoice.pdf");
    OR
    workbook.Save("Invoice.pdf", SaveFileFormat.Pdf);
```

**JAVA**

```
    worksheet.setDataSource(new JsonDataSource(jsonText));
    . . .
    workbook.save("Invoice.pdf");
    OR
    workbook.save("Invoice.pdf", SaveFileFormat.Pdf);
```

GcExcel offers several properties with the **PdfSaveOptions** class that you can use to manage how the PDF is generated.

**.NET**

```
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
    {
         //Set pdf options
         DocumentProperties = documentProperties
         SecurityOptions = securityOptions
         . . .
    };

    workbook.Save("Invoice.pdf", pdfSaveOptions);
```

**JAVA**

```
    PdfSaveOptions pdfSaveOptions = new PdfSaveOptions();
    pdfSaveOptions.setSecurityOptions(securityOptions);
    pdfSaveOptions.setDocumentProperties(documentProperties);
    . . .

    workbook.save("Invoice.pdf", pdfSaveOptions);
```

Have a look at the [.NET](https://www.grapecity.com/documents-api-excel/demos/pdfexporting) | [Java](https://www.grapecity.com/documents-api-excel-java/demos/pdfexporting) demos.

For more details, follow the [.NET](https://www.grapecity.com/documents-api-excel/docs/online/ExporttoaPDFFile.html) | [Java](https://www.grapecity.com/documents-api-excel-java/docs/online/ExporttoaPDFFile.html) documentation.

### <a id="html"></a>Convert JSON to HTML

Companies often want to publish information on websites for customers, employees, and other people involved in the business. For example, an automobile website publishing the car price list, a finance company posting financial documents, and many more such scenarios.

In this situation, you may want to convert the JSON data to HTML format to embed it in your website page easily.

With GcExcel, use the **JsonDatasource** object in GcExcel APIs to get the data in a Table, Worksheet, or Template file using **IWorkbook.AddDataSource** method or **IWorksheet.DataSource** property. Then use the **IWorkbook**.**Save** method and **SaveFileFormat.HTML** enum to convert it to HTML, as shown in the sample snippet below:

**.NET**

```
    var datasource = new JsonDataSource(jsonText);
    workbook.AddDataSource("ds", datasource);
    OR
    workbook.ActiveSheet.datasource = datasource;
    . . .

    workbook.Save("PriceList.html");
    OR
    workbook.Save("PriceList.html", SaveFileFormat.Html);
```

**JAVA**

```
    worksheet.setDataSource(new JsonDataSource(jsonText));
    OR
    workbook.getActiveSheet.datasource = new JsonDataSource(jsonText);
    . . .
    workbook.save("PriceList.html");
    OR
    workbook.save("PriceList.html", SaveFileFormat.Html);
```

GcExcel offers several properties in **HtmlSaveOptions** class that you can use to control the exported content in various ways, like exporting headings, gridlines, and document properties or applying other settings like scalable width, page title, displaying tooltip text, etc.

**.NET**

```
    HtmlSaveOptions options = new HtmlSaveOptions();
    options.ExportSheetName = workbook.Worksheets[0].Name;
    options.ExportFileName = "PriceList";

    workbook.Save("PriceList.html", options);
```

**JAVA**

```
    HtmlSaveOptions options = new HtmlSaveOptions();
    options.setExportSheetName(workbook.getWorksheets().get(0).getName());
    options.setExportArea("D2:G23");
    options.setExportFileName("PriceList");

    workbook.save("PriceList.html", options);
```

Explore the [.NET](https://www.grapecity.com/documents-api-excel/demos/htmlexporting) | [Java](https://www.grapecity.com/documents-api-excel-java/demos/htmlexporting) demos.

For more details, follow the [.NET](https://www.grapecity.com/documents-api-excel/docs/online/export-to-html-file.html) | [Java](https://www.grapecity.com/documents-api-excel-java/docs/online/export-to-html-file.html) documentation.

### <a id="csv"></a>Convert JSON to CSV

CSV file format is popular with developers because it is easier to import into a spreadsheet or another storage database and is supported by most data upload interfaces. Though JSON is taking over the market for data exchange scenarios, CSV still finds its use when data is required in the form of a matrix where each row represents a record.

GcExcel offers to convert the JSON data to CSV using two simple steps - load the JSON data in a worksheet using the **JsonDataSource** object and **IWorkbook.AddDataSource** method or **IWorksheet.DataSource** property as shown in the code snippet below:

**.NET**

```
    var datasource = new JsonDataSource(jsonText);
    workbook.AddDataSource("ds", datasource);
    OR
    workbook.ActiveSheet.datasource = datasource;

```

**Java**

```
    JsonDataSource datasource = new JsonDataSource(jsonText);
    workbook.addDataSource("ds", datasource);
    OR
    workbook.getActiveSheet.datasource = datasource;
```

Then exporting the worksheet to CSV using the **IWorkbook**.**Save method**, **SaveFileFormat.CSV** enum, and **CsvSaveOptions** class as shown below:

**.NET**

```
    CsvSaveOptions options = new CsvSaveOptions
    {
        ColumnSeparator = "-",
        RowSeparator = "\r\n",
        CellSeparator = '"'
    };

    workbook.Save("Data.csv", options);
```

**Java**

```
    CsvOpenOptions options = new CsvOpenOptions();
    options.setColumnSeparator(",");
    options.setRowSeparator("\r\n");
    options.setCellSeparator('"');
    workbook.save("Data.csv", options);
```

Check out the [.NET](https://www.grapecity.com/documents-api-excel/demos/importandexportcsvfiles) | [Java](https://www.grapecity.com/documents-api-excel-java/demos/importandexportcsvfiles) demos.

For more details about import and export to CSV, follow the [.NET](https://www.grapecity.com/documents-api-excel/docs/online/ImportandExportCSVFile.html) | [Java](https://www.grapecity.com/documents-api-excel-java/docs/online/ImportandExportCSVFile.html) documentation.

### <a id="image"></a>Convert JSON to Image

Images are often required to present a defined snapshot of data in documents such as Analysis reports, PPT presentations, etc.

With JSON data, there are two common situations where image conversion is required. One where JSON contains base64 image strings must be converted to Image types such as .jpg, .png, and so on. Other, where the raw JSON data is to be visualized using a table, graph, or chart and then captured as an image snapshot.

**Case 1 - Convert base64 image string from JSON to Image**

To meet this need, use the **JsonDataSource** class with **IWorkbook.AddDataSource** method or **IWorksheet.DataSource** property to [bind the data](https://www.grapecity.com/blogs/programmatically-get-json-in-excel-xlsx-using-c-sharp-net-java-document-apis) and display it in a sheet or table. Then convert the base64 image strings to the image stream and then use this stream to add a picture to a worksheet in [.NET](https://www.grapecity.com/documents-api-excel/docs/online/WorkWithShapeAndPicture.html) and [Java](https://www.grapecity.com/documents-api-excel-java/docs/online/WorkWithShapeAndPicture.html) as shown below:

**.NET**

```
    //base64 to image stream conversion 
    byte[] imageBytes = Convert.FromBase64String(worksheet.Range["A1"].Value.ToString());
    MemoryStream stream = new MemoryStream();
    stream.Write(imageBytes, 0, imageBytes.Length);

    //convert image stream to picture object
    worksheet.Shapes.AddPicture(stream, ImageType.JPG, worksheet.Range["B1"]);
```

**Java**

```
    BufferedImage image = null;
    byte[] imageByte;
    BASE64Decoder decoder = new BASE64Decoder();
    imageByte = decoder.decodeBuffer(imageString);
    ByteArrayInputStream bis = new ByteArrayInputStream(imageByte);
    image = ImageIO.read(bis);
    bis.close();

    worksheet.getShapes.addPicture(image,ImageType.JPG, worksheet.getRange("B1"));
```

**Case 2 - Convert JSON data to a visual for an image snapshot**

Similar to the case above, to meet this need, use the **JsonDataSource** class with **IWorkbook.AddDataSource** method or **IWorksheet.DataSource** property to [bind the data](https://www.grapecity.com/blogs/programmatically-get-json-in-excel-xlsx-using-c-sharp-net-java-document-apis) and display it in a Chart, Range, or Table. Finally, convert them using the **ToImage** method with worksheet, shape, or range as shown below:

**.NET**

```
    var datasource = new JsonDataSource(jsonText);
    worksheet.DataSource = datasource;
    worksheet.AutoGenerateColumns = true;
    . . .
    IShape chartObj = worksheet.Shapes.AddChart(ChartType.Bubble, 250, 20, 360, 230);
    chartObj.Chart.SeriesCollection.Add(worksheet.Range["A1:C5"], GrapeCity.Documents.Excel.Drawing.RowCol.Columns);
    . . .
    chartObj.ToImage("JsonToImage.png", ImageType.PNG);
```

**Java**

```
    worksheet.setDataSource(new JsonDataSource(jsonText));
    worksheet.setAutoGenerateColumns(true);
    . . .
    IShape chartObj = worksheet.getShapes().addChart(ChartType.Line, 250, 20, 400, 250);
    chartObj.getChart().getSeriesCollection().add(worksheet.getRange("A1:C5"));
    . . .
    chartObj.toImage("JsonToImage.png");
```

Have a look at the [.NET](https://www.grapecity.com/documents-api-excel/demos/imageexporting) | [Java](https://www.grapecity.com/documents-api-excel-java/demos/imageexporting) demos.

Learn exporting to image from [.NET](https://www.grapecity.com/documents-api-excel/docs/online/convert-to-image.html) | [Java](https://www.grapecity.com/documents-api-excel-java/docs/online/convert-to-image.html) documentation.