page 218

We always use chart in Excel documents to make the data more clear and visible. With the help of Spire.XLS, developers can easily create different kinds of charts, such as Pie chart, column chart, bar chart, radar chart and so on. This article will focus on demonstrate how to create Excel pie chart on WPF applications.

Note: Before Start, please download the latest version of Spire.XLS and add Spire.Xls.Wpf.dll in the bin folder as the reference of Visual Studio.

Here comes to the code snippets of how to create Excel pie chart:

Step 1: Create a new Excel workbook and load from file.

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

Step 2: Get the first worksheet from workbook.

Worksheet sheet = workbook.Worksheets[0];

Step 3: Add Chart and set chart type.

Chart chart = sheet.Charts.Add(ExcelChartType.Pie);

Step 4: Set chart data range.

chart.DataRange = sheet.Range["B2:B8"];
chart.SeriesDataFromRange = false;

Step 5: Set the position, border, title and legend for the chart.

//Chart Position
chart.LeftColumn = 1;
chart.TopRow =12;
chart.RightColumn = 8;
chart.BottomRow = 26;

//Chart Border
chart.ChartArea.Border.Weight = ChartLineWeightType.Medium;
chart.ChartArea.Border.Color = System.Drawing.Color.SandyBrown;

//Chart Title
chart.ChartTitle = "Parts Sales Info";
chart.ChartTitleArea.Font.FontName = "Calibri";
chart.ChartTitleArea.Font.Size = 10;
chart.ChartTitleArea.Font.IsBold = true;

//Chart Legend
chart.Legend.Position = LegendPositionType.Right;

Step 6: Save the document to file and launch to preview it.

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

Effective screenshot of the generated pie chart by Spire.XLS for WPF:

How to create Excel pie chart in C# on WPF applications

Full codes:

using Spire.Xls;
using System.Windows;

namespace WpfApplication1
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("Sample.xlsx");
            Worksheet sheet = workbook.Worksheets[0];

            Chart chart = sheet.Charts.Add(ExcelChartType.Pie);

            chart.DataRange = sheet.Range["B2:B8"];
            chart.SeriesDataFromRange = false;

            chart.LeftColumn = 1;
            chart.TopRow = 12;
            chart.RightColumn = 8;
            chart.BottomRow = 26;

            chart.ChartArea.Border.Weight = ChartLineWeightType.Medium;
            chart.ChartArea.Border.Color = System.Drawing.Color.SandyBrown;

            chart.ChartTitle = "Parts Sales Info";
            chart.ChartTitleArea.Font.FontName = "Calibri";
            chart.ChartTitleArea.Font.Size = 10;
            chart.ChartTitleArea.Font.IsBold = true;

            chart.Legend.Position = LegendPositionType.Right;

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

How to Add Annotation to PDF in WPF

2016-03-25 09:09:15 Written by Koohji

An annotation is a note or comment added to a pdf file, it can be an explanation or a reference to the specific part of the original data. Add annotations can make the file becomes easier to understand for readers.

Spire.PDF enables us to add, edit and delete annotation from a PDF document. This section will demonstrate how to add text annotation and edit annotation in pdf from WPF Applications using Spire.PDF for WPF.

This is the effective screenshot after adding and editing annotation:

Add Annotation to PDF in WPF

After editing, the text content of the annotation changed from “Demo” to “This is an annotation”.

At first, use the following namespace:

using System.Drawing;
using System.Windows;
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Graphics;

Then refer to the detail steps below:

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

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

Step 2: Draw text in the page and set text font, font size, color and location.

PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 15);
string text = "Spire.PDF";
PdfSolidBrush brush = new PdfSolidBrush(Color.DeepSkyBlue);
PointF point = new PointF(50, 100);
page.Canvas.DrawString(text, font, brush, point);

Step 3: Add annotation.

First, create a text annotation using the PdfTextMarkupAnnotation class:

PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Author", "Demo", text, new PointF(0, 0), font);

There are four parameters of the annotation:

“Author”: the author of annotation.
“Demo”: text content of annotation.
text: the text which will be marked.
new PointF(0, 0): not supported at present.
font: the font of the text which will be marked.

Second, Set Border, TextMarkupColor and location of the annotation:

annotation.Border = new PdfAnnotationBorder(0.50f);
annotation.TextMarkupColor = Color.LemonChiffon;
annotation.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);

Third, add the annotation to the specified page:

(page as PdfNewPage).Annotations.Add(annotation);

If you need to edit the text of the annotation, then you can use following code:

(page as PdfNewPage).Annotations[0].Text = "This is an annotation";

Step 4: Save and launch the file.

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

Full codes:

private void button1_Click(object sender, RoutedEventArgs e)
{
    PdfDocument doc = new PdfDocument();
    PdfPageBase page = doc.Pages.Add();
    PdfFont font = new PdfFont(PdfFontFamily.TimesRoman, 15);
    string text = "Spire.PDF";
    PdfSolidBrush brush = new PdfSolidBrush(Color.DeepSkyBlue);
    PointF point = new PointF(50, 100);
    page.Canvas.DrawString(text, font, brush, point);
    
    //Add annotation
    PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation("Author", "Demo", text, new PointF(0, 0), font);
    annotation.Border = new PdfAnnotationBorder(0.50f);
    annotation.TextMarkupColor = Color.LemonChiffon;

    annotation.Location = new PointF(point.X + doc.PageSettings.Margins.Left, point.Y + doc.PageSettings.Margins.Left);
(page as PdfNewPage).Annotations.Add(annotation);

    //Edit the text of the annotation
    (page as PdfNewPage).Annotations[0].Text = "This is an annotation";

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

Freeze Excel panes in WPF

2016-03-25 08:54:49 Written by Koohji

Whenever you are working with lots of data, it can be difficult to compare information in your workbook. You may want to see certain rows or columns all the time in your worksheet, especially header cells. By freezing rows or columns in place, you can keep rows or columns visible while scrolling through the rest of the worksheet.

In the following sections, I will demonstrate how to freeze Excel panes in WPF.

Step 1: Initialize a new instance of Workbook class. Load the word document from the file.

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

Step 2: In our example, my workbook has several worksheets. We want to check the data from the second worksheet. Therefore, we select the second worksheet.

Worksheet sheet = workbook.Worksheets[1];

Step 3: In this case, we want to fix the first two rows and the leftmost column. The row and column will be frozen in place, as indicated by the solid grey line.

sheet.FreezePanes(2, 1);

Step 4: Save the workbook and launch the file.

workbook.SaveToFile("SalesReport Result.xlsx");
System.Diagnostics.Process.Start("SalesReport Result.xlsx");

Effective screenshot:

Freeze Excel panes in WPF

Full Codes:

[C#]
using System.Windows;
using Spire.Xls;

namespace SalesReport
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    { 
        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            //Load Excel File
            Workbook workbook = new Workbook();
            workbook.LoadFromFile("SalesReport.xlsx");

            //Select the second worksheet

            Worksheet sheet = workbook.Worksheets[1];

            //Select to freeze the first two rows and the leftmost column

            sheet.FreezePanes(3, 2);

            //Save and Launch

            workbook.SaveToFile("SalesReport Result.xlsx");
            System.Diagnostics.Process.Start("SalesReport Result.xlsx");
        }
    }
}
[VB.NET]
Imports System.Windows
Imports Spire.Xls

Namespace SalesReport
	''' 
	''' Interaction logic for MainWindow.xaml
	''' 
	Public Partial Class MainWindow
		Inherits Window
		Public Sub New()
			InitializeComponent()
		End Sub

		Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
			'Load Excel File
			Dim workbook As New Workbook()
			workbook.LoadFromFile("SalesReport.xlsx")

			'Select the second worksheet

			Dim sheet As Worksheet = workbook.Worksheets(1)

			'Select to freeze the first two rows and the leftmost column

			sheet.FreezePanes(3, 2)

			'Save and Launch

			workbook.SaveToFile("SalesReport Result.xlsx")
			System.Diagnostics.Process.Start("SalesReport Result.xlsx")
		End Sub
	End Class
End Namespace
page 218