Shadows make your shapes or pictures especially with transparent background pop out of your slide. They make flat 2 dimensional graphics look like 3 dimensional graphics. This article will show you how we can apply shadow effects to shapes and pictures in PowerPoint using Spire.Presentation.

Apply Shadow Effect to Shape

//Create a Presentation object and get the first slide.
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];

//Add a shape to slide.
RectangleF rect = new RectangleF(30, 80, 300, 120);
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.Solid;
shape.Fill.SolidColor.Color = Color.LightBlue;
shape.Line.FillType = FillFormatType.None;
shape.TextFrame.Text = "This demo shows how to apply shadow effect to shape.";
shape.TextFrame.TextRange.Fill.FillType = FillFormatType.Solid;
shape.TextFrame.TextRange.Fill.SolidColor.Color = Color.Black;

//Create an inner shadow effect through InnerShadowEffect object. 
InnerShadowEffect innerShadow = new InnerShadowEffect();
innerShadow.BlurRadius = 20;
innerShadow.Direction = 0;
innerShadow.Distance = 0;
innerShadow.ColorFormat.Color = Color.Black;

//Apply the shadow effect to shape.
shape.EffectDag.InnerShadowEffect = innerShadow;

//Save to file.
ppt.SaveToFile("ShadowOnShape.pptx", FileFormat.Pptx2010);

Apply Shadow Effect to Shape in PowerPoint in C#

Apply Shadow Effect to Picture

//Create a Presentation object and get the first slide.
Presentation ppt = new Presentation();
ISlide slide = ppt.Slides[0];

//Get the picture path.
string imagePath = "dinosaur.png";
Image image = Image.FromFile(imagePath);
float width = (float)image.Width / 3;
float height = (float)image.Height / 3;

//Add a shape to slide and fill the shape with picture.
RectangleF rect = new RectangleF(80, 80, width, height);
IAutoShape shape = slide.Shapes.AppendShape(ShapeType.Rectangle, rect);
shape.Fill.FillType = FillFormatType.Picture;
shape.Fill.PictureFill.Picture.Url = imagePath;
shape.Fill.PictureFill.FillType = PictureFillType.Stretch;
shape.Line.FillType = FillFormatType.None;

//Choose a preset shadow effect.
PresetShadow presetShadow = new PresetShadow();
presetShadow.Preset = PresetShadowValue.BackLeftPerspective;
presetShadow.ColorFormat.Color = Color.LightGray;

//Apply the shadow effect to shape.
shape.EffectDag.PresetShadowEffect= presetShadow;

//Save to file.
ppt.SaveToFile("ShadowOnPicture.pptx", FileFormat.Pptx2010);

Apply Shadow Effect to Shape in PowerPoint in C#

This article we will demonstrate how to convert the PDF pages to HTML, Word, SVG, XPS, PDF and save them to stream by calling the method PdfDocument.SaveToStream() offered by Spire.PDF. And starts from Spire.PDF version 4.3, it newly supports to convert the defined range of PDF pages and save them to stream.

Save the PDF to stream

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

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

Step 2: Save the document to stream.

MemoryStream ms=new MemoryStream ();
pdf.SaveToStream(ms);

Save the PDF to stream and defined the file format to HTML, Word, SVG, XPS and PDF

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

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf");

Step 2: Save the document to stream and use FileFormat format to define the format.

MemoryStream ms=new MemoryStream ();
pdf.SaveToStream(ms, FileFormat.HTML);

Convert the defined range of PDF pages to HTML, word, SVG, XPS and save them to stream

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

PdfDocument pdf = new PdfDocument();
pdf.LoadFromFile("Sample.pdf"); 

Step 2: Only save some PDF pages to stream by using pdf.SaveToStream(int startIndex, int endIndex, FileFormat format) method; and FileFormat.PDF is not supported.

pdf.SaveToStream(1, 2, FileFormat.SVG);

Full codes of save PDF to stream:

PdfDocument pdf = new PdfDocument();

pdf.LoadFromFile("Sample.pdf");
            
MemoryStream ms=new MemoryStream ();
pdf.SaveToStream(ms);
pdf.SaveToStream(ms, FileFormat.HTML);

pdf.SaveToStream(1, 2, FileFormat.SVG);

A legend is displayed in the chart area by default. However it can be removed from the chart. With Spire.XLS, we can delete the whole legend as well as specific legend entries from Excel chart. This article is going to demonstrate how we can use Spire.XLS to accomplish this function.

Below screenshot shows the Excel chart we used for demonstration:

Delete Legend and Specific Legend Entries from Excel Chart in C#

Delete the whole legend

//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("sample.xlsx");

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

//Get the chart
Chart chart = sheet.Charts[0];

//Delete legend from the chart
chart.Legend.Delete();

//Save the file                        
workbook.SaveToFile("DeleteLegend.xlsx", ExcelVersion.Version2013);

Screenshot:

Delete Legend and Specific Legend Entries from Excel Chart in C#

Delete specific legend entries

//Create a Workbook instance
Workbook workbook = new Workbook();
//Load the Excel file
workbook.LoadFromFile("sample.xlsx");

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

//Get the chart
Chart chart = sheet.Charts[0];

//Delete the first and the second legend entries from the chart
chart.Legend.LegendEntries[0].Delete();
chart.Legend.LegendEntries[1].Delete();

//Save the file                        
workbook.SaveToFile("DeleteLegendEntries.xlsx", ExcelVersion.Version2013);

Screenshot:

Delete Legend and Specific Legend Entries from Excel Chart in C#

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#]
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]
Dim wb As Workbook = 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)
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:

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#]
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);
        }
    }
}
Page 9 of 99