Multi-level category chart is a chart type that has both main category and subcategory labels. This type of chart is useful when you have figures for items that belong to different categories. In this article, you will learn how to create a multi-level category chart in Excel using Spire.XLS with C# and VB.NET.

Step 1: Create a Workbook instance and get the first worksheet.

Workbook wb = new Workbook();
Worksheet sheet = wb.Worksheets[0];

Step 2: Write data to cells.

sheet.Range["A1"].Text = "Main Category"; 
sheet.Range["A2"].Text = "Fruit";
sheet.Range["A6"].Text = "Vegies";
sheet.Range["B1"].Text = "Sub Category";
sheet.Range["B2"].Text = "Bananas";
sheet.Range["B3"].Text = "Oranges";
sheet.Range["B4"].Text = "Pears";
sheet.Range["B5"].Text = "Grapes";
sheet.Range["B6"].Text = "Carrots";
sheet.Range["B7"].Text = "Potatoes";
sheet.Range["B8"].Text = "Celery";
sheet.Range["B9"].Text = "Onions";
sheet.Range["C1"].Text = "Value";
sheet.Range["C2"].Value = "52";
sheet.Range["C3"].Value = "65";
sheet.Range["C4"].Value = "50";
sheet.Range["C5"].Value = "45";
sheet.Range["C6"].Value = "64";
sheet.Range["C7"].Value = "62";
sheet.Range["C8"].Value = "89";
sheet.Range["C9"].Value = "57";

Step 3: Vertically merge cells from A2 to A5, A6 to A9.

sheet.Range["A2:A5"].Merge();
sheet.Range["A6:A9"].Merge();

Step 4: Add a clustered bar chart to worksheet.

Chart chart = sheet.Charts.Add(ExcelChartType.BarClustered);
chart.ChartTitle = "Value";   
chart.PlotArea.Fill.FillType = ShapeFillType.NoFill;
chart.Legend.Delete();

Step 5: Set the data source of series data.

chart.DataRange = sheet.Range["C2:C9"];
chart.SeriesDataFromRange = false;

Step 6: Set the data source of category labels.

ChartSerie serie = chart.Series[0];
serie.CategoryLabels = sheet.Range["A2:B9"];

Step 7: Show multi-level category labels.

chart.PrimaryCategoryAxis.MultiLevelLable = true;

Step 8: Save the file.

wb.SaveToFile("output.xlsx", ExcelVersion.Version2013);

Output:

Create Multi-Level Category Chart in Excel in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Charts;
namespace CreateMutilLevelChart
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook wb = new Workbook();
            Worksheet sheet = wb.Worksheets[0];

            sheet.Range["A1"].Text = "Main Category";
            sheet.Range["A2"].Text = "Fruit";
            sheet.Range["A6"].Text = "Vegies";
            sheet.Range["B1"].Text = "Sub Category";
            sheet.Range["B2"].Text = "Bananas";
            sheet.Range["B3"].Text = "Oranges";
            sheet.Range["B4"].Text = "Pears";
            sheet.Range["B5"].Text = "Grapes";
            sheet.Range["B6"].Text = "Carrots";
            sheet.Range["B7"].Text = "Potatoes";
            sheet.Range["B8"].Text = "Celery";
            sheet.Range["B9"].Text = "Onions";
            sheet.Range["C1"].Text = "Value";
            sheet.Range["C2"].Value = "52";
            sheet.Range["C3"].Value = "65";
            sheet.Range["C4"].Value = "50";
            sheet.Range["C5"].Value = "45";
            sheet.Range["C6"].Value = "64";
            sheet.Range["C7"].Value = "62";
            sheet.Range["C8"].Value = "89";
            sheet.Range["C9"].Value = "57";
            sheet.Range["A2:A5"].Merge();
            sheet.Range["A6:A9"].Merge();
            sheet.AutoFitColumn(1);
            sheet.AutoFitColumn(2);

            Chart chart = sheet.Charts.Add(ExcelChartType.BarClustered);
            chart.ChartTitle = "Value";
            chart.PlotArea.Fill.FillType = ShapeFillType.NoFill;
            chart.Legend.Delete();
            chart.LeftColumn = 5;
            chart.TopRow = 1;
            chart.RightColumn = 14;
            chart.DataRange = sheet.Range["C2:C9"];
            chart.SeriesDataFromRange = false;
            ChartSerie serie = chart.Series[0];
            serie.CategoryLabels = sheet.Range["A2:B9"];
            chart.PrimaryCategoryAxis.MultiLevelLable = true;
            wb.SaveToFile("output.xlsx", ExcelVersion.Version2013);
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Charts
Namespace CreateMutilLevelChart

	Class Program

		Private Shared Sub Main(args As String())
			Dim wb As New Workbook()
			Dim sheet As Worksheet = wb.Worksheets(0)

			sheet.Range("A1").Text = "Main Category"
			sheet.Range("A2").Text = "Fruit"
			sheet.Range("A6").Text = "Vegies"
			sheet.Range("B1").Text = "Sub Category"
			sheet.Range("B2").Text = "Bananas"
			sheet.Range("B3").Text = "Oranges"
			sheet.Range("B4").Text = "Pears"
			sheet.Range("B5").Text = "Grapes"
			sheet.Range("B6").Text = "Carrots"
			sheet.Range("B7").Text = "Potatoes"
			sheet.Range("B8").Text = "Celery"
			sheet.Range("B9").Text = "Onions"
			sheet.Range("C1").Text = "Value"
			sheet.Range("C2").Value = "52"
			sheet.Range("C3").Value = "65"
			sheet.Range("C4").Value = "50"
			sheet.Range("C5").Value = "45"
			sheet.Range("C6").Value = "64"
			sheet.Range("C7").Value = "62"
			sheet.Range("C8").Value = "89"
			sheet.Range("C9").Value = "57"
			sheet.Range("A2:A5").Merge()
			sheet.Range("A6:A9").Merge()
			sheet.AutoFitColumn(1)
			sheet.AutoFitColumn(2)

			Dim chart As Chart = sheet.Charts.Add(ExcelChartType.BarClustered)
			chart.ChartTitle = "Value"
			chart.PlotArea.Fill.FillType = ShapeFillType.NoFill
			chart.Legend.Delete()
			chart.LeftColumn = 5
			chart.TopRow = 1
			chart.RightColumn = 14
			chart.DataRange = sheet.Range("C2:C9")
			chart.SeriesDataFromRange = False
			Dim serie As ChartSerie = chart.Series(0)
			serie.CategoryLabels = sheet.Range("A2:B9")
			chart.PrimaryCategoryAxis.MultiLevelLable = True
			wb.SaveToFile("output.xlsx", ExcelVersion.Version2013)
		End Sub
	End Class
End Namespace
Thursday, 08 March 2018 03:46

How to clone a word document in C#

With Spire.Doc, we can copy the content from one word document to another word document in C#. When we need to generate a large amount of documents from a single document, clone the document will be much easier. The clone method speeds up the generation of the word documents and developers only need one single line of code to get the copy of the word document.

Now we will show the code snippet of how to clone a word document in C#.

Step 1: Create a new instance of Document and load the document from file.

Document doc = new Document();
doc.LoadFromFile("Sample.docx",FileFormat.Docx2010);

Step 2: Clone the word document.

doc.Clone();

Step 3: Save the document to file.

doc.SaveToFile("Cloneword.docx", FileFormat.Docx2010);

Effective screenshot of clone the word document:

How to clone a word document in C#

Full codes of clone a word document:

using Spire.Doc;
namespace CloneWord
{
    class Program
    {
        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("Sample.docx", FileFormat.Docx2010);
            doc.Clone();
            doc.SaveToFile("Cloneword.docx", FileFormat.Docx2010);
        }
    }
}

When creating chart, the legend appears by default. If we just need the chart series rather than the legend, we can delete the legend entries after creating the chart. Spire.Presentation supports to delete legend entries from chart by using ChartLegend.DeleteEntry method. This article is going to demonstrate how we can use Spire.Presentation to accomplish this function.

Below screenshot shows the example PowerPoint file which contains 3 chart legend entries:

Delete Chart Legend Entries in PowerPoint in C#

Detail steps:

Step 1: Initialize an object of Presentation class and load the PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Input.pptx");

Step 2: Get the chart.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Delete the first and the second legend entries from the chart.

chart.ChartLegend.DeleteEntry(0);
chart.ChartLegend.DeleteEntry(1);

Step 4: Save the file.

ppt.SaveToFile("Output.pptx", FileFormat.Pptx2010);

Screenshot:

Delete Chart Legend Entries in PowerPoint in C#

Full code:

using Spire.Presentation;
using Spire.Presentation.Charts;

namespace Delete_chart_legend_entries_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            IChart chart = ppt.Slides[0].Shapes[0] as IChart;

            chart.ChartLegend.DeleteEntry(0);
            chart.ChartLegend.DeleteEntry(1);

            ppt.SaveToFile("Output.pptx", FileFormat.Pptx2010);
        }
    }
}

Sometimes, you may have a chart that contains two levels of horizontal axis labels, as shown in the following screenshot, and you need to group the labels by fruit and vegies. This article will show you how to group the category axis labels using Spire.Presentation.

Group Two-Level Axis Labels in a Chart in PowerPoint in C#, VB.NET

Step 1: Create a Presentation instance and load the sample PowerPoint file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("chart.pptx");

Step 2: Get the chart.

IChart chart = ppt.Slides[0].Shapes[0] as IChart;

Step 3: Get the category axis from the chart.

IChartAxis chartAxis = chart.PrimaryCategoryAxis;

Step 4: Determine if the axis has multilevel labels, if yes, group the axis labels that have the same first-level label.

if(chartAxis.HasMultiLvlLbl)
{
    chartAxis.IsMergeSameLabel = true;
}

Step 5: Save the file.

ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);

Output:

Group Two-Level Axis Labels in a Chart in PowerPoint in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Charts;

namespace GroupLabel
{
    class Program
    {
        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("chart.pptx");
            IChart chart = ppt.Slides[0].Shapes[0] as IChart;
            IChartAxis chartAxis = chart.PrimaryCategoryAxis;
            if (chartAxis.HasMultiLvlLbl)
            {
                //group labels
                chartAxis.IsMergeSameLabel = true;
            }
            ppt.SaveToFile("result.pptx", FileFormat.Pptx2010);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Charts

Namespace GroupLabel
	Class Program
		Private Shared Sub Main(args As String())
			Dim ppt As New Presentation()
			ppt.LoadFromFile("chart.pptx")
			Dim chart As IChart = TryCast(ppt.Slides(0).Shapes(0), IChart)
			Dim chartAxis As IChartAxis = chart.PrimaryCategoryAxis
			If chartAxis.HasMultiLvlLbl Then
				'group labels
				chartAxis.IsMergeSameLabel = True
			End If
			ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)
		End Sub
	End Class
End Namespace

Sometimes, you may want to convert Excel sheet to a high-resolution image, especially when the Excel report contains graphs or pictures. This article will show you how to set image resolution when saving Excel sheet to JPG using Spire.XLS.

Step 1: Create a custom function that you can use to reset the image resolution.

private static Bitmap ResetResolution(Metafile mf, float resolution)
{
    int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
    int height = (int)(mf.Height * resolution / mf.VerticalResolution);
    Bitmap bmp = new Bitmap(width, height);
    bmp.SetResolution(resolution, resolution);
    Graphics g = Graphics.FromImage(bmp);
    g.DrawImage(mf, 0, 0);
    g.Dispose();
    return bmp;
}

Step 2: Create a workbook instance and load the sample Excel file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013);

Step 3: Get the worksheet you want to convert.

Worksheet worksheet = workbook.Worksheets[0];

Step 4: Convert the worksheet to EMF stream.

MemoryStream ms = new MemoryStream();
worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);

Step 5: Create an image from the EMF stream, and call ResetResolution to reset the resolution for the image.

Image image = Image.FromStream(ms);
Bitmap images = ResetResolution(image as Metafile, 300);

Step 6: Save the image in JPG file format.

images.Save("Result.jpg", ImageFormat.Jpeg);

Output:

Convert Excel Sheet to a High-Resolution Image in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Convert
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Chart.xlsx", ExcelVersion.Version2013);
            Worksheet worksheet = workbook.Worksheets[0];

            using (MemoryStream ms = new MemoryStream())
            {

                worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn);
                Image image = Image.FromStream(ms);
                Bitmap images = ResetResolution(image as Metafile, 300);
                images.Save("Result.jpg", ImageFormat.Jpeg);
            }
        }
        private static Bitmap ResetResolution(Metafile mf, float resolution)
        {
            int width = (int)(mf.Width * resolution / mf.HorizontalResolution);
            int height = (int)(mf.Height * resolution / mf.VerticalResolution);
            Bitmap bmp = new Bitmap(width, height);
            bmp.SetResolution(resolution, resolution);
            Graphics g = Graphics.FromImage(bmp);
            g.DrawImage(mf, 0, 0);
            g.Dispose();
            return bmp;
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.IO
Namespace Convert
	Class Program
		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("Chart.xlsx", ExcelVersion.Version2013)
			Dim worksheet As Worksheet = workbook.Worksheets(0)

			Using ms As New MemoryStream()

				worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn)
				Dim image__1 As Image = Image.FromStream(ms)
				Dim images As Bitmap = ResetResolution(TryCast(image__1, Metafile), 300)
				images.Save("Result.jpg", ImageFormat.Jpeg)
			End Using
		End Sub
		Private Shared Function ResetResolution(mf As Metafile, resolution As Single) As Bitmap
			Dim width As Integer = CInt(mf.Width * resolution / mf.HorizontalResolution)
			Dim height As Integer = CInt(mf.Height * resolution / mf.VerticalResolution)
			Dim bmp As New Bitmap(width, height)
			bmp.SetResolution(resolution, resolution)
			Dim g As Graphics = Graphics.FromImage(bmp)
			g.DrawImage(mf, 0, 0)
			g.Dispose()
			Return bmp
		End Function
	End Class
End Namespace

With Spire.XLS, we can set the 3-D rotation easily in C#. The following code example explains how to set the rotation of the 3D chart view in C#. We will use 3D pie chart for example. Firstly, view the original 3D pie chart:

How to set the rotation for the 3D chart on Excel in C#

Code snippet of how to set the 3D rotation for Excel Chart:

Step 1: Create a new instance of workbook and load the sample document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("Sample.xlsx");

Step 2: Get the chart from the first worksheet.

Worksheet sheet = workbook.Worksheets[0];
Chart chart = sheet.Charts[0];

Step 3: Set Rotation of the 3D chart view for X and Y.

//X rotation:
chart.Rotation=30;
//Y rotation:
chart.Elevation = 20;

Step 4: Save the document to file.

workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);

Effective screenshot of the Excel 3D chart rotation:

How to set the rotation for the 3D chart on Excel in C#

Full codes:

using Spire.Xls;
namespace SetRotation
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");

            Worksheet sheet = workbook.Worksheets[0];
            Chart chart = sheet.Charts[0];

            //X rotation:
            chart.Rotation = 30;
            //Y rotation:
            chart.Elevation = 20;

            workbook.SaveToFile("Result.xlsx", ExcelVersion.Version2010);
        }
    }
}

Although Excel files typically store numbers and data, they can also contain images associated with the file. After inserting images into Excel, it may sometimes be necessary to extract the images for use elsewhere, or to remove them to improve simplicity. In this article, you will learn how to programmatically extract or remove images from an Excel document using Spire.XLS for .NET.

Install Spire.XLS for .NET

To begin with, you need to add the DLL files included in the Spire.XLS for .NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.XLS

Extract Images from Excel in C# and VB.NET

Spire.XLS for .NET allows you to extract all images in an Excel worksheet at once. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Loop through to get all pictures in the worksheet using Worksheet.Pictures property.
  • Extract pictures and save them to a specified file path using ExcelPicture.Picture.Save() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace ExtractImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("input.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Get all images in the worksheet
            for (int i = sheet.Pictures.Count - 1; i >= 0; i--)
            {
                ExcelPicture picture = sheet.Pictures[i];

                //Extract the images
                picture.Picture.Save(string.Format(@"Image\image_{0}.png", i));
            }
        }
    }
}

C#/VB.NET: Extract or Delete Images in Excel

Delete Images from Excel in C# and VB.NET

To delete pictures from an Excel worksheet, you can use the Worksheet.Pictures.Remove() method. The following are the detailed steps.

  • Create a Workbook instance.
  • Load a sample Excel file using Workbook.LoadFromFile() method.
  • Get a specific worksheet using Workbook.Worksheets[] property.
  • Loop through all pictures in the worksheet and remove them using Worksheet.Pictures.Remove() method.
  • Save the result document using Workbook.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Xls;

namespace DeleteImages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Workbook instance
            Workbook workbook = new Workbook();

            //Load a sample Excel document
            workbook.LoadFromFile("input0.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Delete all pictures from the worksheet
            for (int i = sheet.Pictures.Count - 1; i >= 0; i--)
            {
                sheet.Pictures[i].Remove();
            }

            //Save the result document
            workbook.SaveToFile("DeleteImages.xlsx", ExcelVersion.Version2016);
        }
    }
}

C#/VB.NET: Extract or Delete Images in Excel

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

With auto-fit feature, you can automatically reduce the font size of text to fit a shape, or you can shrink or enlarge shape to fit the exact size of your text. This article will show you how to auto-fit text or shape using Spire.Presentation with C#, VB.NET.

Step 1: Create an instance of Presentation class.

Presentation presentation = new Presentation();

Step 2: Insert a shape, and set the AutofitType property to Normal, which means the text automatically shrinks to fit shape.

IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

Step 3: Insert a shape, and set the AutofitType property to Shape, which means the shape size automatically decreases or increases to fit text.

IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
textShape2.TextFrame.Text = "Resize shape to fit text.";
textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

Step 4: Save the file.

presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);

Output:

Auto-Fit Text or Shape in PowerPoint in C#, VB.NET

Full Code:

[C#]
using Spire.Presentation;
using System.Drawing;
namespace AutofitinPPT
{
    class Program
    {
        static void Main(string[] args)
        {

            Presentation presentation = new Presentation();

            IAutoShape textShape1 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(50, 50, 150, 80));
            textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape.";
            textShape1.TextFrame.AutofitType = TextAutofitType.Normal;

            IAutoShape textShape2 = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, new RectangleF(350, 50, 150, 80));
            textShape2.TextFrame.Text = "Resize shape to fit text.";
            textShape2.TextFrame.AutofitType = TextAutofitType.Shape;

            presentation.SaveToFile("output.pptx", FileFormat.Pptx2013);
        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports System.Drawing
Namespace AutofitinPPT
	Class Program
		Private Shared Sub Main(args As String())

			Dim presentation As New Presentation()

			Dim textShape1 As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(50, 50, 150, 80))
			textShape1.TextFrame.Text = "Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape. Shrink text to fit shape."
			textShape1.TextFrame.AutofitType = TextAutofitType.Normal

			Dim textShape2 As IAutoShape = presentation.Slides(0).Shapes.AppendShape(ShapeType.Rectangle, New RectangleF(350, 50, 150, 80))
			textShape2.TextFrame.Text = "Resize shape to fit text."
			textShape2.TextFrame.AutofitType = TextAutofitType.Shape

			presentation.SaveToFile("output.pptx", FileFormat.Pptx2013)
		End Sub
	End Class
End Namespace
Tuesday, 06 February 2018 07:37

How to explode a doughnut chart in C#

In the previous article, we have demonstrated how to explode a pie chart via Spire.XLS. This article we will explain how to explode a doughnut chart in C#.

When we need to explode a doughnut chart, we can set the chart type as DoughnutExploded when we create a doughnut chart from scratch. For the existing doughnut chart, we can easily change the chart type to explode the doughnut chart.

Step 1: Create an instance of Excel workbook and load the document from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("DoughnutChart.xlsx",ExcelVersion.Version2010);

Step 2: Get the first worksheet from the sample workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Get the first chart from the first worksheet.

Chart chart = sheet.Charts[0];

Step 4: Set the chart type as DoughnutExploded to explode the doughnut chart.

chart.ChartType = ExcelChartType.DoughnutExploded;

Step 5: Save the document to file.

workbook.SaveToFile("ExplodedDoughnutChart.xlsx", ExcelVersion.Version2010);

Effective screenshot of the explode doughnut chart:

How to explode a doughnut chart in C#

Full codes of how to explode a doughnut chart:

using Spire.Xls;
namespace ExplodeDoughnut
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("DoughnutChart.xlsx", ExcelVersion.Version2010);

            Worksheet sheet = workbook.Worksheets[0];
            Chart chart = sheet.Charts[0];
            chart.ChartType = ExcelChartType.DoughnutExploded;

            workbook.SaveToFile("ExplodedDoughnutChart.xlsx", ExcelVersion.Version2010);
        }

    }
}
Tuesday, 06 February 2018 07:32

Resize Images in a Word document in C#

We often need to resize images to a desired size after we insert them into a Word document. This article demonstrates how to programmatically resize images in a Word document using Spire.Doc and C#.

Below is the screenshot of the example document we used for demonstration:

Resize Images in a Word document in C#

Detail steps:

Step 1: Load the Word document.

Document document = new Document("Input.docx");

Step 2: Get the first section and the first paragraph in the section.

Section section = document.Sections[0];
Paragraph paragraph = section.Paragraphs[0];

Step 3: Resize images in the paragraph.

foreach (DocumentObject docObj in paragraph.ChildObjects)
{
    if (docObj is DocPicture)
    {
        DocPicture picture = docObj as DocPicture;
        picture.Width = 50f;
        picture.Height = 50f;
    }
} 

Step 4: Save the document.

document.SaveToFile("ResizeImages.docx");

Output:

Resize Images in a Word document in C#

Full code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
namespace Resize
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document("Input.docx");
            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[0];

            foreach (DocumentObject docObj in paragraph.ChildObjects)
            {
                if (docObj is DocPicture)
                {
                    DocPicture picture = docObj as DocPicture;
                    picture.Width = 50f;
                    picture.Height = 50f;
                }
            }
            document.SaveToFile("ResizeImages.docx");
        }
    }
}