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

Free Support Forum - aspose.com

copyStylesFromTemplate not working for every element in word

I`m trying to apply branding functionality to the word templates using Aspose.Word for Java. Partially it is working just fine, but for some elements like the table, the styling is not applied.

@Test
void name() throws Exception {
    byte[] brandingBytes = Files.readAllBytes(new File("branding.docx").toPath());
    byte[] sourceBytes = Files.readAllBytes(new File("branding-document-base.docx").toPath());
    com.aspose.words.Document brandingDocument = new com.aspose.words.Document(new ByteArrayInputStream(brandingBytes));
    com.aspose.words.Document sourceDocument = new com.aspose.words.Document(new ByteArrayInputStream(sourceBytes));

    sourceDocument.copyStylesFromTemplate(brandingDocument);
    sourceDocument.setAutomaticallyUpdateStyles(true);
    sourceDocument.save("output.docx");
}

In both source and branding word templates, I have defined custom and default styles. So both files have the same set of styles. In the source file default font is Arial, in the branding, it is defined as Calibri.
After the code is executed I see that the regular text and heading has the correct font - Calibri, but table content remained on Arial.

I have tried both docx and dotx formats of the branding file with the same result.
examples.zip (67.8 KB)

@artyomsv2 Your source document (branding-document-base.docx) contains TableGrid style, which is applied to the table. This style does not exist in the template (branding.docx) and in this style font is explicitly specified:

	<w:style w:type="table" w:styleId="TableGrid">
		<w:name w:val="Table Grid"/>
		<w:basedOn w:val="TableNormal"/>
		<w:rsid w:val="00D81CEF"/>
		<w:pPr>
			<w:spacing w:after="0" w:line="240" w:lineRule="auto"/>
		</w:pPr>
		<w:rPr>
			<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
			<w:lang w:val="de-DE"/>
		</w:rPr>
		<w:tblPr>
			<w:tblBorders>
				<w:top w:val="single" w:sz="4" w:space="0" w:color="auto"/>
				<w:left w:val="single" w:sz="4" w:space="0" w:color="auto"/>
				<w:bottom w:val="single" w:sz="4" w:space="0" w:color="auto"/>
				<w:right w:val="single" w:sz="4" w:space="0" w:color="auto"/>
				<w:insideH w:val="single" w:sz="4" w:space="0" w:color="auto"/>
				<w:insideV w:val="single" w:sz="4" w:space="0" w:color="auto"/>
			</w:tblBorders>
		</w:tblPr>
	</w:style>

If you remove explicit font, the font will be inherited from TableNormal style, which exist in your template (branding.docx). Please see the modified document branding-document-base.docx (22.2 KB). The following tags were removed from the style definition:

		<w:rPr>
			<w:rFonts w:ascii="Arial" w:hAnsi="Arial"/>
			<w:lang w:val="de-DE"/>
		</w:rPr>

Here is the output produced with the modified document.output.docx (17.9 KB)

1 Like

thanks a lot for the quick reply and a solution!