Copy a Style

How would I copy a style from 1 document to another?

void CopyStyle(Document from, Document to, string styleName)
{
    Style styleToCopy = from.Styles[styleName];
    // ??
}

Hi

Thanks for your request. There is no way to copy styles between documents using Aspose.Words. This is the known issue #3286 in our defect database.
Best regards.

In case anyone needs this functionality (including the Aspose Developers)

I’ve attached a zip file containing the class I created. This code isn’t 100% complete, but it did the job for me.

Hi

It is nice that you have found a workaround of this issue. This code will work for simple styles. However, some of style properties that Aspose.Words exposes are calculated and some are not exposed at all. That is why the code cannot be 100% complete.
Best regards.

The Aspose Defect #3286 covering this issue seems to have been opened back in April 2007 !!
Resolving this issue would drastically improve our users ability to control the styles within our report generation mechanisms.
Has there been any further development/plans on this issue recently? Are there any intentions to ever resolve this issue?
Thanks

Hi

Thanks for your inquiry. Unfortunately, this feature is still unavailable. But if it is acceptable for you, I can try to suggest you a workaround. As you may know, Aspose.Words copies styles from one document to another upon importing nodes between documents. So you can use this effect to copy styles between documents. Here is simple code example:

// Open documents 
Document src = new Document(@"Test001\in1.doc");
Document dst = new Document(@"Test001\in1.doc");
// Copy styles.
CopyStyle(src.Styles["myParagraphStyle"], dst);
CopyStyle(src.Styles["myCharStyle"], dst);
// Save ourpur document.
dst.Save(@"Test001\out.doc");

====================================================================

/// 
/// Method copies style from one document to another.
/// Works only for Paragraph and Character styles.
/// 
/// Style, which should be copied.
/// Destination document.
private void CopyStyle(Style styleToCopy, Document dstDoc)
{
    if (styleToCopy.Type != StyleType.Paragraph && styleToCopy.Type != StyleType.Character)
        throw new ArgumentException("Style should be Paragraph or Character style.");
    if (styleToCopy.Type == StyleType.Paragraph)
    {
        // Create an empty paragraph and assign the style.
        Paragraph par = new Paragraph(styleToCopy.Document);
        par.ParagraphFormat.Style = styleToCopy;
        // Import paragraph to the destination document.
        dstDoc.ImportNode(par, true, ImportFormatMode.UseDestinationStyles);
    }
    else if (styleToCopy.Type != StyleType.Character)
    {
        // Crreate an empty run and assign the style.
        Run run = new Run(styleToCopy.Document);
        run.Font.Style = styleToCopy;
        // Import Run to the destination document.
        dstDoc.ImportNode(run, true, ImportFormatMode.UseDestinationStyles);
    }
}

Hope this helps.
Best regards.

The issues you have found earlier (filed as WORDSNET-1268) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.
(22)

Which Api solves this problem? Adding style from one document to another?

Hi Praneeth,

Thanks for your inquiry. Yes, we have added possibility to copy styles between documents. See StyleCollection.AddCopy method.

Best regards,