We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Styles duplicated when copy styles from document to another one

Hi Aspose,
Follow this link, I tried to copy all styles from document (file 1.dot of attachment) to another one (file 2.doc of attachment) using AddCopy method.
But after changing style name, it still have the suffix “_0” (this error occurs with some style such as Heading 1, Heading 2, Subtitle, …).
For example, I copied style “Heading 1” from 1.dot to 2.doc, it become “Heading 1_0”. Then I changed that style to “Heading 1”.
But the result document still have style “Heading 1_0”.

I tested with Aspose Words version 16.10.0 and 17.1.0

Would you please have a look at this issue ?
Thank in advance

Hi Roman,

Thanks for your inquiry. This is an expected behavior.

If Styles collection of 2.doc already contains a style with the same name, then new name is automatically generated by adding “_number” suffix starting from 0 e.g. “Normal_0”, “Heading 1_1” etc. Use Name setter for changing the name of the imported style.

However, you can use the following code to copy a style from 1.dot document to 2.doc and override an existing style in the destination document.

Document srcDoc = new Document(MyDir + @"1.dot");
Document doc = new Document(MyDir + @"dst.doc");
Style srcStyle = srcDoc.Styles[StyleIdentifier.Heading1];
// Change the font of the heading style to red.
srcStyle.Font.Color = Color.Red;
// The AddCopy method can be used to copy a style from a different document.
Style newStyle = doc.Styles.AddCopy(srcStyle);
// The name of the new style can be changed to the name of any existing style. Doing this will override the existing style.
newStyle.Name = "Heading 1";
doc.Save(MyDir + @"17.1.0.doc");

Hope, this helps.

Best regards,

Hi Awais,
I’ve already tried that way, but it didn’t worked.
After this step : Style newStyle = doc.Styles.AddCopy(srcStyle);
Destination document has “Heading 1” and “Heading 1_0”.
And after chaging style name, it still has “Heading 1_0” style.

This is my code :

public static void ApplyStyles(this Document destination, Document source)
{
    var importingStyleList = new List();

    var importingStyleNameList = new List();

    for (var index = 0; index < source.Styles.Count; index++)
    {
        string styleName;

        if (destination.Styles[source.Styles[index].Name] != null)
        {
            styleName = destination.Styles[source.Styles[index].Name].Name;
        }
        else
        {
            styleName = source.Styles[index].Name;
        }

        importingStyleNameList.Add(styleName);

        importingStyleList.Add(source.Styles[index]);
    }

    for (var listIndex = 0; listIndex < importingStyleList.Count; listIndex++)
    {
        if (!string.IsNullOrWhiteSpace(importingStyleNameList[listIndex]))
        {
            importingStyleList[listIndex].Name = importingStyleNameList[listIndex];
        }

        var targetStyle = destination.Styles.AddCopy(importingStyleList[listIndex]);

        targetStyle.Name = importingStyleList[listIndex].Name;
    }
}

Additional information, “Heading 1” style type of source document is “Linked (paragraph and character)” as image below :

word-styles-from-the-beginning-microsoft-word-3576.png534×539

I tried other source document with “Heading 1” style has type Paragraph, and it worked fine.

Do you have any clue for that ?
Thank in advance

Hi Roman,

Thanks for your inquiry. For the documents that you had attached in your first post, can you please try running the following code?

Document srcDoc = new Document(MyDir + @"1.dot");
Document doc = new Document(MyDir + @"2.doc");
Style srcStyle = srcDoc.Styles[StyleIdentifier.Heading1];
// Change the font of the heading style to red.
srcStyle.Font.Color = Color.Red;
// The AddCopy method can be used to copy a style from a different document.
Style newStyle = doc.Styles.AddCopy(srcStyle);
Console.WriteLine("-----BEFORE Style Count----->" + doc.Styles.Count);
foreach (Style style in doc.Styles)
{
    if (style.Name.StartsWith("Heading"))
        Console.WriteLine(style.Name);
}
// The name of the new style can be changed to the name of any existing style. Doing this will override the existing style.
newStyle.Name = "Heading 1";
Console.WriteLine("-----AFTER Style Count----->" + doc.Styles.Count);
foreach (Style style in doc.Styles)
{
    if (style.Name.StartsWith("Heading"))
        Console.WriteLine(style.Name);
}
doc.Save(MyDir + @"17.2.0.doc");

We have also attached 17.2.0.doc here for your reference. I am afraid, we don’t see any “Heading 1_0” in this Aspose.Words for .NET 17.2.0 generated DOC.

Best regards,

Hi Awais,
Thank you for your reply.
I tried your code, and it worked.
I also adjusted your code : I loop by styles of source document and copy them to destination document.
Please have a look at 2 cases below (sorry, I don’t know how to set format for the code):

Case 1 :

Document srcDoc = new Document(MyDir + @"1.dot");
Document doc = new Document(MyDir + @"2.doc");
foreach (Style style in sourceDoc.Styles)
{
    if (style.Name.StartsWith("Heading"))
    {
        var savedStyleName = style.Name;
        Console.WriteLine("-----BEFORE Style Count----->" + doc.Styles.Count);
        foreach (Style style1 in doc.Styles)
        {
            if (style1.Name.StartsWith("Heading"))
                Console.WriteLine(style1.Name);
        }

        Style newStyle = doc.Styles.AddCopy(style);
        newStyle.Name = savedStyleName;
        Console.WriteLine("-----AFTER Style Count----->" + doc.Styles.Count);
        foreach (Style style1 in doc.Styles)
        {
            if (style1.Name.StartsWith("Heading"))
                Console.WriteLine(style1.Name);
        }
    }
}
doc.Save(MyDir + @"17.2.0.doc");

Result of case 1 : “Heading 1_0” didn’t occur. But style count of destination document increased.

Case 2 :

Document srcDoc = new Document(MyDir + @"1.dot");
Document doc = new Document(MyDir + @"2.doc");
foreach (Style style in sourceDoc.Styles)
{
    var savedStyleName = style.Name;
    Console.WriteLine("-----BEFORE Style Count----->" + doc.Styles.Count);
    foreach (Style style1 in doc.Styles)
    {
        if (style1.Name.StartsWith("Heading"))
            Console.WriteLine(style1.Name);
    }

    Style newStyle = doc.Styles.AddCopy(style);
    newStyle.Name = savedStyleName;
    Console.WriteLine("-----AFTER Style Count----->" + doc.Styles.Count);
    foreach (Style style1 in doc.Styles)
    {
        if (style1.Name.StartsWith("Heading"))
            Console.WriteLine(style1.Name);
    }
}
doc.Save(MyDir + @"17.2.0.doc");

Result of case 2 : “Heading 1_0” occurred. And style count of destination document increased.

Do you have any clue for these case (and why the style count of destination document always increase) ?
Best regards

In MS Words currently we have 5 style types : paragraph, character, linked, table, list.
But Aspose just supports 4 : paragraph, character, table, list.

Could you explain me about it ?

Hi Roman,

Thanks for the additional information.

While using the latest version of Aspose.Words i.e. 17.2.0, we managed to reproduce these problems on our end. We have logged this issue in our bug tracking system. The ID of this issue is WORDSNET-14860. Your thread has also been linked to the appropriate issue and you will be notified as soon as it is resolved. Sorry for the inconvenience.

*Roman:
In MS Words currently we have 5 style types : paragraph, character, linked, table, list.
But Aspose just supports 4 : paragraph, character, table, list.

Could you explain me about it ?*

We are also in coordination with product team to get answer pertaining to your above query. Soon you will be updated with the required information.

Best regards,

Hi Awais,
Thank you for your support !!

Hi Roman,

Thanks for being patient. Regarding WORDSNET-14860, our product team has completed the work on your issue and has come to a conclusion that this issue and the undesired behavior you are observing is actually not a bug in Aspose.Words for .NET. So, we will close this issue as ‘Not a Bug’. Please see the following details:

Roman:
I loop by styles of source document and copy them to destination document.
Result of case 1 : “Heading 1_0” didn’t occur. But style count of destination document increased.

It is expected behavior. Description of the ‘StyleCollection.AddCopy’ method has the following paragraph: ‘Linked style is copied.’ Styles with prefix ‘Heading’ (Heading [1…9]) in the source document contains linked styles with names ‘Überschrift [1…9] Zchn’ and these styles does not exist in the destination. So linked styles will be copied to destination and client can see that every iteration increasing count of styles.

Roman:
Result of case 2 : “Heading 1_0” occurred. And style count of destination document increased.

Current case excludes filter for styles (starts with ‘Heading’). The reason of the occurrence of styles like a ‘Heading 1_0’ is linked styles too. For example, style ‘Überschrift 9 Zchn’ is linked to style ‘Heading 9’, so it will be added as ‘Heading 9_0’.
You can update second example in the following way:

Document srcDoc = new Document(MyDir + @"1.dot");
Document doc = new Document(MyDir + @"2.doc");
foreach (Style style in srcDoc.Styles)
{
    var savedStyleName = style.Name;
    Console.WriteLine("-----BEFORE Style Count----->" + doc.Styles.Count);
    foreach (Style style1 in doc.Styles)
    {
        if (style1.Name.StartsWith("Heading"))
            Console.WriteLine(style1.Name);
    }
    Style newStyle = doc.Styles.AddCopy(style);
    newStyle.Name = savedStyleName;
    // Replace old style in the destination with a new linked style from the source.
    if (!string.IsNullOrEmpty(newStyle.LinkedStyleName))
        doc.Styles[newStyle.LinkedStyleName].Name = style.LinkedStyleName;
    Console.WriteLine("-----AFTER Style Count----->" + doc.Styles.Count);
    foreach (Style style1 in doc.Styles)
    {
        if (style1.Name.StartsWith("Heading"))
            Console.WriteLine(style1.Name);
    }
}
doc.Save(MyDir + @"17.5-case2.doc");

*Roman:
In MS Words currently we have 5 style types : paragraph, character, linked, table, list.
But Aspose just supports 4 : paragraph, character, table, list.
Could you explain me about it ?
We will provide way in Aspose.Words API to create Linked Styles. The ID of this feature is WORDSNET-11556. Your thread has also been linked to this issue and you will be notified as soon as it is supported. Sorry for any inconvenience.

Also, you can use ‘Style.LinkedStyleName’ property to determine a linked style. It can be used to get the name of the Style linked to this one. It returns empty string if no styles are linked.

Best regards,*