Knowledgebase (2343)
Children categories
Generally, a hyperlink is a picture or a piece of text which contains a web link that points to another object. The object can be a website, an email address and a file, etc. By clicking on the hyperlink, readers can access the target place quickly and conveniently.
This article demonstrates how to insert hyperlink in excel for WPF Applications by using Spire.XLS for WPF.
Below is the effective screenshot:

In this sample, we added two hyperlinks: one for cell A5, another for cell D13. Now, refer to the following detail steps:
Code snippets:
Use namespace:
using System.Windows; using Spire.Xls;
Step 1: Initialize a new instance of Workbook class and load the excel document from file, then get its first worksheet.
Workbook workbook = new Workbook();
workbook.LoadFromFile("Vendors Information.xlsx");
Worksheet sheet = workbook.Worksheets[0];
Step 2: In the first worksheet, add hyperlink to cell A5, set its display text as the existing text of cell A5, next set type and address.
HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]); Link.TextToDisplay = sheet.Range["A5"].Text; Link.Type = HyperLinkType.Url; Link.Address = "https://en.wikipedia.org/wiki/Canada";
Step 3: Add a new hyperlink to cell D13 and set its display text, type and address.
HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]); NewLink.TextToDisplay = "https://www.google.com"; NewLink.Type = HyperLinkType.Url; NewLink.Address = "https://www.google.com";
Step 4: Save and launch the file.
workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("Hyperlink.xlsx");
Full codes:
private void button1_Click(object sender, RoutedEventArgs e)
{
Workbook workbook = new Workbook();
workbook.LoadFromFile("Vendors Information.xlsx");
Worksheet sheet = workbook.Worksheets[0];
HyperLink Link = sheet.HyperLinks.Add(sheet.Range["A5"]);
Link.TextToDisplay = sheet.Range["A5"].Text;
Link.Type = HyperLinkType.Url;
Link.Address = "https://en.wikipedia.org/wiki/Canada";
HyperLink NewLink = sheet.HyperLinks.Add(sheet.Range["D13"]);
NewLink.TextToDisplay = "https://www.google.com";
NewLink.Type = HyperLinkType.Url;
NewLink.Address = "https://www.google.com";
workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010);
System.Diagnostics.Process.Start("Hyperlink.xlsx");
}
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
Dim workbook As New Workbook()
workbook.LoadFromFile("Vendors Information.xlsx")
Dim sheet As Worksheet = workbook.Worksheets(0)
Dim Link As HyperLink = sheet.HyperLinks.Add(sheet.Range("A5"))
Link.TextToDisplay = sheet.Range("A5").Text
Link.Type = HyperLinkType.Url
Link.Address = "https://en.wikipedia.org/wiki/Canada"
Dim NewLink As HyperLink = sheet.HyperLinks.Add(sheet.Range("D13"))
NewLink.TextToDisplay = "https://www.google.com"
NewLink.Type = HyperLinkType.Url
NewLink.Address = "https://www.google.com"
workbook.SaveToFile("Hyperlink.xlsx", ExcelVersion.Version2010)
System.Diagnostics.Process.Start("Hyperlink.xlsx")
End Sub
We often use shape to the PDF file to make it obvious and show the data clearly. With Spire.PDF for WPF, developers can easily use the object Spire.Pdf.PdfPageBase.Canvas to add different kind of shapes, such as rectangle, circle and Ellipse, etc. This article will demonstrate how to add shapes to PDF in C# on WPF applications.
Note: Before Start, please download the latest version of Spire.PDF and add Spire.Pdf.Wpf.dll in the bin folder as the reference of Visual Studio.
Here comes to the steps of how to add shapes to PDF file in C#:
Step 1: Create a new PDF document and add a page to it.
PdfDocument pdfDoc = new PdfDocument(); PdfPageBase page = pdfDoc.Pages.Add();
Step 2: Save graphics state.
PdfGraphicsState state = page.Canvas.Save();
Step 3: Set the color for the shapes.
PdfPen pen = new PdfPen(System.Drawing.Color.ForestGreen, 0.1f); PdfPen pen1 = new PdfPen(System.Drawing.Color.Red, 3f); PdfBrush brush = new PdfSolidBrush(System.Drawing.Color.DeepSkyBlue);
Step 4: Draw shapes and set the position for them.
page.Canvas.DrawRectangle(pen, new System.Drawing.Rectangle(new System.Drawing.Point(2, 22), new System.Drawing.Size(120, 120))); page.Canvas.DrawPie(pen1, 150, 30, 100, 90, 360, 360); page.Canvas.DrawEllipse(brush, 350, 40, 20, 60);
Step 5: Restore graphics.
page.Canvas.Restore(state);
Step 6: Save the PDF document to file.
pdfDoc.SaveToFile("Shapes.pdf");
Effective screenshots of the adding shapes to PDF file:

Full codes:
private void button1_Click(object sender, RoutedEventArgs e)
{
PdfDocument pdfDoc = new PdfDocument();
PdfPageBase page = pdfDoc.Pages.Add();
PdfGraphicsState state = page.Canvas.Save();
PdfPen pen = new PdfPen(System.Drawing.Color.ForestGreen, 0.1f);
PdfPen pen1 = new PdfPen(System.Drawing.Color.Red, 3f);
PdfBrush brush = new PdfSolidBrush(System.Drawing.Color.DeepSkyBlue);
//draw rectangle
page.Canvas.DrawRectangle(pen, new System.Drawing.Rectangle(new System.Drawing.Point(2, 22), new System.Drawing.Size(120, 120)));
//draw circle
page.Canvas.DrawPie(pen1, 150, 30, 100, 90, 360, 360);
//draw ellipse
page.Canvas.DrawEllipse(brush, 350, 40, 20, 60);
page.Canvas.Restore(state);
pdfDoc.SaveToFile("Shapes.pdf");
}
The simplest and most efficient way to keep your Excel file confidential is to encrypt the whole workbook or the specified worksheet with password. Apart from setting a password for your document, you could also choose whether to protect workbook structure or protect worksheet with a certain protection type.
In this article, I am going to introduce how to password protect a workbook or a worksheet as well as set specified permissions to control what types of changes people can make to this workbook using Spire.XLS for WPF.
Code Snippets:
Step 1: Initialize a new instance of workbook class and load an existing Excel file.
Workbook wb = new Workbook();
wb.LoadFromFile("sample.xlsx", ExcelVersion.Version2013);
Step 2: Call Workbook.Protect(string passwordToOpen, bool bIsProtectWindow, bool bIsProtectContent) to protect the workbook with password and also protect the workbook window and structure.
wb.Protect("password-1",true,true);
Step 3: Get the third worksheet from workbook, call protect method of XlsWorksheetBase class to encrypt the sheet with password and set the protection type as None which represents no changes are allowed in the protected sheet. Besides None, there are 17 others in SheetProtectionType enum that can help you to set different permissions to command what users can do to this worksheet.
Worksheet sheet = wb.Worksheets[2];
sheet.Protect("password-2", SheetProtectionType.None);
Step 4: Save and launch the file.
wb.SaveToFile("ProtectedExcel.xlsx");
System.Diagnostics.Process.Start("ProtectedExcel.xlsx");
Output:
Protect Workbook

Protect Worksheet

Full Code:
using Spire.Xls;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Workbook wb = new Workbook();
wb.LoadFromFile("sample.xlsx", ExcelVersion.Version2013);
wb.Protect("password-1", true, true);
Worksheet sheet = wb.Worksheets[2];
sheet.Protect("password-2", SheetProtectionType.None);
wb.SaveToFile("ProtectedExcel.xlsx");
System.Diagnostics.Process.Start("ProtectedExcel.xlsx");
}
}
}
Imports Spire.Xls
Imports System.Windows
Namespace WpfApplication1
Public Partial Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub button1_Click(sender As Object, e As RoutedEventArgs)
Dim wb As New Workbook()
wb.LoadFromFile("sample.xlsx", ExcelVersion.Version2013)
wb.Protect("password-1", True, True)
Dim sheet As Worksheet = wb.Worksheets(2)
sheet.Protect("password-2", SheetProtectionType.None)
wb.SaveToFile("ProtectedExcel.xlsx")
System.Diagnostics.Process.Start("ProtectedExcel.xlsx")
End Sub
End Class
End Namespace