Charts

Chart elements are drawing elements used for graphical representation of data. GemBox.Document supports charts in DOCX, PDF, XPS, PNG, JPEG, GIF, TIFF, EMF, and WMF formats with help of the GemBox.Spreadsheet component. Without the GemBox.Spreadsheet, charts will be preserved.

The following example demonstrates how to convert a Word document with charts to XPS, PDF, or one of the supported image formats.

Screenshot of PDF file with charts
Word document with charts
Upload your file (Drag file here)
using GemBox.Document;
using GemBox.Spreadsheet;

class Program
{
    static void Main()
    {
        // If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");

        // This is needed to enable chart rendering in the GemBox.Document.
        // If you own a professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        // Load input file and save it in selected output format
        DocumentModel document = DocumentModel.Load("%InputFileName%");
        document.Save("Output.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Spreadsheet

Module Program

    Sub Main()

        ' If using Professional version, put your serial key below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")

        ' This Is needed to enable chart rendering in the GemBox.Document.
        ' If you own a professional version, put your serial key below.
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        ' Load input file and save it in selected output format
        Dim document As DocumentModel = DocumentModel.Load("%InputFileName%")
        document.Save("Output.%OutputFileType%")

    End Sub

End Module

As shown above, you will need to load the GemBox.Spreadsheet library in order to export charts to PDF, XPS, or any image format. Without it, charts will not be loaded into the model as Chart elements. However, they will be preserved so you can edit DOCX files without the fear of losing any of them.

To enable chart support, all you have to do is add a reference to the GemBox.Spreadsheet library and call the SpreadsheetInfo.SetLicense method with your professional or free version key.

Please note that not all chart types are supported. For more information visit Spreadsheet's charts help page.

Create chart elements

Besides converting Word documents with charts to PDF, XPS, or image formats, you can also create and edit charts using GemBox.Document.

The following example demonstrates how you can create a chart using the GemBox.Document and select data for it using the GemBox.Spreadsheet component.

Screenshot of created Word file with chart
Word document with created chart element
using GemBox.Document;
using GemBox.Spreadsheet;
using GemBox.Spreadsheet.Charts;

class Program
{
    static void Main()
    {
        // If using Professional versions, put your serial keys below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY");
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY");

        DocumentModel document = new DocumentModel();

        // Create simple document title
        Paragraph title = new Paragraph(document, "GemBox.Document - Create chart example");

        title.ParagraphFormat.Alignment = HorizontalAlignment.Center;

        // Create chart
        Chart chart = new Chart(document, GemBox.Document.ChartType.Bar,
            new FloatingLayout(
                new HorizontalPosition(HorizontalPositionType.Center, HorizontalPositionAnchor.Margin),
                new VerticalPosition(VerticalPositionType.Top, VerticalPositionAnchor.Paragraph),
                new Size(14, 7, GemBox.Document.LengthUnit.Centimeter)));

        // Get underlying Excel chart
        ExcelChart excelChart = (ExcelChart)chart.ExcelChart;
        ExcelWorksheet worksheet = excelChart.Worksheet;

        // Add data which will be used by the Excel chart.
        worksheet.Cells["A1"].Value = "Name";
        worksheet.Cells["A2"].Value = "John Doe";
        worksheet.Cells["A3"].Value = "Fred Nurk";
        worksheet.Cells["A4"].Value = "Hans Meier";
        worksheet.Cells["A5"].Value = "Ivan Horvat";

        worksheet.Cells["B1"].Value = "Salary";
        worksheet.Cells["B2"].Value = 3600;
        worksheet.Cells["B3"].Value = 2580;
        worksheet.Cells["B4"].Value = 3200;
        worksheet.Cells["B5"].Value = 4100;

        // Select data
        excelChart.SelectData(
            worksheet.Cells.GetSubrangeAbsolute(0, 0, 4, 1), true);

        // Add document elements
        document.Sections.Add(new Section(document,
            title, new Paragraph(document, chart)));

        document.Save("Chart.%OutputFileType%");
    }
}
Imports GemBox.Document
Imports GemBox.Spreadsheet
Imports GemBox.Spreadsheet.Charts

Module Program

    Sub Main()

        ' If using Professional versions, put your serial keys below.
        ComponentInfo.SetLicense("FREE-LIMITED-KEY")
        SpreadsheetInfo.SetLicense("FREE-LIMITED-KEY")

        Dim document As DocumentModel = New DocumentModel()

        ' Create simple document title
        Dim title As Paragraph = New Paragraph(document, "GemBox.Document - Create chart example")

        title.ParagraphFormat.Alignment = HorizontalAlignment.Center

        ' Create chart
        Dim chart As Chart = New Chart(document, GemBox.Document.ChartType.Bar,
            New FloatingLayout(
                New HorizontalPosition(HorizontalPositionType.Center, HorizontalPositionAnchor.Margin),
                New VerticalPosition(VerticalPositionType.Top, VerticalPositionAnchor.Paragraph),
                New Size(14, 7, GemBox.Document.LengthUnit.Centimeter)))

        ' Get underlying Excel chart
        Dim excelChart As ExcelChart = DirectCast(chart.ExcelChart, ExcelChart)
        Dim worksheet As ExcelWorksheet = excelChart.Worksheet

        ' Add data which will be used by the Excel chart.
        worksheet.Cells("A1").Value = "Name"
        worksheet.Cells("A2").Value = "John Doe"
        worksheet.Cells("A3").Value = "Fred Nurk"
        worksheet.Cells("A4").Value = "Hans Meier"
        worksheet.Cells("A5").Value = "Ivan Horvat"

        worksheet.Cells("B1").Value = "Salary"
        worksheet.Cells("B2").Value = 3600
        worksheet.Cells("B3").Value = 2580
        worksheet.Cells("B4").Value = 3200
        worksheet.Cells("B5").Value = 4100

        ' Select data
        excelChart.SelectData(
            worksheet.Cells.GetSubrangeAbsolute(0, 0, 4, 1), True)

        ' Add document elements
        document.Sections.Add(New Section(document,
            title, New Paragraph(document, chart)))

        document.Save("Chart.%OutputFileType%")

    End Sub

End Module

For more examples about editing or formatting charts, please refer to Spreadsheet's chart examples.

Check next example or download examples from GitHub.