Convert Word to PDF Easily with C#/VB.NET

PDF Introduction

PDF (Portable Document Format), developed by Adobe, has been become one of the most frequently used electronic document format now.

Why is PDF so popular? Firstly, PDF has nothing to do with operation system. It means that it can work well on Windows, Mac and Unix. This function enables PDF to be the best choice to publish electronic document and deliver digital information online. Secondly, PDF can reproduce characters, colors and images in original data exactly because it is based on PostScript image model. Therefore, PDF document can give readers a good reading effect.

Convert Word to PDF

Then, how to create PDF document? The method people often use is to convert from other format document, for example, Word to PDF.

In this post, I want to introduce a simple method to convert Word to PDF with C# and VB.NET quickly.

In this method,  a component, Spire.Doc for .NET is used. If you want to use the following code, please download and install this component on your computer firstly and then add Spire.Doc dll file as reference.

Detailed Steps Shown as Following:          

Step 1:

Load file which you want to convert to PDF from your computer by using document.LoadFromFile() method.

C#

            Document document = new Document();

            document.LoadFromFile(@”E:\work\Antarctic.docx”);

VB.NET

            Dim document As New Document()

            document.LoadFromFile(“E:\work\Antarctic.docx”)

Step 2:

Convert this document to PDF by using document.SaveToFile() method. Two parameters passed to this method, file name and file format.

C#

            document.SaveToFile(“ToPDF.PDF”, FileFormat.PDF);

VB.NET

            document.SaveToFile(“ToPDF.PDF”, FileFormat.PDF)

Step 3:

Launch new PDF document.

C#

            System.Diagnostics.Process.Start(“ToPDF.PDF”);

VB.NET

            System.Diagnostics.Process.Start(“ToPDF.PDF”)

Result Shown as Following:

Freely Download Spire.Doc for .NET

Advertisements

Easy to Decrypt Word Document with C#, VB.NET

Word Decryption is relative to Word encryption, which is one method to protect important documents. As is known, people need to have the correct password to open one encrypted Word document. This process is also called decryption.

In this post, I want to introduce a method about how to decrypt word with C#/VB.NET.

In my example, I have a document which has been encrypted with password. The password is 123456. What I will do is to decrypt this document.

Note: I use the .NET Word component, Spire.Doc for .NET, which specializes in operating Word documents, to meet my requirement more easily and quickly. Therefore, if you want to use the following code, please add Spire.Doc for .NET dll file as reference.

Detailed Steps Shown as Following:           

  1. Load the encrypted file from computer by using document.LoadFromFile() method. Generally speaking, I just need to pass one parameter which is filename to this method. However, now, I need to send three parameters to this method for decrypting this document directly. The three parameters are file name, file format and password string.
  2. Save the decrypted document with another name and launch this new document.

Main Coding:

C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace DecryptWord
{
    class Decryption
    {
        static void Main(string[] args)
        {

            //Load and Decrypt
            Document document = new Document();
            document.LoadFromFile(@”E:\work\Documents\welcome.docx”, FileFormat.Docx, “123456”);

            //Save and Launch
            document.SaveToFile(“decryption.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“decryption.docx”);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace DecryptWord
    Friend Class Decryption
        Shared Sub Main(ByVal args() As String)

            ‘Load and Decrypt
            Dim document As New Document()
            document.LoadFromFile(“E:\work\Documents\welcome.docx”, FileFormat.Docx, “123456”)

            ‘Save and Launch
            document.SaveToFile(“decryption.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“decryption.docx”)

        End Sub
    End Class
End Namespace

After running, Document Welcome.docx will be shown without entering password. 

Download Spire.Doc for .NET Here

Related Post: How to Encrypt Word Document – C#/VB.NET 

How to Use Mail Merge Function in Word Document with C#/VB.NET

Mail Merge Introduction

Word Mail Merge is used to merge data to a template file and then form different items which can be sent via E-mail or print in batch. It is based on two elements, template and data information.

Template: Include description words and fields. Description words are used to hint what kind of information should be given. Field is placeholder. It means that there has been occupied. When starting with mail merge, data will be put in corresponding location which fields occupy.

Actually, MS Word provides users with several templates. We can use the default template or create a new one according to requirement.

Data information: Get from Excel or database. Also, new data list can be created when merging.

Use Mail Merge

In this post, I want to introduce a method to use Word mail merge with C#/VB.NET. At the beginning, I create a template file. It is a message, including receiver address and name, message body, sender address and name, date.  

Template shown as following:

Because I just want to create one item, so I create new data information when coding. Also, the C# VB.NET Word component, Spire.Doc for .NET is used for realizing this function more quickly and easily.

Detailed Steps Shown as Following:           

  1. Load template file from computer.
  2. Firstly, assign value for fieldNames string array, which should be the same as field name in template. Secondly, assign value for fieldValues string array. The values are data information I create. Thirdly, merge data in template by using document.MailMerge.Execute() method. There are two parameter passed, fieldNames and fieldValues.
  3. Save and launch.

Main Coding:

C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace MailMerge
{
    class Mail_Merge
    {
        static void Main(string[] args)
        {

            //Load File
            Document document = new Document();
            document.LoadFromFile(@”E:\work\Documents\Message.docx”);

            //Mail Merge
            string[] filedNames = new string[] { “receiver_Name”, “receiver_address”,“Sender_Name”,“Sender_Address”, “Date” };
            string[] filedValues = new string[] { “Branka”, “No. 40, Grand Street, Lisa Road, Wells, England”, “Lartias”,“No. 901, Tee Street, Wall Road, Wells, England”,System.DateTime.Now.Date.ToString() };
            document.MailMerge.Execute(filedNames, filedValues);

            //Save and Launch
            document.SaveToFile(“MailMerge.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“MailMerge.docx”);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace MailMerge
    Friend Class Mail_Merge
        Shared Sub Main(ByVal args() As String)

            ‘Load File
            Dim document As New Document()
            document.LoadFromFile(“E:\work\Documents\Message.docx”)

            ‘Mail Merge
            Dim filedNames() As String = {“receiver_Name”, “receiver_address”, “Sender_Name”, “Sender_Address”, “Date”}
            Dim filedValues() As String = {“Branka”, “No. 40, Grand Street, Lisa Road, Wells, England”, “Lartias”, “No. 901, Tee Street, Wall Road, Wells, England”, Date.Now.Date.ToString()}
            document.MailMerge.Execute(filedNames, filedValues)

            ‘Save and Launch
            document.SaveToFile(“MailMerge.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“MailMerge.docx”)

        End Sub
    End Class
End Namespace

Result Shown as Following:

Download Spire.Doc for .NET Here

Insert Bookmark to Fix Location Quickly in Word with C#/VB.NET

When talking about bookmark, we think of e-bookmark which saves website to be convenient for visiting next time. But, Word Bookmark is quietly different with e-bookmark. It is used to fix location to lead readers to get wanted contents quickly. In this post, I want to introduce a method to insert bookmark in Word with C#/VB.NET.

In a Word document, we can add bookmark for paragraph, text and image. For example, users can set bookmark to start with paragraph beginning and end with paragraph ending for get location of one special paragraph. Also, bookmark is very useful when there are several fields in your document. You can set bookmark for these fields to fill them quickly. 

In this example, I will insert a bookmark in my document. This bookmark starts from the end of paragraph 2 and end with the end of paragraph 3.

This method is based on a .NET Word component, Spire.Doc for .NET.  So if you want to use the following code, please download and install it on your system. Then, add its dll file as reference in your project.

Detailed Steps Shown as Following:           

  1. Load document which I want to insert bookmark.
  2. Get sections in this document and fix location you want to start with bookmark. That is the second paragraph of section.
  3. Use paragraph.BookmarkStart() method to define bookmark beginning location. There is a parameter passed to this method, string bookmark name.
  4. Fix location you want to end with bookmark. That is the third paragraph of section.
  5. Use paragraph.BookmarkEnd() method to define bookmark ending location. Also, string bookmark name should be passed to this method.
  6. Save and launch file.

Please note that: if you want to add bookmark for paragraph in existed document, the bookmark will be fixed at the end of paragraphs by default.

Main Coding:

C#

using System;
using Spire.Doc;
using Spire.Doc.Documents;

namespace WordBookmark
{
    class Bookmark
    {
        static void Main(string[] args)
        {
            //Create word document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\Documents\Blues Introduction.docx”);

            Section section = document.Sections[0];
            Paragraph paragraph = section.Paragraphs[1];
            paragraph.AppendBookmarkStart(“blues”);
            Paragraph paragraph2 = section.Paragraphs[2];
            paragraph2.AppendBookmarkEnd(“blues”);

            //Save and Launch
            document.SaveToFile(“Bookmark.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Bookmark.docx”);
        }
    }
}

VB.NET:

Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace WordBookmark
    Friend Class Bookmark
        Shared Sub Main(ByVal args() As String)

            ‘Create word document
            Dim document As New Document()
            document.LoadFromFile(“D:\work\Documents\Blues Introduction.docx”)

            Dim section As Section = document.Sections(0)
            Dim paragraph As Paragraph = section.Paragraphs(1)
            paragraph.AppendBookmarkStart(“blues”)
            Dim paragraph2 As Paragraph = section.Paragraphs(2)
            paragraph2.AppendBookmarkEnd(“blues”)

            ‘Save and Launch
            document.SaveToFile(“Bookmark.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Bookmark.docx”)

        End Sub
    End Class
End Namespace

Result Shown as Following:

 Download Spire.Doc for .NET Here

How to Insert Text Watermark in Word with C#/VB.NET

Word Watermark is text or image with translucence mode. It can be put with different direction, for example, diagonal or horizontal. Although we insert watermark in Word, the contents will not be influenced at all. Its main function is to protect author’s copyright.

There are two kinds of watermark, text watermark and image watermark. Text watermark often presents document properties. For example, text which is taken as watermark may be company name, document title etc. Image watermark will be picture which is related to document contents. Also, we can use any pictures to take as watermark. What’s more, some beautiful image watermark can make appearance to be more appealed.

In this post, I will introduce a method to insert watermark in Word with C#/VB.NET.

A .NET Word Component, Spire.Doc for .NET is used to help me realize this function more quickly.

Detailed Steps Shown as Following:           

  1. Load document which I want to insert watermark from computer.
  2. Insert text watermark.  Firstly, declare a new txtWatermark and assign value for txtWatermark.Text. Secondly, format watermark, including color, font size and layout. There are two kinds of layout, diagonal and horizontal. Thirdly, insert watermark in document.
  3. Save and launch file.

Main Coding:

C#

using System;
using System.Drawing;
using Spire.Doc;
using Spire.Doc.Documents;

namespace InsertWatermark
{
    class watermark
    {
        static void Main(string[] args)
        {

            //Load File
            Document document = new Document();
            document.LoadFromFile(@”D:\work\documents\Antarctic.docx”);

            //Insert TextWatermark
            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = “Wiki Article”;
            txtWatermark.Color = Color.DeepSkyBlue;
            txtWatermark.FontSize = 45;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            document.Watermark = txtWatermark;

            //Save and Launch
            document.SaveToFile(“Watermark.docx”, FileFormat.Docx);
            System.Diagnostics.Process.Start(“Watermark.docx”);
        }
    }
}

VB.NET:

Imports System
Imports System.Drawing
Imports Spire.Doc
Imports Spire.Doc.Documents

Namespace InsertWatermark
    Friend Class watermark
        Shared Sub Main(ByVal args() As String)

            ‘Load File
            Dim document As New Document()
            document.LoadFromFile(“D:\work\documents\Antarctic.docx”)

            ‘Insert TextWatermark
            Dim txtWatermark As New TextWatermark()
            txtWatermark.Text = “Wiki Article”
            txtWatermark.Color = Color.DeepSkyBlue
            txtWatermark.FontSize = 45
            txtWatermark.Layout = WatermarkLayout.Diagonal
            document.Watermark = txtWatermark

            ‘Save and Launch
            document.SaveToFile(“Watermark.docx”, FileFormat.Docx)
            System.Diagnostics.Process.Start(“Watermark.docx”)

        End Sub
    End Class
End Namespace

For image watermark, we can use the following sentence:

            PictureWatermark imagewatermark = new PictureWatermark();
            Image image = Image.FromFile(@”D:\work\Documents\coffee.jpg”);
            imagewatermark.Picture = image;
            document.Watermark = imagewatermark;

Result Shown as Following:

Download Spire.Doc for .NET Here