Preserve Theme When Copying Sections from One Word Document to Another in C#

2018-01-19 08:04:49 Written by  support iceblue
Rate this item
(0 votes)

A theme is a set of colors, fonts, and effects that determines the overall look of your Word document. Suppose you have a document which is neat and stylish, you’d like to copy contents of a section to another document without losing the theme and style. You can clone the theme to destination file using CloneThemeTo method.

Step 1: Create a Document object and load a sample Word file.

Document doc = new Document();
doc.LoadFromFile("theme.docx");

Step 2: Create a new Word document.

Document newWord = new Document();

Step 3: Clone default style, theme, compatibility from the source file to destination document.

doc.CloneDefaultStyleTo(newWord);
doc.CloneThemesTo(newWord);
doc.CloneCompatibilityTo(newWord);

Step 4: Add the cloned section to destination document.

newWord.Sections.Add(doc.Sections[0].Clone());

Step 5: Save the file.

newWord.SaveToFile("result.docx", FileFormat.Docx);

Output:

Preserve Theme When Copying Sections from One Word Document to Another in C#

Full Code:

using Spire.Doc;
namespace PreserveTheme
{
    class Program
    {

        static void Main(string[] args)
        {
            Document doc = new Document();
            doc.LoadFromFile("theme.docx");

            Document newWord = new Document();
            doc.CloneDefaultStyleTo(newWord);
            doc.CloneThemesTo(newWord);
            doc.CloneCompatibilityTo(newWord);
            newWord.Sections.Add(doc.Sections[0].Clone());

            newWord.SaveToFile("result.docx", FileFormat.Docx);

        }
    }
}

Additional Info

  • tutorial_title:
Last modified on Friday, 03 September 2021 04:01