A combination chart that combines two or more chart types in a single chart is often used to emphasize different types of information in that chart. As is shown in the below Excel sheet, we have different type of data in series 3. To clearly display data of different types, it can be helpful to plot varying data sets either with different chart types or on different axes.

In this article, we will introduce how to combine different chart types in one chart and how to add a secondary axis to a chart using Spire.XLS in C#, VB.NET.

How to Create a Combination Chart in Excel in C#, VB.NET

Code Snippet:

Step 1: Create a new instance of Workbook class and the load the sample Excel file.

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

Step 2: Get the first worksheet from workbook.

Worksheet sheet=workbook.Worksheets[0];

Step 3: Add a chart to worksheet based on the data from A1 to D5.

Chart chart = sheet.Charts.Add();
chart.DataRange = sheet.Range["A1:D5"];
chart.SeriesDataFromRange = false;

Step 4: Set position of chart.

chart.LeftColumn = 6;
chart.TopRow = 1;
chart.RightColumn = 12;
chart.BottomRow = 13;

Step 5: Apply Column chart type to series 1 and series 2, apply Line chart type to series 3.

var cs1 = (ChartSerie)chart.Series[0];
cs1.SerieType = ExcelChartType.ColumnClustered;

var cs2 = (ChartSerie)chart.Series[1];
cs2.SerieType = ExcelChartType.ColumnClustered;

var cs3 = (ChartSerie)chart.Series[2];
cs3.SerieType = ExcelChartType.LineMarkers;

Step 6: Add a secondary axis to the chart, plot data of series 3 on the secondary axis.

chart.SecondaryCategoryAxis.IsMaxCross = true;
cs3.UsePrimaryAxis = false;

Step 7: Save and launch the file

workbook.SaveToFile("result.xlsx",ExcelVersion.Version2010);
System.Diagnostics.Process.Start("result.xlsx");

Result:

How to Create a Combination Chart in Excel in C#, VB.NET

Full Code:

[C#]
using Spire.Xls;
using Spire.Xls.Charts;
namespace CreateCombinationExcel
{
    class Program
    {

        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("data.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            //add a chart based on the data from A1 to D5
            Chart chart = sheet.Charts.Add();
            chart.DataRange = sheet.Range["A1:D5"];
            chart.SeriesDataFromRange = false;

            //set position of chart
            chart.LeftColumn = 6;
            chart.TopRow = 1;
            chart.RightColumn = 12;
            chart.BottomRow = 13;

            //apply different chart type to different series
            var cs1 = (ChartSerie)chart.Series[0];
            cs1.SerieType = ExcelChartType.ColumnClustered;
            var cs2 = (ChartSerie)chart.Series[1];
            cs2.SerieType = ExcelChartType.ColumnClustered;
            var cs3 = (ChartSerie)chart.Series[2];
            cs3.SerieType = ExcelChartType.LineMarkers;

            //add a secondary axis to chart 
            chart.SecondaryCategoryAxis.IsMaxCross = true;
            cs3.UsePrimaryAxis = false;

            //save and launch the file
            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("result.xlsx");
        }
    }
}
[VB.NET]
Imports Spire.Xls
Imports Spire.Xls.Charts
Namespace CreateCombinationExcel
	Class Program

		Private Shared Sub Main(args As String())
			Dim workbook As New Workbook()
			workbook.LoadFromFile("data.xlsx")
			Dim sheet As Worksheet = workbook.Worksheets(0)

			'add a chart based on the data from A1 to D5
			Dim chart As Chart = sheet.Charts.Add()
			chart.DataRange = sheet.Range("A1:D5")
			chart.SeriesDataFromRange = False

			'set position of chart
			chart.LeftColumn = 6
			chart.TopRow = 1
			chart.RightColumn = 12
			chart.BottomRow = 13

			'apply different chart type to different series
			Dim cs1 = DirectCast(chart.Series(0), ChartSerie)
			cs1.SerieType = ExcelChartType.ColumnClustered
			Dim cs2 = DirectCast(chart.Series(1), ChartSerie)
			cs2.SerieType = ExcelChartType.ColumnClustered
			Dim cs3 = DirectCast(chart.Series(2), ChartSerie)
			cs3.SerieType = ExcelChartType.LineMarkers

			'add a secondary axis to chart 
			chart.SecondaryCategoryAxis.IsMaxCross = True
			cs3.UsePrimaryAxis = False

			'save and launch the file
			workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010)
			System.Diagnostics.Process.Start("result.xlsx")
		End Sub
	End Class
End Namespace

Spire.Presentation enables developers to export the whole presentation slides with table, chart and shape to image. And it also supports to export the single element in the presentation slides, such as chart, table and shape into image. This article will show you how to save chart and table on Presentation Slide as image in C# and please check the steps as below:

Step 1: Create a presentation document and load from file.

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

Step 2: Traverse every shape in the slide. Call ShapeList.SaveAsImage(int shapeIndex) to save table and chart as image.

//Save table as image in .Png format
Image image = ppt.Slides[0].Shapes.SaveAsImage(4);
image.Save("table.png", System.Drawing.Imaging.ImageFormat.Png);

//Save chart as image in .Jpeg format
image = ppt.Slides[1].Shapes.SaveAsImage(3);
image.Save("chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

You can use Shapes.SaveAsEMF method to save the chart and table to image in EMF format.

Effective screenshot:

Save Presentation table as image:

How to save Chart and table on Presentation Slide as image in C#

Save Presentation chart as image:

How to save Chart and table on Presentation Slide as image in C#

Full codes:

using Spire.Presentation;
using System.Drawing;

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

            Image image = ppt.Slides[0].Shapes.SaveAsImage(4);
            image.Save("table.png", System.Drawing.Imaging.ImageFormat.Png);
            image = ppt.Slides[1].Shapes.SaveAsImage(3);
            image.Save("chart.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

            //ppt.Slides[1].Shapes.SaveAsEMF(3, "chart.emf");  
        }
    }
}

When you protect your PDF documents with passwords you can optionally specify a set of permissions. The permissions determine how users can interact with the file. For example, you can apply permissions to your document to prohibit the user from printing or using cut and paste operations. This article demonstrates how to change security permissions of PDF documents using Spire.PDF for .NET in C# and VB.NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF 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.PDF

Change Security Permissions of a PDF Document

The following are the steps to apply security permissions to a PDF document using Spire.PDF for .NET.

  • Create a PdfDocument object.
  • Load a sample PDF file using PdfDocument.LoadFileFile() method.
  • Specify open password and permission password. The open password can be set to empty so that the generated document will not require a password to open.
  • Encrypt the document with the open password and permission password, and set the security permissions using PdfDocument.Security.Encypt() method. This method takes PdfPermissionsFlags enumeration as a parameter, which defines user access permissions for an encrypted document.
  • Save the document to another PDF file using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;
using Spire.Pdf.Security;

namespace ChangeSecurityPermission
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument object
            PdfDocument doc = new PdfDocument();

            //Load a sample PDF file
            doc.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //Specify open password
            string openPsd = string.Empty;

            //Specify permission password
            string permissionPsd = "e-iceblue";

            //Encrypt the document with open password and permission password, and set the permissions and encryption key size
            doc.Security.Encrypt(openPsd, permissionPsd, PdfPermissionsFlags.FullQualityPrint, PdfEncryptionKeySize.Key128Bit);

            //Save the document to another PDF file
            doc.SaveToFile("SecurityPermissions.pdf");
        }
    }
}

C#/VB.NET: Change Security Permissions of PDF Documents

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.

Columns are widely used to set page layout, for which could split text into two or more columns so that the text could flow from one column to the next on the same page. Using Spire.Doc, we could achieve this feature and add a line between columns at the same time. This article is going to introduce how to split text into two columns and add line between them.

Note: please download the latest version of Spire.Doc and add Spire.Doc .dll as reference of Visual Studio.

Step 1: Create a new document and load from file

Document document = new Document();
document.LoadFromFile("S.docx");

Step 2: Add a column to section one, set the width of columns and the spacing between columns. Here we set width as 100f and spacing as 20f.

document.Sections[0].AddColumn(100f, 20f);

Step 3: Add a line between the two columns

document.Sections[0].PageSetup.ColumnsLineBetween = true;

Step 4: Save the document and launch to see effects

document.SaveToFile("result.docx",FileFormat.docx2013);
System.Diagnostics.Process.Start("result.docx");

Before adding the columns:

How to split text into two columns and add line between them

Effects:

How to split text into two columns and add line between them

Full Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;

namespace Column
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document();

            document.LoadFromFile("S.docx");

            document.Sections[0].AddColumn(100f, 20f);
        
            document.Sections[0].PageSetup.ColumnsLineBetween = true;
            
            document.SaveToFile("result.docx",FileFormat.docx2013);
            System.Diagnostics.Process.Start("result.docx");

        }
    }
}

Text Box has been widely used in word documents to present additional information to readers. As a powerful and easy to use .NET word component, Spire.Doc has powerful function to work with text box. We have already showed you how to insert text box to word document and extract Text from Text Boxes in Word document. In this article let's see how to remove text box from word document in C#.

Spire.Doc supports to remove the particular text box or clear all the text boxes from the word documents. We will show you the details as below. Firstly, check the original word document contains three text boxes on it.

How to remove Text Box from word document in C#

Here comes to the steps of how to remove the text box from word document by using the class of TextBoxes.

Step 1: Create a new document and load from file.

Document Doc = new Document();
Doc.LoadFromFile("Sample.docx");

Step 2: Remove the first text box by using the class of TextBoxes.

Doc.TextBoxes.RemoveAt(0);

Step 3: Save and launch the file.

Doc.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");

Effective screenshot:

How to remove Text Box from word document in C#

If you want to clear all the text boxes at one time, the following code snippet will fulfill it.

Screenshot after clear all the text boxes:

Doc.TextBoxes.Clear();

How to remove Text Box from word document in C#

Full codes:

namespace Removetextbox
{
    class Program
    {
        static void Main(string[] args)
        {
            Document Doc = new Document();
            Doc.LoadFromFile("Sample.docx");
            
            Doc.TextBoxes.RemoveAt(0);
            //Doc.TextBoxes.Clear();

            Doc.SaveToFile("result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");

        }
    }
}
Wednesday, 27 May 2015 03:08

Merge cells in grid via Spire.PDF

Grid also offers more flexible resizing behavior than Table and lighter weight then a Table. It derives from the Panel element which is best used inside of forms. This article is mainly talk about how to merge cells in grid via Spire.PDF.

Prerequisite:

  • Download Spire.PDF for .NET (or Spire.Office for .NET) and install it on your system.
  • Add Spire.PDF.dll as reference in the downloaded Bin folder thought the below path: "..\Spire.PDF\Bin\NET4.0\ Spire.PDF.dll".
  • Check the codes as below in C#:

Here are the detail steps:

Step 1: Create a new PDF document and add a new page.

PdfDocument doc = new PdfDocument();
PdfPageBase page = doc.Pages.Add();

Step 2: Add a new grid with 5 columns and 2 rows, and set height and width.

PdfGrid grid = new PdfGrid();
grid.Columns.Add(5);
float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
for (int j = 0; j < grid.Columns.Count;j++)
{
  grid.Columns[j].Width = width * 0.20f;
}

 PdfGridRow row0 = grid.Rows.Add();
 PdfGridRow row1 = grid.Rows.Add();
 float height = 20.0f;
 for (int i = 0; i < grid.Rows.Count; i++)
 {
      grid.Rows[i].Height = height;
 }

Step 3: Draw the current grid.

grid.Draw(page, new PointF(0, 50));

Step 4: Set the font of the grid and fill some content in the cells. Use RowSpan and ColumnSpan to merge certain cells vertically and horizontally.

row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);
row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true);

row0.Cells[0].Value = "Corporation";
row0.Cells[0].RowSpan = 2;

row0.Cells[1].Value = "B&K Undersea Photo";
row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row0.Cells[1].ColumnSpan = 3;

row0.Cells[4].Value = "World";
row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen;

row1.Cells[1].Value = "Diving International Unlimited";
row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
row1.Cells[1].ColumnSpan = 4;

Step 5: Draw the new grid and save the file, the review it.

grid.Draw(page, new PointF(0, 100));

doc.SaveToFile("result.pdf");
System.Diagnostics.Process.Start("result.pdf");

Following is the result screenshot:

How to merge cells in grid via Spire.PDF

Full Code:

using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Grid;
using System.Drawing;


namespace MergeCells
{
    class Program
    {
        static void Main(string[] args)
        {
            PdfDocument doc = new PdfDocument();
            PdfPageBase page = doc.Pages.Add();

            PdfGrid grid = new PdfGrid();
            grid.Columns.Add(5);
            float width = page.Canvas.ClientSize.Width - (grid.Columns.Count + 1);
            for (int j = 0; j < grid.Columns.Count; j++)
            {
                grid.Columns[j].Width = width * 0.20f;
            }

            PdfGridRow row0 = grid.Rows.Add();
            PdfGridRow row1 = grid.Rows.Add();
            float height = 20.0f;
            for (int i = 0; i < grid.Rows.Count; i++)
            {
                grid.Rows[i].Height = height;
            }

            grid.Draw(page, new PointF(0, 50));

            row0.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold), true);
            row1.Style.Font = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Italic), true);

            row0.Cells[0].Value = "Corporation";
            row0.Cells[0].RowSpan = 2;

            row0.Cells[1].Value = "B&K Undersea Photo";
            row0.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[1].ColumnSpan = 3;

            row0.Cells[4].Value = "World";
            row0.Cells[4].Style.Font = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Bold | FontStyle.Italic), true);
            row0.Cells[4].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row0.Cells[4].Style.BackgroundBrush = PdfBrushes.LightGreen;

            row1.Cells[1].Value = "Diving International Unlimited";
            row1.Cells[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle);
            row1.Cells[1].ColumnSpan = 4;

            grid.Draw(page, new PointF(0, 100));

            doc.SaveToFile("result.pdf");
            System.Diagnostics.Process.Start("result.pdf");
        }
    }
}

Table in word document can make your data more logical and uncluttered, this article is talk about set absolute position of table in word document via Spire.Doc. Here try to realize placing a table on the right of an image on header.

Here are the steps:

Step 1: Create a new word document and add new section.

Document doc = new Document();
Section sec = doc.AddSection();

Step 2: Create header on Section[0].

HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

Step 3: Add new paragraph on header and set HorizontalAlignment of the paragraph as left.

Paragraph paragraph = header.AddParagraph();
paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;

Step 4: Load an image for the paragraph.

DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));

Step 5: Add a table of 4 rows and 2 columns.

Table table = header.AddTable();
table.ResetCells(4, 2);

Step 6: Set the position of the table to the right of the image. Set WrapTextAround is true, HorizPositionAbs is outside, VertRelationTo is margin, and VertPosition is 43 to fit the height of the image.

table.TableFormat.WrapTextAround = true;
table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
table.TableFormat.Positioning.VertPosition = 43;

Step 7: Then add contents for the table, first column alignment set as left ,second column alignment set as right.

String[][] data = {
                    new string[] {"Spire.Doc.left","Spire XLS.right"},
                    new string[] {"Spire.Presentatio.left","Spire.PDF.right"},
                    new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"},
                    new string []{"Spire.DocViewer.left","Spire.BarCode.right"}
                              };
           
            for (int r = 0; r < 4; r++)
            {
                TableRow dataRow = table.Rows[r];
                for (int c = 0; c < 2; c++)
                {
                    if (c == 0)
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        par.Format.HorizontalAlignment = HorizontalAlignment.Left;
                        dataRow.Cells[c].Width = 180;
                    }
                    else
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        par.Format.HorizontalAlignment = HorizontalAlignment.Right;
                        dataRow.Cells[c].Width = 180;
                    }
                }
            }

Step 8: Save the file and review it.

doc.SaveToFile("result.docx", FileFormat.Docx);
System.Diagnostics.Process.Start("result.docx");

Here is the screenshot:

Set position of table in Word Document as outside via Spire.Doc

Full Code:

using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using System;
using System.Drawing;
namespace SetPosition
{
    class Program
    {

        static void Main(string[] args)
        {

            Document doc = new Document();
            Section sec = doc.AddSection();

            HeaderFooter header = doc.Sections[0].HeadersFooters.Header;

            Paragraph paragraph = header.AddParagraph();
            paragraph.Format.HorizontalAlignment = HorizontalAlignment.Left;
            DocPicture headerimage = paragraph.AppendPicture(Image.FromFile(@"1.png"));

            Table table = header.AddTable();
            table.ResetCells(4, 2);

            table.TableFormat.WrapTextAround = true;
            table.TableFormat.Positioning.HorizPositionAbs = HorizontalPosition.Outside;
            table.TableFormat.Positioning.VertRelationTo = VerticalRelation.Margin;
            table.TableFormat.Positioning.VertPosition = 43;


            String[][] data = {
                    new string[] {"Spire.Doc.left","Spire XLS.right"},
                    new string[] {"Spire.Presentatio.left","Spire.PDF.right"},
                    new string[] {"Spire.DataExport.left","Spire.PDFViewe.right"},
                    new string []{"Spire.DocViewer.left","Spire.BarCode.right"}
                              };

            for (int r = 0; r < 4; r++)
            {
                TableRow dataRow = table.Rows[r];
                for (int c = 0; c < 2; c++)
                {
                    if (c == 0)
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        par.Format.HorizontalAlignment = HorizontalAlignment.Left;
                        dataRow.Cells[c].Width = 180;
                    }
                    else
                    {
                        Paragraph par = dataRow.Cells[c].AddParagraph();
                        par.AppendText(data[r][c]);
                        par.Format.HorizontalAlignment = HorizontalAlignment.Right;
                        dataRow.Cells[c].Width = 180;
                    }
                }
            }
            doc.SaveToFile("result.docx",Spire.Doc.FileFormat.Docx);
            System.Diagnostics.Process.Start("result.docx");
        }
    }
}

To display Excel data in a PowerPoint presentation, we can copy and paste the data to a slide as a PowerPoint table, a worksheet object, a picture, or plain text. This article introduces how to insert an Excel sheet in PowerPoint as an OLE object in C# and VB.NET.

In this solution, you'll need to use Spire.XLS to load the Excel file and get the data, and then create OEL object using Spire.Presentation. So, please download Spire.Office and reference the related DLLs in your project.

Workbook book = new Workbook();
book.LoadFromFile(@"data.xlsx");

Step 2: Select the cell range from the first worksheet and save it as image.

Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);

Step 3: Initialize a new instance of Presentation class, add the image to presentation.

Presentation ppt = new Presentation();
IImageData oleImage = ppt.Images.Append(image);

Step 4: Insert an OLE object to presentation based on the Excel data.

Rectangle rec = new Rectangle(60,60,image.Width,image.Height);
using (MemoryStream ms = new MemoryStream())
{
    book.SaveToStream(ms);
    ms.Position = 0;
    Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);
    oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;
    oleObject.ProgId = "Excel.Sheet.12";
}

Step 5: Save the PowerPoint file with the specified format.

ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007);

Output:

How to Embed Excel Object into PowerPoint Slide in C#, VB.NET

Entire Code:

[C#]
using Spire.Presentation;
using Spire.Presentation.Drawing;
using Spire.Xls;
using System.Drawing;
using System.IO;
namespace ExceltoPPT
{

    class Program
    {

        static void Main(string[] args)
        {
            Workbook book = new Workbook();
            book.LoadFromFile(@"data.xlsx");
            Image image = book.Worksheets[0].ToImage(1, 1, 5, 4);

            Presentation ppt = new Presentation();
            IImageData oleImage = ppt.Images.Append(image);
            Rectangle rec = new Rectangle(60, 60, image.Width, image.Height);
            using (MemoryStream ms = new MemoryStream())
            {
                book.SaveToStream(ms);
                ms.Position = 0;
                Spire.Presentation.IOleObject oleObject = ppt.Slides[0].Shapes.AppendOleObject("excel", ms.ToArray(), rec);
                oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage;
                oleObject.ProgId = "Excel.Sheet.12";
            }

            ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007);


        }
    }
}
[VB.NET]
Imports Spire.Presentation
Imports Spire.Presentation.Drawing
Imports Spire.Xls
Imports System.Drawing
Imports System.IO
Namespace ExceltoPPT

	Class Program

		Private Shared Sub Main(args As String())
			Dim book As New Workbook()
			book.LoadFromFile("data.xlsx")
			Dim image As Image = book.Worksheets(0).ToImage(1, 1, 5, 4)

			Dim ppt As New Presentation()
			Dim oleImage As IImageData = ppt.Images.Append(image)
			Dim rec As New Rectangle(60, 60, image.Width, image.Height)
			Using ms As New MemoryStream()
				book.SaveToStream(ms)
				ms.Position = 0
				Dim oleObject As Spire.Presentation.IOleObject = ppt.Slides(0).Shapes.AppendOleObject("excel", ms.ToArray(), rec)
				oleObject.SubstituteImagePictureFillFormat.Picture.EmbedImage = oleImage
				oleObject.ProgId = "Excel.Sheet.12"
			End Using

			ppt.SaveToFile("InsertOle.pptx", Spire.Presentation.FileFormat.Pptx2007)


		End Sub
	End Class
End Namespace
Wednesday, 20 May 2015 03:44

How to add a trendline in a chart

A trendline is used to show the trend of data series and predict future tendency. Totally, there are six different types: linear trendline, logarithmic trendline, polynomial trendline, power trendline, exponential trendline, and moving average trendline. We may choose different trendline based on the data type we have for different trendline has different advantages to show trend.

For example, a linear trendline works well to show the increase or decrease of data at a steady rate while a logarithmic trendline is often used to show the rate of change in the data increases or decreases quickly and then levels out.

Spire.XLS supports to add all of those six trendlines in charts. This article is going to show how to add trendlines in visual studio.

Before adding trendlines

How to add a trendline in a chart

After adding trendlines

How to add a trendline in a chart

Step 1: Load the workbook and create a sheet

Workbook workbook = new Workbook();
workbook.LoadFromFile("S2.xlsx");
Worksheet sheet = workbook.Worksheets[0];

Step 2: Select targeted chart, add trendline and set its type

//select chart and set logarithmic trendline
Chart chart = sheet.Charts[0];
chart.Series[0].TrendLines.Add(TrendLineType.Logarithmic);
//select chart and set moving_average trendline
Chart chart1 = sheet.Charts[1];
chart1.Series[0].TrendLines.Add(TrendLineType.Moving_Average);
//select chart and set linear trendline
Chart chart2 = sheet.Charts[2];
chart2.Series[0].TrendLines.Add(TrendLineType.Linear);
//select chart and set exponential trendline
Chart chart3 = sheet.Charts[3];
chart3.Series[0].TrendLines.Add(TrendLineType.Exponential);

Step 3: Save and launch the document

workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("S3.xlsx");

Full code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;

namespace TradeLine
{
    class Program
    {
        static void Main(string[] args)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("S2.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            
            Chart chart = sheet.Charts[0];
            chart.Series[0].TrendLines.Add(TrendLineType.Logarithmic);
            
            Chart chart1 = sheet.Charts[1];
            chart1.Series[0].TrendLines.Add(TrendLineType.Moving_Average);
            
            Chart chart2 = sheet.Charts[2];
            chart2.Series[0].TrendLines.Add(TrendLineType.Linear);
            
            Chart chart3 = sheet.Charts[3];
            chart3.Series[0].TrendLines.Add(TrendLineType.Exponential);

            workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("S3.xlsx");

        }
    }
}

Please note that we cannot add a trendline to data series in a stacked, 3-D, radar, pie, surface, or doughnut chart because those types of chart don't support trendline.

With the help of Spire.Presentation, developers can insert new hyperlink into PowerPoint Presentation and edit the existing hyperlinks in C# and VB.NET. This article will show you how to add hyperlink to the existing text on presentation slides.

Firstly check the screenshot of the original presentation slides.

How to add hyperlink to the existing text on presentation slides in C#

Here comes to the steps of how to adding hyperlink to an existing text in Presentation slide:

Step 1: Create a new presentation document and load from file.

Presentation ppt = new Presentation();
ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

Step 2: Find the text we want to add link to it.

IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;
TextParagraph tp = shape.TextFrame.TextRange.Paragraph;
string temp = tp.Text;

Step 3: Adding the hyperlink to the existing text.

//split the original text
string textToLink = "Spire.Presentation";
string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None);

//Clear all text
tp.TextRanges.Clear();

//Add new text
TextRange tr = new TextRange(strSplit[0]);
tp.TextRanges.Append(tr);

//Add the hyperlink
tr = new TextRange(textToLink);
tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html";

tp.TextRanges.Append(tr);

Step 4: Save the document to file.

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

Effective screenshot after adding the hyperlink to the existing text:

How to add hyperlink to the existing text on presentation slides in C#

Full codes:

using Spire.Presentation;
using System;
namespace AddHyperlink
{

    class Program
    {

        static void Main(string[] args)
        {
            Presentation ppt = new Presentation();
            ppt.LoadFromFile("Sample.pptx", FileFormat.Pptx2010);

            IAutoShape shape = ppt.Slides[0].Shapes[0] as IAutoShape;
            TextParagraph tp = shape.TextFrame.TextRange.Paragraph;
            string temp = tp.Text;

            //split the original text
            string textToLink = "Spire.Presentation";
            string[] strSplit = temp.Split(new string[] { "Spire.Presentation" }, StringSplitOptions.None);

            //Clear all text
            tp.TextRanges.Clear();

            //Add new text
            TextRange tr = new TextRange(strSplit[0]);
            tp.TextRanges.Append(tr);

            //Add the hyperlink
            tr = new TextRange(textToLink);
            tr.ClickAction.Address = "http://www.e-iceblue.com/Introduce/presentation-for-net-introduce.html";

            tp.TextRanges.Append(tr);

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

        }
    }
}