.NetExcel

Jul 22, 2011 at 08:03 o\clock

How to Create Digital Certificate with C#

by: youthday   Keywords: PDF, Digital, Certificate

Digital Certificate

Digital Certificate, a digital information file, is used to define and verify identity of two parties on internet. It is similar to ID card in our daily life. A simple digital certificate includes public key, title and digital signature of Certificate Authority Center.

Because of insecurity of online transaction, digital certificate is developed to make sure the confidentiality and integrity of information online. Although the information you publish online is gotten by others, even your personal information has lost, your account is still safe. Also, people can use digital certificate to prove self’s identity and distinguish others’ identity.

How to Create PDF Digital Certificate

PDF has become one of the most frequently used formats to deliver digital information and digital certificate is to be used in PDF documents gradually.

Then, the method I want to introduce is to use C# to create PDF digital certificate. The method is based on a component: Spire.PDF.

Using Code:

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using Spire.Pdf.Security;

 

namespace DigitalSignature

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a pdf document.

            PdfNewDocument doc = new PdfNewDocument();

 

            // Create one page

            PdfPageBase page = doc.Pages.Add();

 

            //Draw the page

            DrawPage(page);

 

            String pfxPath = @"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;

 

            //Save pdf file.

            doc.Save("DigitalSignature.pdf");

            doc.Close();

 

            //Launching the Pdf file.

            System.Diagnostics.Process.Start("DigitalSignature.pdf");

        }

 

        private static void DrawPage(PdfPageBase page)

        {

            float pageWidth = page.Canvas.ClientSize.Width;

            float y = 0;

 

            //page header

            PdfPen pen1 = new PdfPen(Color.LightGray, 1f);

            PdfBrush brush1 = new PdfSolidBrush(Color.LightGray);

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f, FontStyle.Italic));

            PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Right);

            String text = "Demo of Spire.Pdf";

            page.Canvas.DrawString(text, font1, brush1, pageWidth, y, format1);

            SizeF size = font1.MeasureString(text, format1);

            y = y + size.Height + 1;

            page.Canvas.DrawLine(pen1, 0, y, pageWidth, y);

 

            //title

            y = y + 5;

            PdfBrush brush2 = new PdfSolidBrush(Color.Black);

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 16f, FontStyle.Bold));

            PdfStringFormat format2 = new PdfStringFormat(PdfTextAlignment.Center);

            format2.CharacterSpacing = 1f;

            text = "Summary of Science";

            page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2);

            size = font2.MeasureString(text, format2);

            y = y + size.Height + 6;

 

            //icon

            PdfImage image = PdfImage.FromFile(@"Wikipedia_Science.png");

            page.Canvas.DrawImage(image, new PointF(pageWidth - image.PhysicalDimension.Width, y));

            float imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2;

            float imageBottom = image.PhysicalDimension.Height + y;

 

            //refenrence content

            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 9f));

            PdfStringFormat format3 = new PdfStringFormat();

            format3.ParagraphIndent = font3.Size * 2;

            format3.MeasureTrailingSpaces = true;

            format3.LineSpacing = font3.Size * 1.5f;

            String text1 = "(All text and picture from ";

            String text2 = "Wikipedia";

            String text3 = ", the free encyclopedia)";

            page.Canvas.DrawString(text1, font3, brush2, 0, y, format3);

 

            size = font3.MeasureString(text1, format3);

            float x1 = size.Width;

            format3.ParagraphIndent = 0;

            PdfTrueTypeFont font4 = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Underline));

            PdfBrush brush3 = PdfBrushes.Blue;

            page.Canvas.DrawString(text2, font4, brush3, x1, y, format3);

            size = font4.MeasureString(text2, format3);

            x1 = x1 + size.Width;

 

            page.Canvas.DrawString(text3, font3, brush2, x1, y, format3);

            y = y + size.Height;

 

            //content

            PdfStringFormat format4 = new PdfStringFormat();

            text = System.IO.File.ReadAllText(@"Summary_of_Science.txt");

            PdfTrueTypeFont font5 = new PdfTrueTypeFont(new Font("Arial", 10f));

            format4.LineSpacing = font5.Size * 1.5f;

            PdfStringLayouter textLayouter = new PdfStringLayouter();

            float imageLeftBlockHeight = imageBottom - y;

            PdfStringLayoutResult result

                = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));

            if (result.ActualSize.Height < imageBottom - y)

            {

                imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight;

                result = textLayouter.Layout(text, font5, format4, new SizeF(imageLeftSpace, imageLeftBlockHeight));

            }

            foreach (LineInfo line in result.Lines)

            {

                page.Canvas.DrawString(line.Text, font5, brush2, 0, y, format4);

                y = y + result.LineHeight;

            }

            PdfTextWidget textWidget = new PdfTextWidget(result.Remainder, font5, brush2);

            PdfTextLayout textLayout = new PdfTextLayout();

            textLayout.Break = PdfLayoutBreakType.FitPage;

            textLayout.Layout = PdfLayoutType.Paginate;

            RectangleF bounds = new RectangleF(new PointF(0, y), page.Canvas.ClientSize);

            textWidget.StringFormat = format4;

            textWidget.Draw(page, bounds, textLayout);

        }

    }

}

 

Jun 28, 2011 at 07:26 o\clock

How to Paginate PDF with C#

by: youthday   Keywords: Paginate, PDF

As is known, one book includes cover, back cover, category and contents. Therefore, if we want to create an electronic book, the above elements are needed to be contained. And firstly we should learn how to paginate.

Generally speaking, the electronic books are PDF books. So, in this article, I want to talk how to create PDF pagination.

It is common to use Adobe Acrobat to edit the PDF book. Open it and count pages. Then click advanced. Select document Operation and then choose Paginate Page. After completing settings, click OK. Then, the page number is generated.

Also, we need to add header or footer in one page to show its page number. Click header and footer on File menu. Then select add and insert page.

Besides using Adobe Acrobat, the other method I want to show is to use C# to paginate PDF. This method is based on a component named Spire.PDF.

using System;

using System.Drawing;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Text;

namespace Pagination

{

    class Program

    {

        static void Main(string[] args)

        {

            //Create a pdf document.

            PdfDocument doc = new PdfDocument();

            //margin

            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;

            DrawCover(doc.Sections.Add(), margin);

            DrawContent(doc.Sections.Add(), margin);

            DrawPageNumber(doc.Sections[1], margin, 1, doc.Sections[1].Pages.Count);

            //Save pdf file.

            doc.SaveToFile("Pagination.pdf");

            doc.Close();

            //Launching the Pdf file.

            System.Diagnostics.Process.Start("Pagination.pdf");

        }

        private static void DrawPageNumber(PdfSection section, PdfMargins margin, int startNumber, int pageCount)

        {

            foreach (PdfPageBase page in section.Pages)

            {

                page.Canvas.SetTransparency(0.5f);

                PdfBrush brush = PdfBrushes.Black;

                PdfPen pen = new PdfPen(brush, 0.75f);

                PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);

                PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

                format.MeasureTrailingSpaces = true;

                float space = font.Height * 0.75f;

                float x = margin.Left;

                float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;

                float y = page.Canvas.ClientSize.Height - margin.Bottom + space;

                page.Canvas.DrawLine(pen, x, y, x + width, y);

                y = y + 1;

                String numberLabel

                    = String.Format("{0} of {1}", startNumber++, pageCount);

                page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format);

                page.Canvas.SetTransparency(1);

            }

        }

        private static void DrawContent(PdfSection section, PdfMargins margin)

        {

            section.PageNumber = new PdfPageNumber();

            section.PageNumber.NumberStyle = PdfNumberStyle.Numeric;

            section.PageNumber.Prefix = "page ";

            section.PageSettings.Size = PdfPageSize.A4;

            section.PageSettings.Margins.All = 0;

            PdfPageBase page = section.Pages.Add();

            DrawPageHeaderAndFooter(page, margin, false);

            float x = margin.Left;

            float y = margin.Top + 8;

            float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 16f), true);

            PdfBrush brush1 = PdfBrushes.Black;

            PdfPen pen1 = new PdfPen(brush1, 0.75f);

            page.Canvas.DrawString(Pagination.Resources.Title, font1, brush1, x, y);

            y = y + font1.MeasureString(Pagination.Resources.Title).Height + 6;

            page.Canvas.DrawLine(pen1, x, y, page.Canvas.ClientSize.Width - margin.Right, y);

            y = y + 1.75f;

            String content = Pagination.Resources.Content;

            String[] lines

                = content.Split(new char[] { 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries);

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 10f, FontStyle.Italic), true);

            PdfStringFormat format1 = new PdfStringFormat();

            format1.MeasureTrailingSpaces = true;

            format1.LineSpacing = font2.Height * 1.5f;

            format1.ParagraphIndent = font2.MeasureString("t", format1).Width;

            y = y + font2.Height * 0.5f;

            SizeF size = font2.MeasureString(lines[0], width, format1);

            page.Canvas.DrawString(lines[0], font2, brush1,

                new RectangleF(new PointF(x, y), size), format1);

            y = y + size.Height;

            PdfTrueTypeFont font3 = new PdfTrueTypeFont(new Font("Arial", 10f), true);

            PdfStringFormat format2 = new PdfStringFormat();

            format2.LineSpacing = font3.Height * 1.4f;

            format2.MeasureTrailingSpaces = true;

            size = font3.MeasureString(lines[1], width, format2);

            page.Canvas.DrawString(lines[1], font3, brush1,

                new RectangleF(new PointF(x, y), size), format2);

            y = y + size.Height;

            y = y + font3.Height * 0.75f;

            float indent = font3.MeasureString("tt", format2).Width;

            float x1 = x + indent;

            size = font3.MeasureString(lines[2], width - indent, format2);

            page.Canvas.DrawString(lines[2], font3, brush1,

                new RectangleF(new PointF(x1, y), size), format2);

            y = y + size.Height + font3.Height * 0.75f;

            StringBuilder buff = new StringBuilder();

            for (int i = 3; i < lines.Length; i++)

            {

                buff.AppendLine(lines[i]);

            }

            content = buff.ToString();

            PdfStringLayouter textLayouter = new PdfStringLayouter();

            PdfStringLayoutResult result

                = textLayouter.Layout(content, font3, format2, new SizeF(width, float.MaxValue));

            foreach (LineInfo line in result.Lines)

            {

                if ((line.LineType & LineType.FirstParagraphLine) == LineType.FirstParagraphLine)

                {

                    y = y + font3.Height * 0.75f;

                }

                if (y > (page.Canvas.ClientSize.Height - margin.Bottom - result.LineHeight))

                {

                    page = section.Pages.Add();

                    DrawPageHeaderAndFooter(page, margin, false);

                    y = margin.Top;

                }

                page.Canvas.DrawString(line.Text, font3, brush1, x, y, format2);

                y = y + result.LineHeight;

            }

        }

        private static void DrawCover(PdfSection section, PdfMargins margin)

        {

            section.PageNumber = new PdfPageNumber();

            section.PageNumber.NumberStyle = PdfNumberStyle.LowerRoman;

            section.PageNumber.Prefix = "cover ";

            section.PageSettings.Size = PdfPageSize.A4;

            section.PageSettings.Margins.All = 0;

            PdfPageBase page = section.Pages.Add();

            DrawPageHeaderAndFooter(page, margin, true);

            //refenrence content

            PdfBrush brush1 = PdfBrushes.LightGray;

            PdfBrush brush2 = PdfBrushes.Blue;

            PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial", 8f));

            PdfStringFormat format = new PdfStringFormat();

            format.MeasureTrailingSpaces = true;

            String text1 = "(All text and picture from ";

            String text2 = "Wikipedia";

            String text3 = ", the free encyclopedia)";

            float x = 0, y = 10;

            x = x + margin.Left;

            y = y + margin.Top;

            page.Canvas.DrawString(text1, font1, brush1, x, y, format);

            x = x + font1.MeasureString(text1, format).Width;

            page.Canvas.DrawString(text2, font1, brush2, x, y, format);

            x = x + font1.MeasureString(text2, format).Width;

            page.Canvas.DrawString(text3, font1, brush1, x, y, format);

            //cover

            PdfBrush brush3 = PdfBrushes.Black;

            PdfBrush brush4 = new PdfSolidBrush(new PdfRGBColor(0xf9, 0xf9, 0xf9));

            PdfImage image

                = PdfImage.FromFile(@"SciencePersonificationBoston.jpg");

            String text = Pagination.Resources.ImageDescription;

            float r = image.PhysicalDimension.Height / image.Height;

            PdfPen pen = new PdfPen(brush1, r);

            SizeF size = font1.MeasureString(text, image.PhysicalDimension.Width - 2);

            PdfTemplate template

                = new PdfTemplate(image.PhysicalDimension.Width + 4 * r + 4,

                    image.PhysicalDimension.Height + 4 * r + 7 + size.Height);

            template.Graphics.DrawRectangle(pen, brush4, 0, 0, template.Width, template.Height);

            x = y = r + 2;

            template.Graphics.DrawRectangle(brush1, x, y,

                image.PhysicalDimension.Width + 2 * r,

                image.PhysicalDimension.Height + 2 * r);

            x = y = x + r;

            template.Graphics.DrawImage(image, x, y);

            x = x - 1;

            y = y + image.PhysicalDimension.Height + r + 2;

            template.Graphics.DrawString(text, font1, brush3,

                new RectangleF(new PointF(x, y), size));

            float x1 = (page.Canvas.ClientSize.Width - template.Width) / 2;

            float y1 = (page.Canvas.ClientSize.Height - margin.Top - margin.Bottom) * (1 - 0.618f) - template.Height / 2 + margin.Top;

            template.Draw(page.Canvas, x1, y1);

            //title

            format.Alignment = PdfTextAlignment.Center;

            PdfTrueTypeFont font2 = new PdfTrueTypeFont(new Font("Arial", 24f, FontStyle.Bold));

            x = page.Canvas.ClientSize.Width / 2;

            y = y1 + template.Height + 10;

            page.Canvas.DrawString(Pagination.Resources.Title, font2, brush3, x, y, format);

        }

        private static void DrawPageHeaderAndFooter(PdfPageBase page, PdfMargins margin, bool isCover)

        {

            page.Canvas.SetTransparency(0.5f);

            PdfImage headerImage

                = PdfImage.FromFile(@"Header.png");

            PdfImage footerImage

                = PdfImage.FromFile(@"Footer.png");

            page.Canvas.DrawImage(headerImage, new PointF(0, 0));

            page.Canvas.DrawImage(footerImage, new PointF(0, page.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height));

            if (isCover)

            {

                page.Canvas.SetTransparency(1);

                return;

            }

            PdfBrush brush = PdfBrushes.Black;

            PdfPen pen = new PdfPen(brush, 0.75f);

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 9f, FontStyle.Italic), true);

            PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Right);

            format.MeasureTrailingSpaces = true;

            float space = font.Height * 0.75f;

            float x = margin.Left;

            float width = page.Canvas.ClientSize.Width - margin.Left - margin.Right;

            float y = margin.Top - space;

            page.Canvas.DrawLine(pen, x, y, x + width, y);

            y = y - 1 - font.Height;

            page.Canvas.DrawString("Demo of Spire.Pdf", font, brush, x + width, y, format);

            page.Canvas.SetTransparency(1);

        }      

Jun 15, 2011 at 07:17 o\clock

How to Protect Excel by Using MS and C#

by: youthday   Keywords: Protect, Excel

Generally speaking, people may protect one important document or file by giving a password for it, especially some important or secret files. For office workers, they often record important data in Excel. Therefore, they need to know clearly about how to protect Excel file.

There are several methods to protect Excel file. We can lock the whole worksheet to prevent others from opening or editing it. Also, we can just protect specified Excel range or cells.

Firstly, I want to talk something about using MS to protect Excel.

Method 1:

Select all cells or range which you want to lock, and then click Cells on Format menu. Select Protect tab. Check Lock checkbox.

Method 2:

Click Protect on Tool menu. Choose Protect Worksheet. Then type the password twice. Click OK.

Method 3:

Click Protect on Tool menu. Then choose Allow users to editing range. Click Create New on the pops-up window. Select range you want to lock. You can set several passwords for different ranges.

After learning how to protect Excel by using MS, I want to share another method through programming. Actually, programmers often need to use C# to protect Excel worksheet. But if they don’t install MS on computer, how can they do to complete the operation? Now, the method I will introduce is to solve this problem.

Note: A component, Spire.XLS is needed to be installed before using the following code:

Private Sub btnRun_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnRun.Click

{    

      workbook As Workbook = New Workbook()

      //Initailize worksheet

      workbook.CreateEmptySheets(1)

      sheet As Worksheet = workbook.Worksheets(0)

      //Protect workshet

      sheet.Protect(Me.textBox1.Text)

      //Protect cells

      sheet.Range("A1").Text = "Locked"

      sheet.Range("B1").Text = "Unlocked"

      sheet.Range("A1").Style.Locked = True

      sheet.Range("B1").Style.Locked = False

      //Protect workbook

      workbook.Protect(Me.textBox1.Text)

      workbook.SaveToFile("Sample.xls")

      ExcelDocViewer(workbook.FileName)

}

Private Sub ExcelDocViewer(ByVal fileName As String)

{

       try

       {

             System.Diagnostics.Process.Start(fileName)

       }

       Catch{}

}

Jun 2, 2011 at 08:05 o\clock

Something about Word Header and How to Set Header and Footer with VB.NET

by: youthday   Keywords: VB, NET, Word, Header

Sometimes, we may add a header for a Word document. The header may be page number, author or file name. Also, people can type text which they want to set as header. I would like to use page number as header because I think that it is convenient for viewing one page and the layout is wonderful after printing.

Actually, people are familiar with how to add a Word header. Click header and footer on View menu. Then the setting toolbar appears. Type the text or use the format MS Word provides. After completing the setting, click Close. Then, the header is added in document.

Now, I want to share something about how to add a Word header and footer with VB.NET.

Code:

Public Class ThisDocument
Private MyDocMenu As Office.CommandBarButton
Private missing = System.Reflection.Missing.Value
'Add a New Menu in Function Zone
Private Sub ThisDocument_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
Dim MyCommandBarPopup As Office.CommandBarPopup = Nothing
Dim MyCommandBarMenu As Office.CommandBar = CType( _
Application.CommandBars.ActiveMenuBar, Office.CommandBar)
Dim MyControlsCount As Integer = MyCommandBarMenu.Controls.Count
MyCommandBarPopup = CType(MyCommandBarMenu.Controls.Add( _
Office.MsoControlType.msoControlPopup, missing, missing, _
MyControlsCount, True), Office.CommandBarPopup)
If (MyCommandBarPopup IsNot Nothing) Then
MyCommandBarPopup.Caption = "File Management"
MyDocMenu = CType(MyCommandBarPopup.Controls.Add( _
Office.MsoControlType.msoControlButton, missing, _
missing, missing, True), Office.CommandBarButton)
MyDocMenu.Caption = "Set Header and Footer"
AddHandler MyDocMenu.Click, AddressOf MyDocMenuCommand_Click
End If
End Sub
Private Sub ThisDocument_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown
End Sub
'Set Header and Footer
Private Sub MyDocMenuCommand_Click(ByVal Ctrl As Microsoft.Office.Core.CommandBarButton, ByRef CancelDefault As Boolean)
Dim MyHeader As String = "My Document"
Dim MyFooter As String = "My Document"
For Each MySection As Word.Section In Me.Sections
MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Font.ColorIndex = Word.WdColorIndex.wdDarkBlue
MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Font.Size = 16
MySection.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Text = MyHeader
MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Font.ColorIndex = Word.WdColorIndex.wdBrightGreen
MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Font.Size = 12
MySection.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary). _
Range.Text = MyFooter
Next
End Sub
End Class

May 20, 2011 at 06:05 o\clock

Insert Hyperlink in Word by Using Microsoft and Programmatically

by: youthday   Keywords: Insert, Word, Hyperlink

In a Word document, people may insert hyperlink for providing more information which is related to the contents of this document.

Hyperlink is very useful because it can show the additional information we want to talk about but has nothing to do with the theme of contents. For readers, they can click the hyperlink to learn something more which may be useful for them.

How to insert hyperlink in a Word document?

Open a word document. Select the place where you want to insert hyperlink or the key words. Then, click Insert. Select Hyperlink. After that, the window for inserting hyperlink pops up. On the left, you can find sections about linking to. Generally speaking, people would like to choose the first one: from existed document or website. Choose the document you want to link or enter the website address. Click OK and then the hyperlink is inserted in document.

Therefore, it is very easy to insert hyperlink in Word. However, programmers need to spend much time on researching how to insert hyperlink with C#.

How to insert hyperlink programmatically?

There are several methods are shared online. And I choose this method because it can save me much time and I don’t need to install Microsoft Word. However, the other third party add-in is needed to be installed, Spire.Doc.

 

private void button1_Click(object sender, EventArgs e)

{

    //Create word document

    Document document = new Document();

    InsertHyberlink(document.AddSection());

    //Save doc file.

    document.SaveToFile("Sample.doc",FileFormat.Doc);

    //Launching the MS Word file.

    WordDocViewer("Sample.doc");

private void InsertHyberlink(Section section)

    Paragraph paragraph = section.AddParagraph();

    paragraph.AppendText("Spire.XLS for .NET rn e-iceblue company Ltd. 2002-2010 All rights reserverd");

}

{

    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    paragraph = section.AddParagraph();

    paragraph.AppendText("Home page");

    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    paragraph = section.AddParagraph();

    paragraph.AppendHyperlink("www.e-iceblue.com", "www.e-iceblue.com", HyperlinkType.WebLink);

    paragraph = section.AddParagraph();

    paragraph.AppendText("Contact US");

    paragraph.ApplyStyle(BuiltinStyle.Heading2);

    paragraph = section.AddParagraph();

    paragraph.AppendHyperlink("mailto:support@e-iceblue.com", "support@e-iceblue.com", HyperlinkType.EMailLink);

}

private void WordDocViewer(string fileName)

{

    try

    {

        System.Diagnostics.Process.Start(fileName);

    }

    catch { }

}