Import an Element with Styles. Mode: KeepDifferentStyles


KeepDifferentStyles means to only copy styles that are different by formatting.
If the destination document already contains a style with the same name, therefore an unique style name will be generated.

For example, a destination document contains a style "MyGreen" (FontSize = 20, Green, Underline).

Destination:

And a source document also contains a style with the same formatting (FontSize = 20, Green, Underline), but a different name "Green" .

Let's import a 1st paragraph from the source document.
The paragraph contains text "Shrek" marked by the style "Green".
Because of the formatting of styles are equal, the "Green" style will not be imported.

Source:

(SourceStyles.docx)

After the importing, all imported elements linked to style "Green" will be remapped to the equal style "MyGreen":

Result:

Complete code

using SautinSoft.Document;
using SautinSoft.Document.Tables;
using System.Linq;

namespace Sample
{
    class Sample
    {

        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            ImportKeepDifferentStyles();
        }

        /// <summary>
        /// Import an Element with Styles from another document. Mode: KeepDifferentStyles.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-different-styles.php
        /// </remarks>
        private static void ImportKeepDifferentStyles()
        {
            // Mode: KeepDifferentStyles 
            // The most useful mode to preserve formatting during importing.

            // 'KeepDifferentStyles' means to only copy styles that are different by formatting.
            // If the destination document already contains a style with the same name, 
            // therefore an unique style name will be generated.

            // For example, a destination document contains a style "MyGreen" (FontSize = 20, Green, Underline).
            // And a source document also contains a style with name "Green" (FontSize = 20, Green, Underline).
            // Because of the formatting of styles are equal, the "Green" style will not be imported.
            // All imported elements linked to style "Green" will be remapped to style "MyGreen"

            DocumentCore source = DocumentCore.Load(@"..\..\..\SourceStyles.docx");
            DocumentCore dest = new DocumentCore();

            // Create a new style "MyGreen" (FontSize = 20, Green, Underline).
            CharacterStyle chStyle = new CharacterStyle("MyGreen");
            chStyle.CharacterFormat.FontColor = Color.Green;
            chStyle.CharacterFormat.Size = 20;
            chStyle.CharacterFormat.FontName = "Calibri";
            chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single;
            dest.Styles.Add(chStyle);
            dest.Content.End.Insert(new Run(dest, "This text has the style MyGreen.", new CharacterFormat() { Style = chStyle }).Content);

            // Create an ImportSession with mode 'KeepDifferentStyles'.
            ImportSession session = new ImportSession(source, dest, StyleImportingMode.KeepDifferentStyles);

            // Let's import a paragraph.
            // The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline). 
            // The style "Green" will not be imported, because we already have "MyGreen" with the same formatting.
            // All links to "Green" will be remapped to "MyGreen".
            Paragraph importedPar = dest.Import<Paragraph>((Paragraph)source.Sections[0].Blocks[0], true, session);
            dest.Content.End.Insert(importedPar.Content);                  

            // Save the destination document into DOCX format.
            string docPath = "KeepDifferentStyles.docx";
            dest.Save(docPath);

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(docPath) { UseShellExecute = true });
        }    
    }
}

Download

Imports SautinSoft.Document
Imports SautinSoft.Document.Tables
Imports System.Linq

Namespace Sample
    Friend Class Sample

        Shared Sub Main(ByVal args() As String)
            ImportKeepDifferentStyles()
        End Sub
        ''' Get your free 30-day key here:   
        ''' https://sautinsoft.com/start-for-free/
        ''' <summary>
        ''' Import an Element with Styles from another document. Mode: KeepDifferentStyles.
        ''' </summary>
        ''' <remarks>
        ''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/import-element-keep-different-styles.php
        ''' </remarks>
        Private Shared Sub ImportKeepDifferentStyles()
            ' Mode: KeepDifferentStyles 
            ' The most useful mode to preserve formatting during importing.

            ' 'KeepDifferentStyles' means to only copy styles that are different by formatting.
            ' If the destination document already contains a style with the same name, 
            ' therefore an unique style name will be generated.

            ' For example, a destination document contains a style "MyGreen" (FontSize = 20, Green, Underline).
            ' And a source document also contains a style with name "Green" (FontSize = 20, Green, Underline).
            ' Because of the formatting of styles are equal, the "Green" style will not be imported.
            ' All imported elements linked to style "Green" will be remapped to style "MyGreen"

            Dim source As DocumentCore = DocumentCore.Load("..\..\..\SourceStyles.docx")
            Dim dest As New DocumentCore()

            ' Create a new style "MyGreen" (FontSize = 20, Green, Underline).
            Dim chStyle As New CharacterStyle("MyGreen")
            chStyle.CharacterFormat.FontColor = Color.Green
            chStyle.CharacterFormat.Size = 20
            chStyle.CharacterFormat.FontName = "Calibri"
            chStyle.CharacterFormat.UnderlineStyle = UnderlineType.Single
            dest.Styles.Add(chStyle)
            dest.Content.End.Insert((New Run(dest, "This text has the style MyGreen.", New CharacterFormat() With {.Style = chStyle})).Content)

            ' Create an ImportSession with mode 'KeepDifferentStyles'.
            Dim session As New ImportSession(source, dest, StyleImportingMode.KeepDifferentStyles)

            ' Let's import a paragraph.
            ' The imported paragraph contains a text with style "Green" (FontSize = 20, Green, Underline). 
            ' The style "Green" will not be imported, because we already have "MyGreen" with the same formatting.
            ' All links to "Green" will be remapped to "MyGreen".
            Dim importedPar As Paragraph = dest.Import(Of Paragraph)(CType(source.Sections(0).Blocks(0), Paragraph), True, session)
            dest.Content.End.Insert(importedPar.Content)

            ' Save the destination document into DOCX format.
            Dim docPath As String = "KeepDifferentStyles.docx"
            dest.Save(docPath)

            ' Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(docPath) With {.UseShellExecute = True})
        End Sub
    End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.