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#]
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]
Dim ppt As Presentation = New Presentation()
ppt.LoadFromFile("chart.pptx")
Dim chart As IChart = ppt.Slides(0).Shapes(0) as IChart
Dim chartAxis As IChartAxis = chart.PrimaryCategoryAxis
if(chartAxis.HasMultiLvlLbl)
{
    'group labels
    chartAxis.IsMergeSameLabel = True
}                 
ppt.SaveToFile("result.pptx", FileFormat.Pptx2010)

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#]
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]
Shared Sub Main(ByVal args() As String)
    Dim workbook As Workbook = New Workbook()
    workbook.LoadFromFile("Chart.xlsx",ExcelVersion.Version2013)‬
    Dim worksheet As Worksheet = workbook.Worksheets(0)


    Imports(MemoryStream ms = New MemoryStream())
    {
 
        worksheet.ToEMFStream(ms, 1, 1, worksheet.LastRow, worksheet.LastColumn)
        Dim image As Image = Image.FromStream(ms)
        Dim images As Bitmap = ResetResolution(image as Metafile, 300)
        images.Save("Result.jpg", ImageFormat.Jpeg)
    }
End Sub
Private Shared Function ResetResolution(ByVal mf As Metafile, ByVal resolution As single) As Bitmap
    Dim width As Integer = CType((mf.Width * resolution / mf.HorizontalResolution), Integer)
    Dim height As Integer = CType((mf.Height * resolution / mf.VerticalResolution), Integer)
    Dim bmp As Bitmap = 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

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:

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);
Tuesday, 27 February 2018 07:22

Delete Images from Excel Worksheet in C#

This article demonstrates how to delete images from an excel worksheet using Spire.XLS component and C#. Below is the screenshot of the sample file we used for demonstration:

Delete Images from Excel Worksheet in C#

Detail steps:

Step 1: Initialize an object of Workbook class and load the excel file.

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

Step 2: Get the first worksheet.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Delete all images from the worksheet.

for (int i = sheet.Pictures.Count-1; i >= 0; i--)
{
    sheet.Pictures[i].Remove();
}

Step 4: Save the file.

workbook.SaveToFile("DeleteImages.xlsx", ExcelVersion.Version2013);

Output:

Delete Images from Excel Worksheet in C#

Full code:

using Spire.Xls;

namespace DeleteImages
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Input.xlsx");

            Worksheet sheet = workbook.Worksheets[0];

            for (int i = sheet.Pictures.Count-1; i >= 0; i--)
            {
                sheet.Pictures[i].Remove();
            }

            workbook.SaveToFile("DeleteImages.xlsx", ExcelVersion.Version2013);
        }
    }
}

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#]
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]
Dim presentation As Presentation =  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)
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:

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:

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");

Error bars in charts can help us to see margins of error and standard deviations at a glance. We can use error bars in area, bar, column, line, and XY charts. In XY charts i.e. scatter and bubble charts, we can show X (Horizontal) and Y (Vertical) error bars. However if the chart type is not XY, the error bars for X values are disabled.

This article demonstrates how to add and format error bars for non-XY and XY charts in a PowerPoint document using Spire.Presentation and C#.

Detail steps:

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

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

Step 2: Get the column chart on the first slide and set chart title.

IChart columnChart = ppt.Slides[0].Shapes[0] as IChart;
columnChart.ChartTitle.TextProperties.Text = "Vertical Error Bars";

Step 3: Add Y (Vertical) error bars to the first chart series and format the error bars.

//Get Y error bars of the first chart series
IErrorBarsFormat errorBarsYFormat1 = columnChart.Series[0].ErrorBarsYFormat;
//Set end cap
errorBarsYFormat1.ErrorBarNoEndCap = false;
//Specify direction
errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus;
//Specify error amount type
errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsYFormat1.ErrorBarVal = 0.3f;            
//Set line format          
errorBarsYFormat1.Line.FillType = FillFormatType.Solid;
errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed;
errorBarsYFormat1.Line.Width = 1;

Step 4: Get the bubble chart on the second slide and set chart title.

IChart chart2 = ppt.Slides[1].Shapes[0] as IChart;
chart2.ChartTitle.TextProperties.Text = "Vertical and Horizontal Error Bars";

Step 5: Add x (Horizontal) and Y (Vertical) error bars to the first chart series and format the error bars.

//Get X error bars of the first chart series
IErrorBarsFormat errorBarsXFormat = bubbleChart.Series[0].ErrorBarsXFormat;
//Set end cap
errorBarsXFormat.ErrorBarNoEndCap = false;
//Specify direction
errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both;

//Specify error amount type
errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsXFormat.ErrorBarVal = 0.3f;
            
//Get Y error bars of the first chart series
IErrorBarsFormat errorBarsYFormat2 = bubbleChart.Series[0].ErrorBarsYFormat;
//Set end cap
errorBarsYFormat2.ErrorBarNoEndCap = false;
//Specify direction
errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both;
//Specify error amount type
errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError;
//Set value
errorBarsYFormat2.ErrorBarVal = 0.3f;

Step 6: Save the document.

ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013);

Screenshot:

Add and Format Error Bars in PowerPoint Charts in C#

Add and Format Error Bars in PowerPoint Charts in C#

Full code:

using System.Drawing;
using Spire.Presentation;
using Spire.Presentation.Charts;
using Spire.Presentation.Drawing;

namespace Add_error_bars_to_chart_in_PPT
{
    class Program
    {
        static void Main(string[] args)
        {
            //Load the PowerPoint document
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Input.pptx");

            //Get the column chart on the first slide and set chart title
            IChart columnChart = ppt.Slides[0].Shapes[0] as IChart;
            columnChart.ChartTitle.TextProperties.Text = "Vertical Error Bars";

            //Add Y (Vertical) Error Bars

            //Get Y error bars of the first chart series
            IErrorBarsFormat errorBarsYFormat1 = columnChart.Series[0].ErrorBarsYFormat;
            //Set end cap
            errorBarsYFormat1.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsYFormat1.ErrorBarSimType = ErrorBarSimpleType.Plus;
            //Specify error amount type
            errorBarsYFormat1.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsYFormat1.ErrorBarVal = 0.3f;            
            //Set line format          
            errorBarsYFormat1.Line.FillType = FillFormatType.Solid;
            errorBarsYFormat1.Line.SolidFillColor.Color = Color.MediumVioletRed;
            errorBarsYFormat1.Line.Width = 1;

            //Get the bubble chart on the second slide and set chart title
            IChart bubbleChart = ppt.Slides[1].Shapes[0] as IChart;
            bubbleChart.ChartTitle.TextProperties.Text = "Vertical and Horizontal Error Bars";

            //Add X (Horizontal) and Y (Vertical) Error Bars

            //Get X error bars of the first chart series
            IErrorBarsFormat errorBarsXFormat = bubbleChart.Series[0].ErrorBarsXFormat;
            //Set end cap
            errorBarsXFormat.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsXFormat.ErrorBarSimType = ErrorBarSimpleType.Both;
            //Specify error amount type
            errorBarsXFormat.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsXFormat.ErrorBarVal = 0.3f;
            
            //Get Y error bars of the first chart series
            IErrorBarsFormat errorBarsYFormat2 = bubbleChart.Series[0].ErrorBarsYFormat;
            //Set end cap
            errorBarsYFormat2.ErrorBarNoEndCap = false;
            //Specify direction
            errorBarsYFormat2.ErrorBarSimType = ErrorBarSimpleType.Both;
            //Specify error amount type
            errorBarsYFormat2.ErrorBarvType = ErrorValueType.StandardError;
            //Set value
            errorBarsYFormat2.ErrorBarVal = 0.3f;
                        
            //Save the document
            ppt.SaveToFile("ErrorBars.pptx", FileFormat.Pptx2013);
        }
    }
}

Spire.Presentation supports to operate the paragraph styles from code. There are two kinds of special indentation styles for the paragraph, first line and hanging. This article will demonstrate how to set the indent style for the paragraph on presentation slide in C#.

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

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

Step 2: Get the paragraphs from the first slide.

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

Step 3: Set the indentation as first line for the first paragraph.

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

Step 4: Set the indentation as Hanging for the third paragraph.

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

Step 5: Save the document to file.

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);

Effective screenshot of the presentation with indentation style:

How to set the indent style for the paragraph on presentation slide in C#

Full codes:

Presentation presentation = new Presentation();
presentation.LoadFromFile("Sample.pptx");

IAutoShape shape = (IAutoShape)presentation.Slides[0].Shapes[0];
ParagraphCollection paras = shape.TextFrame.Paragraphs;

paras[0].Indent = 20;
paras[0].SpaceAfter = 10;

paras[2].Indent = -100;
paras[2].LeftMargin = 30;

presentation.SaveToFile("Result.pptx", FileFormat.Pptx2010);