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

Funny Song – Pray For You

Do you know Pray for You? LOL. Every time I think of this song, I will laugh. It is called curse song, the most vicious song. Actually, the lyrics are so funny and completely different from song name "Pray for You". No, it matches with song name very well even though there are unfavorable events which are prayed for former lover.

It is co-written and recorded by American singer Jaron Lowenstein and the first solo single release of Lowenstein. This song gets many positive reviews. Matt Bjorke of Roughstock thought lyrics "clever and unique enough". Jim Malec said that it demonstrates a delightful disdain for mainstream protocol.

Now, let's enjoy this song.

Lyrics:

I haven't been to church since I don't remember when 

Things were going great til they fell apart again

So I listened to the preacher as he told me what to do

He said you can't go hatin' others who have done wrong to you

Sometimes we get angry but we must not condemn

Let the good Lord do his job, you just pray for them

I pray your brakes go out runnin' down a hill

I pray a flower pot falls from a window sill

And knocks you in the head like I'd like to

I pray your birthday comes and nobody calls

I pray you're flyin' high when your engine stops

I pray all your dreams never come true

Just know wherever you are, honey, I pray for you

I'm really glad I found my way to church

Cause I'm already feelin' better and I thank God for the words

Yeah, I'm gonna take the high road and do what the preacher told me to do

You keep messin' up, and I'll keep prayin' for you

I pray you tire blows out at 110

I pray you pass out drunk with your best friend

And wake up with his and her tattoos

I pray your brakes go out runnin' down a hill

I pray a flower pot falls from a window sill

And knocks you in the head like I'd like to

I pray your birthday comes and nobody calls

I pray you're flyin' high when your engine stops

I pray all your dreams never come true

Just know wherever you are, near or far

In your house or in your car

Wherever you are, honey, I pray for you

I pray for you

Video:

http://www.youtube.com/watch?v=atBg9zLI2bA

Reference: Wiki: Pray for You

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