Setting Excel header and footer to add information to a spreadsheet is a pretty attractive choice. This is the solution for how to set Excel header and footer in C#, VB.NET. Within this solution, you can either set Excel header and footer in C#, VB.NET at design mode or runtime mode.

Headers and Footers are the lines of text that are displayed below the top margin or above the bottom margin respectively. They can be used to display any kind of useful information on the pages like page number, author name, topic name or date/time etc.

Spire.XLS for .NET allows adding headers and footer to the worksheets and setting them at runtime. At the same time, you can also set headers and footers manually in the pre-designed file for printing and then load it as a template. For example, you can use Microsoft Excel as a GUI tool to set headers and footers easily to reduce your efforts and development time and then upload it by using Spire.XLS for .NET. The image below displays the effect of setting Excel header and footer in C#, VB.NET. You can also freely download Spire.XLS for .NET and have a trial.

Add Excel Header and Footer

The next part is the code for setting Excel header and footer in C#, VB.NET.

[C#]
using Spire.Xls;

namespace HeaderAndFooterForXls
{
    class Program
    {
        static void Main(string[] args)
        {
            //create your excel document           
            Workbook myworkbk = new Workbook();
            //Gets first worksheet
            Worksheet mysht = myworkbk.Worksheets[0];
            //Writes hello world to A1
            mysht.Range["A1"].Text = "Hello ";
            //set header&footer
            mysht.PageSetup.LeftHeader = "Header Footer Sample by E-iceblue Co.,Ltd";        
            mysht.PageSetup.CenterFooter = "footer";
            //Save workbook and preview it
            myworkbk.SaveToFile(@"..\Sample.xls",ExcelVersion.Version97to2003);                     
            System.Diagnostics.Process.Start(@"..\Sample.xls");
        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace HeaderAndFooterForXls
	Class Program
		Private Shared Sub Main(args As String())
			'create your excel document           
			Dim myworkbk As New Workbook()
			'Gets first worksheet
			Dim mysht As Worksheet = myworkbk.Worksheets(0)
			'Writes hello world to A1
			mysht.Range("A1").Text = "Hello "
			'set header&footer
			mysht.PageSetup.LeftHeader = "Header Footer Sample by E-iceblue Co.,Ltd"
			mysht.PageSetup.CenterFooter = "footer"
			'Save workbook and preview it
			myworkbk.SaveToFile("..\Sample.xls",ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start("..\Sample.xls")
		End Sub
	End Class
End Namespace

As for headers and footers manually in the pre-designed file, please refer how to use Excel template in C#, VB.NET.

Spire.XLS for .NET is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document for .NET. It supports C#, VB.NET, ASP.NET, ASP.NET MVC.

Wednesday, 25 July 2012 08:05

Convert Word to HTML in WPF

During software development, developers may need to convert Word to HTML. Different from many available ways on Internet, in this example, the solution for converting Word to HTML enables WPF developers load a predefined Doc, Docx, or RTF document, fill it with data, convert the document to HTML.

Spire.Doc for WPF is a professional component specializing in manipulating Word document for WPF. To convert a Word to HTML simply in WPF, developers need to invoke document.LoadFromFile(), which is used to load document and document.SaveToFile(), which is used to convert document.

The following screenshot shows parts of contents of original Word document. The target HTML is at the bottom after this coding. It is shown in Firefox, same as the original document.

Word to HTML

Download and install Spire.Doc for WPF on your system. Then, add a button in MainWindow and double click it to use the following code to convert Word to HTML in WPF

[C#]
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\work\Documents\Humor Them.docx");

//Convert to HTML
document.SaveToFile("ToHTML.html", FileFormat.Html);

//Launch Document
System.Diagnostics.Process.Start("ToHTML.html");
[VB.NET]
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\work\Documents\Humor Them.docx")

'Convert to HTML
document.SaveToFile("ToHTML.html", FileFormat.Html)

'Launch Document
System.Diagnostics.Process.Start("ToHTML.html")

After running, you can get the result as following:

Word to HTML

Spire.Doc is an Microsoft Word component which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF.NET and Silverlight.

Tuesday, 24 July 2012 08:22

Convert Excel to PDF in WPF

Software developers are often asked to find a way to convert Microsoft Excel into PDF as PDF files are widely used for exchanging documents between organizations, government sectors and individuals. This solution demonstrates a way of converting Excel  to PDF for WPF developers and maintains high visual fidelity in the conversion.

Spire.XLS for WPF is a WPF Excel component which enables your WPF applications to fast generate, read, write and modify Excel document without Microsoft Office Excel Automation. It also fully supports converting files from Excel to PDF, Excel to HTML, Excel to CSV, Excel to Text, Excel to Image and Excel to XML. All the conversion is independent of any other software.

Any kind of trial and evaluation is always welcomed. Please feel free to download Spire.XLS for WPF to have a trial and convert your Excel to PDF for personal use. Below is a screenshot of the source Excel file we load in this demo. At the end, a screenshot of PDF will be demonstrated for comparison with the source Excel file.

Excel to PDF

Now this is the key procedure for converting Excel to PDF in WPF.

Step 1: Load Excel file or Create Excel from scratch.

In this step, instantiate an object of the Workbook class by calling its empty constructor and then you may open/load an existing template file or skip this step if you are creating the workbook from scratch.

[C#]
// load Excel file
Workbook workbook = new Workbook();
workbook.LoadFromFile("D:\\test.xlsx");                  

[VB.NET]

' load Excel file
Dim workbook As New Workbook()
workbook.LoadFromFile("D:\test.xlsx")

Step 2: Set PDF template.

In this step, we will set PDF template which will be used below.

[C#]
// Set PDF template
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape;
pdfDocument.PageSettings.Width = 970;
pdfDocument.PageSettings.Height = 850;                  
[VB.NET]
' Set PDF template
Dim pdfDocument As New PdfDocument()
pdfDocument.PageSettings.Orientation = PdfPageOrientation.Landscape
pdfDocument.PageSettings.Width = 970
pdfDocument.PageSettings.Height = 850

Step 3: Convert Excel to PDF in WPF.

Now we will use the template which we have already set above to convert Excel to PDF in WPF.

[C#]
//Convert Excel to PDF using the template above
PdfConverter pdfConverter = new PdfConverter(workbook);
PdfConverterSettings settings = new PdfConverterSettings();
settings.TemplateDocument = pdfDocument;
pdfDocument = pdfConverter.Convert(settings);            
[VB.NET]
'Convert Excel to PDF using the template above
Dim pdfConverter As New PdfConverter(workbook)
Dim settings As New PdfConverterSettings()
settings.TemplateDocument = pdfDocument
pdfDocument = pdfConverter.Convert(settings)

Step 4: Save and preview PDF.

Please use the codes below to save and preview PDF.

[C#]
// Save and preview PDF
pdfDocument.SaveToFile("sample.pdf");
System.Diagnostics.Process.Start("sample.pdf");        
[VB.NET]
' Save and preview PDF
pdfDocument.SaveToFile("sample.pdf")
System.Diagnostics.Process.Start("sample.pdf")

The picture below is a screenshot of the PDF, Please see:

Excel to PDF

Tuesday, 24 July 2012 05:41

Draw PDF Image in WPF

Drawing PDF image in WPF is not as easy as most of developers imagine. Getting a beautifully  matched PDF text and image requires them to properly set both the image size and image position in the PDF document. Any unsuitably handled in each aspect may lead the text covered by the PDF image. This solution is offered for you to draw PDF image in WPF applications without the worry above.

 

Spire.PDF for WPF is a WPF PDF component which provides great convenience in reading, writing and modifying PDF documents without using Adobe Acrobat or any third party component library. Spire.PDF for WPF contains a namespace “Spire.Pdf.Graphics” in which there are four versions of DrawImage method that can be applied to insert PdfImage in PDF document.

 

Please feel free to Download Spire.PDF for WPF and draw PDF Image in WPF, Below is an effective screenshot of the PDF image.

 

Draw Image in PDF

 

Now this is the key procedure of PDF image in WPF.

 

1, Load PDF and image file from system.
Two files are demanded in this step: a PDF file in which the image will be inserted and an image file.

 

[C#]
doc.LoadFromFile(@"..\Sample.pdf");
System.Drawing.Image image = System.Drawing.Image.FromFile(@"..\MS.jpg");
[VB.NET]
doc.LoadFromFile("..\Sample.pdf")
Dim image As System.Drawing.Image = System.Drawing.Image.FromFile("..\MS.jpg")

 

2,Adjust image size and draw image in PDF file.
In this step, one way to use DrawImage method is demonstrated with two parameters: the source image and its position.

 

[C#]
//adjust image size
int width = image.Width;
int height = image.Height;
float schale = 0.7f;
System.Drawing.Size size = new System.Drawing.Size((int)(width * schale), (int)(height * schale));
Bitmap schaleImage = new Bitmap(image, size);
//draw image into the first PDF page at specific position 
PdfImage pdfImage = PdfImage.FromImage(schaleImage);
PdfPageBase page0 = doc.Pages[0];
PointF position = new PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10);
page0.Canvas.DrawImage(pdfImage, position);

[VB.NET]
'adjust image size
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim schale As Single = 0.7F
Dim size As New System.Drawing.Size(CInt(Math.Truncate(width * schale)), CInt(Math.Truncate(height * schale)))
Dim schaleImage As New Bitmap(image, size)
'draw image into the first PDF page at specific position 
Dim pdfImage__1 As PdfImage = PdfImage.FromImage(schaleImage)
Dim page0 As PdfPageBase = doc.Pages(0)
Dim position As New PointF((page0.Canvas.ClientSize.Width - schaleImage.Width) / 8, 10)
page0.Canvas.DrawImage(pdfImage__1, position)

 

It is obvious that Spire.PDF for WPF can realize PDF image task in minutes. Besides, Spire.PDF for WPF also has the functions of PDF protect, PDF conversion, PDF drawing, click to know more about this Professional WPF PDF Components.

 

Thursday, 19 July 2012 07:34

Move Worksheet in WPF

It is not an easy task for WPF developers moving worksheet to another location in a workbook, as calculations or charts that are based on worksheet data might become inaccurate if you move the worksheet in WPF application. Here presents a solution that saves you from these worries. Apply Spire.Xls for WPF in your application,  and you easily can move worksheet in your WPF application.

Spire.XLS for WPF is a professional Excel component which enables developers/programmers to fast generate, read, write and modify Excel document in WPF applications. Spire.XLS for .NET embed a method - Spire.Xls.WorkShee.MoveWorksheet(int destIndex) in its class design used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter and lead no inaccuracy on these calculations or charts.

Now Feel free to download Spire.XLS for WPF and use  the code samples below to move worksheet in WPF application.

[C#]
            //open Excel
                Workbook mywbk=new Workbook();
                mywbk.LoadFromFile(@"..\test.xls");

            // Locate the Worksheet
                Worksheet mysht=mywbk.Worksheets[0];

            //Move Worksheet
                mysht.MoveWorksheet(2);

            //Save and Launch
                mywbk.SaveToFile("result.xls");
                System.Diagnostics.Process.Start("result.xls");
[VB.NET]
	   'open Excel
	    Dim mywbk As New Workbook()
	    mywbk.LoadFromFile("..\test.xls")

	   ' Locate the Worksheet
	    Dim mysht As Worksheet = mywbk.Worksheets(0)

	   'Move Worksheet
	    mysht.MoveWorksheet(2)

	    'Save and Launch
	     mywbk.SaveToFile("result.xls")
	     System.Diagnostics.Process.Start("result.xls")
Wednesday, 18 July 2012 08:49

Move Worksheet in C#, VB.NET

This section will demonstrate a nice solution to use C#, VB.NET to move worksheet within workbook. Within this solution, moving worksheet in C#, VB.NET is as easy as ABC.  Apply Spire.Xls for .NET in your application, and you can easily move worksheet within Excel using C#, VB.NET.

Spire.XLS for .NET is a professional Excel component for .NET which enables developers/programmers to fast generate, read, write and modify Excel document in their applications. It supports C#, VB.NET, ASP.NET, ASP.NET MVC. Spire.XLS for .NET embed a method - Spire.Xls.WorkShee.MoveWorksheet(int destIndex) in its class design used to move a worksheet to another location in the spreadsheet. The method takes the target worksheet index as a parameter.

Below is the whole code samples used to move worksheet in Excel via C#, VB.NET.

[C#]
using Spire.Xls;

namespace moveSheet
{
    class Program
    {
    static void Main(string[] args)
        {
            //open Excel
                Workbook mywbk=new Workbook();
                mywbk.LoadFromFile(@"..\test.xls");

            // Locate the Worksheet
                Worksheet mysht=mywbk.Worksheets[0];

            //Move Worksheet
                mysht.MoveWorksheet(2);

            //Save and Launch
                mywbk.SaveToFile("result.xls",ExcelVersion.Version97to2003);
                System.Diagnostics.Process.Start("result.xls");

        }
    }
}
[VB.NET]
Imports Spire.Xls

Namespace moveSheet
	Class Program
		Private Shared Sub Main(args As String())
			'open Excel
			Dim mywbk As New Workbook()
			mywbk.LoadFromFile("..\test.xls")

			' Locate the Worksheet
			Dim mysht As Worksheet = mywbk.Worksheets(0)

			'Move Worksheet
			mysht.MoveWorksheet(2)

			'Save and Launch
			mywbk.SaveToFile("result.xls",ExcelVersion.Version97to2003)
			System.Diagnostics.Process.Start("result.xls")

		End Sub
	End Class
End Namespace
Wednesday, 18 July 2012 05:44

Quickly Convert Doc/Docx to PDF in WPF

This guide focuses on how to convert Word to PDF with WPF in an easy way. At the beginning, prepare a Word document which you want to convert to PDF. Also, you can set format for the contents to have a good appearance after converting.

Spire.Doc for WPF, professional component to manipulate Word document with no dependency on Microsoft Word, enables users to convert Word to PDF for WPF.

The following screenshot is the Word document which I prepare. This document only includes a title and an image.

Doc to PDF

Download and install Spire.Doc for WPF on your system and then convert Word to PDF with WPF by using the following code.

[C#]
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:\work\Documents\Welcome.docx");

//Convert Word to PDF
document.SaveToFile("WPFWord2PDF.pdf", FileFormat.PDF);
[VB.NET]
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\work\Documents\Welcome.docx")

'Convert Word to PDF
document.SaveToFile("WPFWord2PDF.pdf", FileFormat.PDF)

After running, you can get the result as following:

Doc to PDF

Spire.Doc is an Microsoft Word component which enables users to perform a wide range of Word document processing tasks directly, such as generate, read, write and modify Word document in WPF.NET and Silverlight.

Wednesday, 04 July 2012 08:29

Merge Multiple PDF Files into One in WPF

Before we start our topic, let us first play a game. At the top of this page, there are nine categories. When we move our mouse on .NET, a dropdown list which shows all e-iceblue .NET products is displayed as the picture below:
Merge PDF Documents

Imagine, if we demonstrate every product on .NET out of the dropdown list on the top, what will this page be? The webpage will be rambling and disordered. As a result, visitors will hardly search what they want and finally have to leave disappointedly.

By the same token, if we do not merge PDF files of similar content to one document, all the information of various classes will mix together. We have no choice but to spend much time on finding the right files. While our work turns more efficient when we use PDF merge function. Furthermore, opening more PDF files at the same time always annoys people in that the windows are mistakenly switched. Finally, merge PDF does not mean gathering tens of PDF files into one. More reasonable choice is to amalgamate PDF files of few pages into single one. Those of numerous pages should be combined with merged PDF files in one folder for the convenience of reading.

Spire.PDF for WPF, as a professional PDF component, is a best hand for users to merge multiple PDF files on WPF application with C#, VB.NET. The whole procedure can be performed by below three simple steps.

Step 1 Create a C#/VB.NET Project

  1. Free download and correctly install Spire.PDF for WPF on system.
  2. Create “PDF merge” project in WPF Application with C#/VB.NET.
  3. Add a button in MainWindow.
  4. Add Spire.Pdf.Wpf.dll and System.Drawing as references.

Step 2 Merge PDF Files into One

1. Load three PDF documents from system.
[C#]
            //pdf document list
            String[] files = new String[]
            {
                @"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf",
                @"D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf",
                @"D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"                            
            };
[VB.NET]
'pdf document list
Dim files As [String]() = New [String]() {"D:\e-iceblue\Spire.PDF\Demos\Data\DigitalSignature.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\Sample2.pdf", "D:\e-iceblue\Spire.PDF\Demos\Data\ImageWaterMark.pdf"}
          
2. Open the three PDF files in a new document and import all pages into a new one.
[C#]
           //open pdf documents            
            PdfDocument[] docs = new PdfDocument[files.Length];
            for (int i = 0; i < files.Length; i++)
            {
                docs[i] = new PdfDocument(files[i]);
            }
            //append document
            docs[0].AppendPage(docs[1]);
            //import pages
            for (int i = 0; i < docs[2].Pages.Count; i = i + 2)
            {
                docs[0].InsertPage(docs[2], i);
            }
[VB.NET]
	'open pdf documents            
	Dim docs As PdfDocument() = New PdfDocument(files.Length - 1) {}
	For i As Integer = 0 To files.Length - 1
		docs(i) = New PdfDocument(files(i))
	Next
	'append document
	docs(0).AppendPage(docs(1))
	'import pages
	Dim i As Integer = 0
	While i < docs(2).Pages.Count
		docs(0).InsertPage(docs(2), i)
		i = i + 2
	End While
          

Step3. Save and Preview the Merged PDF File.

[C#]
            docs[0].SaveToFile("MergeDocuments.pdf");
            foreach (PdfDocument doc in docs)
            {
                doc.Close();
            }
            PDFDocumentViewer("MergeDocuments.pdf");
[VB.NET]
docs(0).SaveToFile("MergeDocuments.pdf")
For Each doc As PdfDocument In docs
	doc.Close()
Next
PDFDocumentViewer("MergeDocuments.pdf")
Effective Screeshot
Merge PDF Documents

Spire.PDF is a PDF document creation component that enables your .NET/Silverlight/WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat.

PDF Family
PDF Family has three members: Spire.PDF for .NET, Spire.PDF for Silverlight and Spire.PDF for WPF, which are packed in Spire.PDF. Spire.PDF enables your .NET /Silverlight/WPF applications to easily manage your PDF documents.


Monday, 02 July 2012 06:16

Insert Hyperlink in PDF in C#, VB.NET

PDF Hyperlink replaces huge information with a simple link which satisfies people the need of rendering external information while not occupying much space. Now, it is time to introduce a solution to insert hyperlink in PDF with C#, VB.NET.

Using this .NET PDF component Spire.PDF for .NET, you can not only to insert hyperlink in your PDF file but also set hyperlink format and position to perfectly match the PDF page. First please see below picture as the target PDF file in which there are two hyperlinks are added. Both hyperlinks can automatically take you to a new place:

Insert Hyperlink in PDF

There are two main steps to insert PDF hyperlink. One is to set the position of hyperlink. The other is to insert hyperlinks. Let us see them one by one.

Step 1: Set PDF hyperlink position

When we set PDF hyperlink position, we should make full use of the class of Spire.Pdf: PdfPageBase.Canvas.ClientSize. You can see detail information as below code:

[C#]
            PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
            PdfMargins margin = new PdfMargins();
            margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Bottom = margin.Top;
            margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
            margin.Right = margin.Left;
            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Verdana", 17));
            float space = font.Height * 0.75f;
            float y = page.Canvas.ClientSize.Height * 0.13f - margin.Bottom + space;
[VB.NET]
Dim unitCvtr As New PdfUnitConvertor()
Dim margin As New PdfMargins()
margin.Top = unitCvtr.ConvertUnits(2.54F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Bottom = margin.Top
margin.Left = unitCvtr.ConvertUnits(3.17F, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point)
margin.Right = margin.Left
Dim font As New PdfTrueTypeFont(New Font("Verdana", 17))
Dim space As Single = font.Height * 0.75F
Dim y As Single = page.Canvas.ClientSize.Height * 0.13F - margin.Bottom + space

Step 2: Insert PDF hyperlink and set hyperlink format

There are two kinds of hyperlinks will be added in this step: url link and text link. When we insert url link, we need to assign value for url string.While when we want to insert a text hyperlink we should call DrawTextWebLink () method. If necessary, we also can add label. After adding hyperlinks, PdfPageBase.Canvas.DrawString(string s, PdfFontBase font, PdfBrush brush, float x, float y); method can help us set hyperlink format with six parameters passed.

[C#]
            String label = "File Source: ";
            PdfStringFormat format = new PdfStringFormat();
            format.MeasureTrailingSpaces = true;
            float x1 = font.MeasureString(label, format).Width;
            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Verdana", 17, System.Drawing.FontStyle.Bold));
            String url1 = "http://www.microsoft.com";
            float x2 = font2.MeasureString(url1, format).Width;
            float x = (page.Canvas.ClientSize.Width - x1 - x2) / 2;
            page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format);
            x += x1;
            page.Canvas.DrawString(url1, font2, PdfBrushes.DarkSeaGreen, x, y);
            y = y + font2.MeasureString(url1).Height;

            label = "Simple Link: ";
            x -= x1;
            page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format);
            float xoffset2 = font.MeasureString(label, format).Width;
            x += xoffset2;
            String text = "microsoft";
            PdfTextWebLink link2 = new PdfTextWebLink();
            link2.Text = text;
            link2.Url = url1;
            link2.Font = font2;
            link2.Brush = PdfBrushes.DarkSeaGreen;
            link2.DrawTextWebLink(page.Canvas, new PointF(x, y));
[VB.NET]
Dim label As [String] = "File Source: "
Dim format As New PdfStringFormat()
format.MeasureTrailingSpaces = True
Dim x1 As Single = font.MeasureString(label, format).Width
Dim font2 As New PdfTrueTypeFont(New Font("Verdana", 17, System.Drawing.FontStyle.Bold))
Dim url1 As [String] = "http://www.microsoft.com"
Dim x2 As Single = font2.MeasureString(url1, format).Width
Dim x As Single = (page.Canvas.ClientSize.Width - x1 - x2) / 2
page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format)
x += x1
page.Canvas.DrawString(url1, font2, PdfBrushes.DarkSeaGreen, x, y)
y = y + font2.MeasureString(url1).Height

label = "Simple Link: "
x -= x1
page.Canvas.DrawString(label, font, PdfBrushes.Firebrick, x, y, format)
Dim xoffset2 As Single = font.MeasureString(label, format).Width
x += xoffset2
Dim text As [String] = "microsoft"
Dim link2 As New PdfTextWebLink()
link2.Text = text
link2.Url = url1
link2.Font = font2
link2.Brush = PdfBrushes.DarkSeaGreen
link2.DrawTextWebLink(page.Canvas, New PointF(x, y))

Spire.PDF for .NET is a PDF application which enables users to handle the whole PDF document with wide range of tasks in C#, VB.NET.

Wednesday, 27 June 2012 08:01

Create PDF Digital Certificate in WPF

Digital Certificate, known as identity certificate, is an electronic document that uses a digital signature to bind together a public key with an identity. As long as the PDF digital certificate is created, it demonstrates the authority of a digital document. This section will introduce a solution to create PDF digital certificate for WPF via a WPF PDF component.

Spire.PDF for WPF , a WPF library which can fully control your PDF documents, enables you to create PDF digital certificate in a simple way. First, please see the effect of this digital certificate task as below picture:

Create Digital Certificate

Before viewing the code, please Download Spire.PDF for WPF and install it in system.

In this solution, first you need to load your .pfx file from system. Then, create a new instance of Spire.Pdf.Security.PdfCertificate class with two parameters passed, one is the certificate file and the other is the password of the imported .pfx file. After creating a PdfSignature class instance, you can add DocumentPermissions to be AllowComments, AllowFormFill and ForbidChanges.

When you create the PdfSignature class instance, there are four parameters. The parameter "page" enables you to choose which PDF page you decide to sign the digital certificate.

[C#]
String pfxPath = @"..\Data\Demo.pfx";
PdfCertificate cert = new PdfCertificate(pfxPath, "e-iceblue");
PdfSignature signature = new PdfSignature(doc, page, cert, "demo");
signature.ContactInfo = "Harry Hu";
signature.Certificated = true;
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill;
[VB.NET]
Dim pfxPath As String = "..\Data\Demo.pfx"
Dim cert As New PdfCertificate(pfxPath, "e-iceblue")
Dim signature As New PdfSignature(doc, page, cert, "demo")
signature.ContactInfo = "Harry Hu"
signature.Certificated = True
signature.DocumentPermissions = PdfCertificationFlags.AllowFormFill

Spire.PDF for WPF enables your WPF applications to read, write and manipulate PDF documents without using Adobe Acrobat or any third party component library, click to know more about this WPF PDF component.