How to Set Word Document Properties – C#/VB.NET

Word Properties are used to show brief information of one document to readers. It includes several aspects, general, summary, statistics and custom. Usually, people pay attention on general and summary setting. General presents document type, location and edited time information, while summary presents titles, author, keywords, comments and other document description information.

In this post, I will introduce a method to set Word properties with C#/VB.NET.

In my example, I prepare a Word document and set its properties, including title, keywords, author, category and comments information. And I use Spire.Doc to help me to realize this function. If you want to use the following code, please add Spire.Doc dll file as reference in your project.

Detailed Steps Shown as Following:           

  1. Load document which I want to set properties from computer.
  2. Set document properties. Assign value for document.BuiltinDocumentProperties.XX(XX is properties which you want to set, for example, title, author etc.).
  3. Save and launch file.

Main Coding:

C#

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

namespace WordProperties
{
    class Properties
    {
        static void Main(string[] args)
        {

            //Load Document
            Document document = new Document();
            document.LoadFromFile(@”D:\work\Documents\Blues Introduction.docx”);

            //Properties
            document.BuiltinDocumentProperties.Title = “Blues Introduction”;
            document.BuiltinDocumentProperties.Keywords = “Blues”;
            document.BuiltinDocumentProperties.Author = “Wiki”;
            document.BuiltinDocumentProperties.Category = “Music”;
            document.BuiltinDocumentProperties.Comments = “This introduction is from Wiki.”;

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

VB.NET:

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

Namespace WordProperties
    Friend Class Properties
        Shared Sub Main(ByVal args() As String)

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

            ‘Properties
            document.BuiltinDocumentProperties.Title = “Blues Introduction”
            document.BuiltinDocumentProperties.Keywords = “Blues”
            document.BuiltinDocumentProperties.Author = “Wiki”
            document.BuiltinDocumentProperties.Category = “Music”
            document.BuiltinDocumentProperties.Comments = “This introduction is from Wiki.”

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

        End Sub
    End Class
End Namespace

Result Shown as Following:

___________________________________________________________________________________________

Click Here to Learn more about Spire.Doc

Click Here to Download Spire.Doc

Spire.Office, a component to operate Word, Excel, PDF documents and export data, also can used to realize the above function.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s