News Category

Spire.Doc 11.5.6 supports adding charts

2023-05-16 02:01:25

We are excited to announce the release of Spire.Doc 11.5.6. This version supports adding charts and supports adding SVG documents. What’s more, it also supports printing multiple pages onto one page and manipulating pages, such as retrieving page content and its coordinates. More details are listed below.

Here is a list of changes made in this release

Category ID Description
New feature - Supports adding charts.
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Column chart.");

//Create a new section to append column chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Column, 500, 300);

//Clear the chart's series data to start with a clean chart.
Chart chart = shape.Chart;
chart.Series.Clear();

//Add a custom series to the chart with categories for the X-axis, and large respective numeric values for the Y-axis. 
chart.Series.Add("Test Series",
    new[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
    new double[] { 1900000, 850000, 2100000, 600000, 1500000 });

//Set the number format of the Y-axis tick labels to group digits with commas. 
chart.AxisY.NumberFormat.FormatCode = "#,##0";

//Save the result file.
document.SaveToFile("AppendColumnChart.docx", FileFormat.Docx);
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Bubble chart.");

//Create a new paragraph and appen bubble chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Bubble, 500, 300);

//Clear char's series data to start with a clean chart
Chart chart = shape.Chart;
chart.Series.Clear();

//Add a custom series with X/Y coordinates and diameter of each bubbles. 
ChartSeries series = chart.Series.Add("Test Series",
    new[] { 2.9, 3.5, 1.1, 4.0, 4.0 },
    new[] { 1.9, 8.5, 2.1, 6.0, 1.5 },
    new[] { 9.0, 4.5, 2.5, 8.0, 5.0 });

//Save doc file.
document.SaveToFile("AppendBubbleChart.docx", FileFormat.Docx);
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Line chart.");

//Create a new paragraph to append line chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Line, 500, 300);

//Clear series data of line chart to start with a clean chart   
Chart chart = shape.Chart;
ChartTitle title = chart.Title;
title.Text = "My Chart";
ChartSeriesCollection seriesColl = chart.Series;
seriesColl.Clear();

//Set new data to chart
string[] categories = { "C1", "C2", "C3", "C4", "C5", "C6" };
seriesColl.Add("AW Series 1", categories, new double[] { 1, 2, 2.5, 4, 5, 6 });
seriesColl.Add("AW Series 2", categories, new double[] { 2, 3, 3.5, 6, 6.5, 7 });

//Save doc file.
document.SaveToFile("AppendLineChart.docx", FileFormat.Docx);
//Create a word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Pie chart.");

//Create a new paragraph to append pie chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Pie, 500, 300);
Chart chart = shape.Chart;

//Insert a custom chart series with a category name for each sectors and their frequency value.
ChartSeries series = chart.Series.Add("Test Series",
  new[] { "Word", "PDF", "Excel" },
  new[] { 2.7, 3.2, 0.8 });

//Save to a docx file.
document.SaveToFile("AppendPieChart.docx", FileFormat.Docx);
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Scatter chart.");

//Create a new paragraph to append scatter chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Scatter, 450, 300);
Chart chart = shape.Chart;

//Clear the chart's series data to start with a clean chart.
chart.Series.Clear();

//Insert a series with X/Y coordinates for five points.
chart.Series.Add("Scatter chart",
    new[] { 1.0, 2.0, 3.0, 4.0, 5.0 },
    new[] { 1.0, 20.0, 40.0, 80.0, 160.0 });

//Save to a docx file.
document.SaveToFile("AppendScatterChart.docx", FileFormat.Docx);
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Surface3D chart.");

//Create a new paragraph to append surface 3D chart
Paragraph newPara = section.AddParagraph();
ShapeObject shape = newPara.AppendChart(ChartType.Surface3D, 500, 300);

//Clear its series data to start with a clean chart
Chart chart = shape.Chart;
chart.Series.Clear();


chart.Title.Text = "My chart";

//Add three series
chart.Series.Add("Series 1",
    new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
    new double[] { 1900000, 850000, 2100000, 600000, 1500000 });

chart.Series.Add("Series 2",
    new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
    new double[] { 900000, 50000, 1100000, 400000, 2500000 });

chart.Series.Add("Series 3",
    new string[] { "Word", "PDF", "Excel", "GoogleDocs", "Office" },
    new double[] { 500000, 820000, 1500000, 400000, 100000 });

//Save to a docx file.
document.SaveToFile("AppendSurface3DChart.docx", FileFormat.Docx);
//Create word document
Document document = new Document();

//Create a new section
Section section = document.AddSection();

//Create a new paragraph and append text
section.AddParagraph().AppendText("Bar chart.");

//Create a new paragraph to append bar chart
Paragraph newPara = section.AddParagraph();
ShapeObject chartShape = newPara.AppendChart(ChartType.Bar, 400, 300);
Chart chart = chartShape.Chart;

//Use the "Title" property to give bar chart a title, which appears at the top center of the chart area.
ChartTitle title = chart.Title;
title.Text = "My Chart";

//Set the "Show" property to "true" to make the title visible. 
title.Show = true;

//Set the "Overlay" property to "true". Give other chart elements more room by allowing them to overlap the title
title.Overlay = true;

//Save to docx file.
document.SaveToFile("AppendBarChart.docx", FileFormat.Docx);
New feature - Supports adding SVG documents.
Document document = new Document();
Section section = document.AddSection();
string svgFile = "sample.svg";
Paragraph para = section.AddParagraph();
DocPicture svgPicture = para.AppendPicture(svgFile);
svgPicture.Width = 200;
svgPicture.Height = 200;
String DocxResult = "Result-AddSvg.docx";
document.SaveToFile(DocxResult, FileFormat.Docx2016);
New feature - Supports printing multiple pages onto one page.
doc.LoadFromFile(inputFile, FileFormat.Docx);
System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog();
printDialog.PrinterSettings.PrintToFile = true;
printDialog.PrinterSettings.PrintFileName = "sample-new-4.xps";
doc.PrintDialog = printDialog;
doc.PrintMultipageToOneSheet(PagesPreSheet.FourPages, true);
New feature - Support manipulating pages, such as retrieving page content and its coordinates.
Document doc = new Document();
doc.LoadFromFile(inputFile, FileFormat.Docx);
FixedLayoutDocument layoutDoc = new FixedLayoutDocument(doc);

// Access to the line of the first page and print to the console.
FixedLayoutLine line = layoutDoc.Pages[0].Columns[0].Lines[0];

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("Line: " + line.Text);

// With a rendered line, the original paragraph in the document object model can be returned.
Paragraph para = line.Paragraph;
stringBuilder.AppendLine("Paragraph text: " + para.Text);

// Retrieve all the text that appears on the first page in plain text format (including headers and footers).
string pageText = layoutDoc.Pages[0].Text;
stringBuilder.AppendLine(pageText);

// Loop through each page in the document and print the count of the lines appear on each page.
foreach (FixedLayoutPage page in layoutDoc.Pages)
{
    LayoutCollection lines = page.GetChildEntities(LayoutElementType.Line, true);
    stringBuilder.AppendLine("Page " + page.PageIndex + " has " + lines.Count + " lines.");
}

// This method provides a reverse lookup of layout entities for any given node
// (except runs and nodes in the header and footer).
stringBuilder.AppendLine("The lines of the first paragraph:");
foreach (FixedLayoutLine paragraphLine in layoutDoc.GetLayoutEntitiesOfNode(
    ((Section)doc.FirstChild).Body.Paragraphs[0]))
{
    stringBuilder.AppendLine(paragraphLine.Text.Trim());
    stringBuilder.AppendLine(paragraphLine.Rectangle.ToString());
}
File.WriteAllText("page.txt", stringBuilder.ToString());
Click the link to download Spire.Doc 11.5.6:
More information of Spire.Doc new release or hotfix: