Spire.Doc

When you type in a document, Word automatically counts the number of pages and words in your document and displays them on the status bar – Word Count, at the bottom of the workspace. But how can we get the number of words, characters in an existing Word document through programming? This article aims to give you a simple solution offered by Spire.Doc.

Test file:

Count the number of words in a document in C#, VB.NET

Detailed Steps for Getting the Number of Words and Characters

Step 1: Create a new instance of Spire.Doc.Document class and load the test file.

Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx2010);

Step 2: Display the number of words, characters including or excluding spaces on console.

Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount);
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace);
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount);

Output:

Count the number of words in a document in C#, VB.NET

Full Code:

[C#]
Document doc = new Document();
doc.LoadFromFile("test.docx", FileFormat.Docx2010);
Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount);
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace);
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount);
Console.ReadKey();
[VB.NET]
Dim doc As New Document()
doc.LoadFromFile("test.docx", FileFormat.Docx2010)
Console.WriteLine("CharCount: " + doc.BuiltinDocumentProperties.CharCount)
Console.WriteLine("CharCountWithSpace: " + doc.BuiltinDocumentProperties.CharCountWithSpace)
Console.WriteLine("WordCount: " + doc.BuiltinDocumentProperties.WordCount)
Console.ReadKey()

Content controls provide a way for you to design documents. When you add a content control to a document, the control is identified by a border, a title, and temporary text that can provide instructions to the user. According to Microsoft, content controls mainly benefit from two features:

  • Prevent users from editing or deleting protected sections of a document.
  • Bind parts of a document or template to data. You can bind content controls to database fields, managed objects in the .NET Framework, XML elements that are stored in the document, and other data sources.

Therefore, it is necessary for developers to get the properties of content controls when dealing content controls at run time. This article illustrates how to get all controls and their properties including alias, id and tag via Spire.Doc.

Firstly, check the test file that contains six content controls distributed in lines and a table. By default, the border and the title of the control do not appear if we don't click the protected section.

Test File:

Get alias, tag and id of content controls in a Word document in C#

Main Steps:

Step 1: Create a new Word document and load the test file.

Step 2: Create two lists to store tags which are distributed in lines and a table separately. Here, each content control will be identified by tag.

Step 3: Use foreach sentence to get all tags in the Word document.

Full Code:

       static void Main(string[] args)
        {
            using (Document document = new Document(@"..\..\TestData\test.docx"))
            {
                StructureTags structureTags = GetAllTags(document);
                List<StructureDocumentTagInline> tagInlines = structureTags.tagInlines;

                string alias = tagInlines[0].SDTProperties.Alias;
                decimal id = tagInlines[0].SDTProperties.Id;
                string tag = tagInlines[0].SDTProperties.Tag;

                List<StructureDocumentTag> tags = structureTags.tags;
                alias = tags[0].SDTProperties.Alias;
                id = tags[0].SDTProperties.Id;
                tag = tags[0].SDTProperties.Tag;

            }
        }
       static StructureTags GetAllTags(Document document)
        {
            StructureTags structureTags = new StructureTags();
            foreach (Section section in document.Sections)
            {
                foreach (DocumentObject obj in section.Body.ChildObjects)
                {
                    if (obj.DocumentObjectType == DocumentObjectType.Paragraph)
                    {
                        foreach (DocumentObject pobj in (obj as Paragraph).ChildObjects)
                        {
                            if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                            {
                                structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                            }
                        }
                    }
                    else if (obj.DocumentObjectType == DocumentObjectType.Table)
                    {
                        foreach (TableRow row in (obj as Table).Rows)
                        {
                            foreach (TableCell cell in row.Cells)
                            {
                                foreach (DocumentObject cellChild in cell.ChildObjects)
                                {
                                    if (cellChild.DocumentObjectType == DocumentObjectType.StructureDocumentTag)
                                    {
                                        structureTags.tags.Add(cellChild as StructureDocumentTag);
                                    }
                                    else if (cellChild.DocumentObjectType == DocumentObjectType.Paragraph)
                                    {
                                        foreach (DocumentObject pobj in (cellChild as Paragraph).ChildObjects)
                                        {
                                            if (pobj.DocumentObjectType == DocumentObjectType.StructureDocumentTagInline)
                                            {
                                                structureTags.tagInlines.Add(pobj as StructureDocumentTagInline);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return structureTags;
        }
        public class StructureTags
        {
            List<StructureDocumentTagInline> m_tagInlines;
            public List tagInlines
            {
                get
                {
                    if (m_tagInlines == null)
                        m_tagInlines = new List();
                    return m_tagInlines;
                }
                set
                {
                    m_tagInlines = value;
                }
            }
            List<StructureDocumentTag> m_tags;
            public List tags
            {
                get
                {
                    if (m_tags == null)
                        m_tags = new List();
                    return m_tags;
                }
                set
                {
                    m_tags = value;
                }
            }
        }

Effect Screenshot:

Content controls in lines

Get alias, tag and id of content controls in a Word document in C#

Content controls in table

Get alias, tag and id of content controls in a Word document in C#

Wednesday, 24 September 2014 06:15

Replace Images in Word with Texts in C#

Written by support iceblue

This topic is from Stackoverflow (original post), let us see how Spire.Doc can help developers to resolve their problems related to programming with Office technology. According to the description, the poster wants to replace each image in the Word file with “Here was image {image index}” correspondingly. However, we would like to provide a solution with below sample code.

Test File:

Replace Images in Word with Texts in C#

Code Snippet for Replace Images with Texts

Step 1: Create a new Word document and load the test file.

Document document = new Document(@"..\..\test.docx");

Step 2: Get all pictures in the Word document and replace them with the text "Here was image {image index}".

int j = 1;
foreach (Section sec in document.Sections)
{
    foreach (Paragraph para in sec.Paragraphs)
    {
        List pictures = new List(); 
        foreach (DocumentObject docObj in para.ChildObjects)
        {
            if (docObj.DocumentObjectType == DocumentObjectType.Picture)
            {
                pictures.Add(docObj);
            }
        }
        foreach (DocumentObject pic in pictures)
        {
            int index = para.ChildObjects.IndexOf(pic);
            TextRange range = new TextRange(document);
            range.Text = string.Format("Here was image {0}", j);
            para.ChildObjects.Insert(index, range);
            para.ChildObjects.Remove(pic);
            j++;
        }
    }
}

Step 3: Save and launch the file.

          document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
          System.Diagnostics.Process.Start(@"..\..\result.docx");

Result:

Replace Images in Word with Texts in C#

Full C# Code:

[C#]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Doc;
using Spire.Doc.Documents;
using Spire.Doc.Fields;
using Spire.Doc.Interface;
using System.Drawing;

namespace ReplaceImageWithText
{
    class Program
    {
        static void Main(string[] args)
        {
            Document document = new Document(@"..\..\test.docx");
            int j = 1;
            foreach (Section sec in document.Sections)
            {
                foreach (Paragraph para in sec.Paragraphs)
                {
                    List pictures = new List(); 
                    foreach (DocumentObject docObj in para.ChildObjects)
                    {
                        if (docObj.DocumentObjectType == DocumentObjectType.Picture)
                        {
                            pictures.Add(docObj);
                        }
                    }
                    foreach (DocumentObject pic in pictures)
                    {
                        int index = para.ChildObjects.IndexOf(pic);
                        TextRange range = new TextRange(document);
                        range.Text = string.Format("Here was image {0}", j);
                        para.ChildObjects.Insert(index, range);
                        para.ChildObjects.Remove(pic);
                        j++;
                    }
                }
            }
            document.SaveToFile(@"..\..\result.docx", FileFormat.Docx);
            System.Diagnostics.Process.Start(@"..\..\result.docx");
        }
    }
}
Page 1 of 25