Copy Style from One Document to Another

Is it possible to copy the style from one document to another.

What would be Ideal is something like this

Aspose.Word.Style style = GetNormalStyle(docpath); // This returns the style from a document

myDoc.Styles["Normal"] = style;

Yes, that will be fine to have such a functionality in the model. Unfortunately, our current API does not permit that. I have logged your proposal to our defect base as a feature request (issue #1056). We will try to implement it in one of our future versions.

Best regards,

Can you suggest the best way to work around it with the current version?

Cheers Bruce

You can import paragraph which has the required style from one document to another and then remove the paragraph. The style will stay in a new document.

Here is a working example:

string filename = Application.StartupPath + @"\testCopyStyle.doc";

string filenameOut = Application.StartupPath + @"\testCopyStyle Out.doc";

Document doc1 = new Document(filename);

Document doc2 = new Document();

foreach (Paragraph paragraph in doc1.GetChildNodes(NodeType.Paragraph, true))

{

if (paragraph.ParagraphFormat.StyleName == "Style1")

{

Paragraph addedParagraph = (Paragraph)doc2.ImportNode(paragraph, true, ImportFormatMode.KeepSourceFormatting);

doc2.LastSection.Body.AppendChild(addedParagraph);

addedParagraph.Remove();

break;

}

}

doc2.Save(filenameOut);

System.Diagnostics.Process.Start(filenameOut);

Please mind that there are four types of styles in MS Word: character, paragraph, list and table.

Only character and paragraph styles are currently supported in Aspose.Words. The example above works for paragraph styles only. The code for copying character style is pretty similar. You need just use Run in place of a Paragraph everywhere in the code.