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.
Hi Roman,
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");
Hi Awais,
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;}}
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");
Hi Awais,
Result of case 1 : “Heading 1_0” didn’t occur. But style count of destination document increased.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”);
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”);
In MS Words currently we have 5 style types : paragraph, character, linked, table, list.
Hi Roman,
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 ?
Hi Awais,
Hi Roman,
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.
Roman:
Result of case 2 : “Heading 1_0” occurred. And style count of destination document increased.
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,