Document.copyStylesFromTemplate() API not copying styles properly


#1

Hi,

We are using Document.copyStylesFromTemplate() API to copy styles from sample document to target document. In sample document there is a style named “TOC 1” which is neither bold nor italic and target document also has a style with same name which is both bold and italic.
After calling the copyStylesFromTemplate() API, the target document “TOC 1” style is both bold and italic. It should neither be bold nor italic as it should copy the style definition from sample document.

The code used is as below:

    public void testCopyStylesFromTemplate() {
        Document target =new Document("target.docx");
        Document sample =new Document("sample.docx");
        target.copyStylesFromTemplate(sample);
        StyleCollection styles = target.getStyles();
        Style toc1 = styles.get("TOC 1");
        Assert.assertFalse("Expected TOC1 style to be non bold", toc1.getFont().getBold());
Assert.assertFalse("Expected TOC1 style to be non italic", toc1.getFont().getItalic());
    }

Please find the documents in attachment ASPOSE DOCS.zip (30.1 KB)
.

Thanks
Manisha


#2

@oraspose,

We tested the scenario and have managed to reproduce the same problem on our end. For the sake of correction, we have logged this problem in our issue tracking system. The ID of this issue is WORDSNET-19193. We will further look into the details of this problem and will keep you updated on the status of correction. We apologize for your inconvenience.


#3

@oraspose,

Regarding WORDSNET-19193, we have completed the analysis on your issue and concluded to close this issue as ‘Not a Bug’. Please see the following analysis details:

Source document (sample.docx) does not contain definition for the “TOC 1” style in the markup. So, this style can not be copied to the target document. The code below demonstrates that “TOC 1” is missing in the source document.

Document sample = new Document(@"sample.docx");
Document target = new Document(@"target.docx");

const string toc1Name = "TOC 1";
Style srcToc1 = sample.Styles.FirstOrDefault(s => s.Name == toc1Name);
Assert.IsNull(srcToc1);

Style targetToc1 = target.Styles.FirstOrDefault(s => s.Name == toc1Name);
Assert.IsNotNull(targetToc1);

Also, we checked Word Automation method “CopyStylesFromTemplate” (https://docs.microsoft.com/en-us/office/vba/api/word.document.copystylesfromtemplate) and got the similar result i.e. “TOC 1” style is both bold and italic in the destination.