How to add & read built-in and custom document properties in C# and VB.NET

Document properties, also known as metadata, are details about a file that describe or identify it. They include details such as title, author name, subject, and keywords that identify the document's topic or contents.
If you include the document properties for your files, you can easily organize and identify them later. You can also search for documents based on their properties or insert them into your documents.

The following example shows how to add built-in and custom document properties (metadata).

First of all, let's create a simple RTF document with inscription: "Hello World" and add some metadata inside:


               // Let's create a simple inscription.
            dc.Content.End.Insert("Hello World!!!", new CharacterFormat() { FontName = "Verdana", Size = 65.5f, FontColor = Color.Orange });
            
The next step is adding built-in document properties:

               // Let's add some documents properties: Author, Subject, Company.
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Title] = "How to add document properties. It works with DOCX, RTF, PDF, HTML etc";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Company] = "SautinSoft";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Author] = "John Smith";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Subject] = "Document.Net";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.HyperlinkBase] = "www.sautinsoft.ru";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Manager] = "Alex Dickard";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Category] = "Document Object Model (DOM)";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.DateContentCreated] =
                new DateTime(2010, 1, 10).ToString();
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.DateLastSaved] =
                new DateTime(2018, 6, 30).ToString();

And the last step is saving a document as RTF file

                                       // Save document as the RTF file
            dc.Save(@"d:\Download\DocumentProperties.rtf");

If you want to read document properties from a file:


                                        DocumentCore dc = DocumentCore.Load(@"d:\Download\DocumentProperties_.docx");

The next step is reading all information about a file and displaying it.


                                        // Let's add some additional inforamtion. It can be anything you like.
            dc.Document.Properties.Custom.Add("Producer", "My Producer");

            // Add a paragraph in which all standard information about the document will be stored
            Paragraph builtInPara = new Paragraph(dc,
            new Run(dc, "Built-in document properties:"),
            new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            builtInPara.ParagraphFormat.Alignment = HorizontalAlignment.Left;
             
            foreach (var docProp in dc.Document.Properties.BuiltIn)
            {
                builtInPara.Inlines.Add(
                    new Run(dc, string.Format("{0}: {1}", docProp.Key, docProp.Value)));

                builtInPara.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            }
            // Add a paragraph in which all additional information about the document will be stored
            Paragraph customPropPara = new Paragraph(dc,
               new Run(dc, "Custom document properties:"),
               new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            customPropPara.ParagraphFormat.Alignment = HorizontalAlignment.Left;

            foreach (var docProp in dc.Document.Properties.Custom)
            {
                customPropPara.Inlines.Add(
                    new Run(dc, string.Format("{0}: {1} (Type: {2})", docProp.Key, docProp.Value, docProp.Value.GetType())));

                customPropPara.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            }

You may find extra info about Document Properties.

Well done!

 

Complete code

using System;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;
using System.IO;
using System.Linq;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateDocumentProperties();
            ReadDocumentProperties();
        }
            public static void CreateDocumentProperties()
        {
            
            DocumentCore dc = new DocumentCore();
            
            // Let's create a simple inscription.
            dc.Content.End.Insert("Hello World!!!", new CharacterFormat() { FontName = "Verdana", Size = 65.5f, FontColor = Color.Orange });
            
            // Let's add some documents properties: Author, Subject, Company.
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Title] = "How to add document properties. It works with DOCX, RTF, PDF, HTML etc";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Company] = "SautinSoft";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Author] = "John Smith";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Subject] = "Document.Net";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.HyperlinkBase] = "www.sautinsoft.ru";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Manager] = "Alex Dickard";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.Category] = "Document Object Model (DOM)";
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.DateContentCreated] =
                new DateTime(2010, 1, 10).ToString();
            dc.Document.Properties.BuiltIn[BuiltInDocumentProperty.DateLastSaved] =
                new DateTime(2018, 6, 30).ToString();
            // Save document as the RTF file
            dc.Save(@"d:\Download\DocumentProperties.rtf");
        }
        
        public static void ReadDocumentProperties()
        {
           
            DocumentCore dc = DocumentCore.Load(@"d:\Download\DocumentProperties_.docx");
            // Let's add some additional inforamtion. It can be anything you like.
            dc.Document.Properties.Custom.Add("Producer", "My Producer");

            // Add a paragraph in which all standard information about the document will be stored
            Paragraph builtInPara = new Paragraph(dc,
            new Run(dc, "Built-in document properties:"),
            new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            builtInPara.ParagraphFormat.Alignment = HorizontalAlignment.Left;
             
            foreach (var docProp in dc.Document.Properties.BuiltIn)
            {
                builtInPara.Inlines.Add(
                    new Run(dc, string.Format("{0}: {1}", docProp.Key, docProp.Value)));

                builtInPara.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            }
            // Add a paragraph in which all additional information about the document will be stored
            Paragraph customPropPara = new Paragraph(dc,
               new Run(dc, "Custom document properties:"),
               new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            customPropPara.ParagraphFormat.Alignment = HorizontalAlignment.Left;

            foreach (var docProp in dc.Document.Properties.Custom)
            {
                customPropPara.Inlines.Add(
                    new Run(dc, string.Format("{0}: {1} (Type: {2})", docProp.Key, docProp.Value, docProp.Value.GetType())));

                customPropPara.Inlines.Add(new SpecialCharacter(dc, SpecialCharacterType.LineBreak));
            }
            // Add all document properties in the document and save it as PDF file
            dc.Sections.Clear();
            dc.Sections.Add(new Section(dc, builtInPara, customPropPara));

            dc.Save(@"d:\download\Properties.pdf");

        }
    }  
}
        
            Imports System
Imports SautinSoft.Document

Module ExampleVB

    Sub Main()
        CreateRtf()
    End Sub

    Public Sub CreateRtf()
        ' Set a path to our rtf file.
        Dim rtfPath As String = "d:\Result.rtf"

        ' Let's create a simple Rtf document.
        Dim rtf As New DocumentCore()
        'DocumentCore.Serial = "put your serial here";

        ' Add new section.
        Dim section As New Section(rtf)
        rtf.Sections.Add(section)

        ' Let's set page size A4.
        section.PageSetup.PaperType = PaperType.A4

        ' Add two paragraphs using different ways:

        ' Way 1: Add 1st paragraph.
        Dim par1 As New Paragraph(rtf)
        par1.ParagraphFormat.Alignment = HorizontalAlignment.Center
        section.Blocks.Add(par1)

        ' Let's create a characterformat for text in the 1st paragraph.
        Dim cf As New CharacterFormat() With {
        .FontName = "Verdana",
        .Size = 16,
        .FontColor = Color.Orange
    }

        Dim text1 As New Run(rtf, "This is a first line in 1st paragraph!")
        text1.CharacterFormat = cf
        par1.Inlines.Add(text1)

        ' Let's add a line break into our paragraph.
        par1.Inlines.Add(New SpecialCharacter(rtf, SpecialCharacterType.LineBreak))

        Dim text2 As Run = text1.Clone()
        text2.Text = "Let's type a second line."
        par1.Inlines.Add(text2)

        ' Way 2 (easy): Add 2nd paragraph using ContentRange.
        rtf.Content.End.Insert(ControlChars.Lf & "This is a first line in 2nd paragraph.", New CharacterFormat() With {
        .Size = 25,
        .FontColor = Color.Blue,
        .Bold = True
    })
        Dim lBr As New SpecialCharacter(rtf, SpecialCharacterType.LineBreak)
        rtf.Content.End.Insert(lBr.Content)
        rtf.Content.End.Insert("This is a second line.", New CharacterFormat() With {
        .Size = 20,
        .FontColor = Color.DarkGreen,
        .UnderlineStyle = UnderlineType.Single
    })

        ' Save Rtf to a file
        rtf.Save(rtfPath, New RtfSaveOptions())
    End Sub
End Module
© SautinSoft 2019