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

Free Support Forum - aspose.com

Custom list bullet image lost on CopyStylesFromTemplate

There seems to be an issue with the CopyStylesFromTemplate, which loses a custom list bullet image. We are merging documents into a template document, which contains the default Word styles. When we import every node and set the ImportFormatMode to KeepSourceFormatting, the custom list bullet seems to be copied correctly. However we must also use the CopyStylesFromTemplate method, and when using this the custom list bullet image is gone from the destination.

This bullet image is inside a style.

If you run the sample project, you can see the bullet is lost. If you remove line 20 destination.CopyStylesFromTemplate(source); the bullet image is copied correctly to the output file.
We are currently on Aspose Words 21.5.0, but I have also tested this in 22.6.0

CopyStyles.zip (114.6 KB)

@hro_jordy You should set KeepSourceNumbering to get the desired output. Try using the following code:

NodeImporter importer = new NodeImporter(source, destination, ImportFormatMode.KeepSourceFormatting, 
    new ImportFormatOptions { IgnoreHeaderFooter = false, IgnoreTextBoxes = false, KeepSourceNumbering = true });

Hi @alexey.noskov, thank you for the suggestion. I have tried it and it fixes the bullet icon, unfortunately it breaks a different part or the documents. The table of contents is not rendered like the input documents.

I have an updated sample project. Without the KeepSourceNumbering all is rendered correctly except the bullet icon, with this option all is rendered correctly except the Table of contents.

Also it seems a bit strange that CopyStylesFromTemplate does copy the list style, just not the bullet icon. But only using the ImportFormatMode is KeepSourceFormatting does copy the style correctly.

PS: don’t mind the theme related color differences in the sample project output, that’s not the issue.

CopyStyles v2.zip (171.8 KB)

@hro_jordy Thank you for additional information. Formatting of TOC become correct is update fields in the document.

destination.UpdateFields();
destination.Save(@"C:\Temp\OutputDocument.docx");

But could you please explain why do you need to use CopyStylesFromTemplate method? The problem is that you use this method several times with different documents, and this might lead into unexpected behavior. For example formatting of TOC items is defined in the appropriate TOC1…TOC9 styles. If your source documents has different TOC1…TOC9 styles only the last TOC styles will be used. Could you please explain your requerements?

Also, it looks like InjectDocument method does almost the same as DocumentBuilder.InsertDocument method. If you need simply merge documents together, you can use Document.AppendDocument.

Hi @alexey.noskov, thank you for the support. Let me try to clarify why we need this method. So like the sample project also has, we currently have a template document, with the basic Word styles and theme. Inside this document there is some logic to identify other generated documents that will be merged into this template document. These generated documents have their own custom style and theme. These styles and them are pretty much the same.

Initially I only used the KeepSourceFormatting, which should just copy the style of the elements/nodes into the destination file, which has always worked fine for our documents, but with our latest document this suddenly had some issues. I noticed that not all elements/nodes kept their source formatting, but received the destination style, while these elements/nodes should also have been imported with the KeepSourceFormatting option.

So for example some paragraphs of the document have the source style “Normal”, font Calibri, while others reverted to the destination style “Normal”, font Verdana.

I’m not quite sure why certain paragraphs receive the destination style. So what I needed was to overwrite the styles in the destination file, to match the source styles. Therefor I needed CopyStylesFromTemplate. At this moment there is only one TOC style, while there are two TOC’s in the document, so that is not an issue.

We are working with Aspose.Words for a long time now (since 2005 maybe?), so I’m not exactly sure why we needed our own InjectDocument, instead of InsertDocument. Perhaps to have some more control on inserting documents into another document.

I will try UpdateFields and will review if this creates a different issue in our case. Perhaps I can also create a sample project which illustrates the behaviour source/destination style issue of my document.

@hro_jordy Thank you for additional information and clarification. But in this case CopyStylesFromTemplate is not required, because when you use KeepSourceFormatting option styles used in the source document are copied into the destination document, if style from the source document already exist in the destination document, Aspose.Words creates a style with name like StyleName_1. So it is not required to additionally copy styles from the source document into destination. Also, you should note that CopyStylesFromTemplate overrides styles in the destination document, so if your destination document has style with the same name as in the source document, style from source document will override formatting defined by style in the destination document. This might cause formatting lose in the destination document.

Could you please attach document that cause the original problem, i.e. when source formatting is not preserved when you copy content from one document into another? We will investigate your document and provide you more information.

Hi @alexey.noskov,

Thank you for the support. I’ve added a sample project which illustrates our original issue. You can see the text in the table originally is set to Calibri, but in the output is set to Verdana, while other text’s keep their original source style of Calibri.
I have to note this this project is created with Aspose.Words 21.5.0 (the version we are currently using) and I notice this is not an issue in 22.6.0. However the Table of Contents style is not properly copied in the newest Aspose.Words version.

CopyStyles v2.2.zip (161.5 KB)

So using this sample project, is there a better “workaround” than using CopyStylesFromTemplate, or is the only solution to upgrade?

@hro_jordy It looks like a bug in the older version of Aspose.Words in the way it handles content in table with applied style. Unfortunately, I did not find a way to work the problem around with the old version of Aspose.Words. So I would suggest you to update to a newer version. It looks like a defect was resolved in 22.3 version of Aspose.Words.

Hi @alexey.noskov,

Thank you for the notification. We will have to investigate what it will take to upgrade to the latest version. Although my solution builds, in my first test it seems to break some things with our templates. I’ll need to investigate what causes these issues and what else is different.

Thank you for the support so far.

@hro_jordy Please feel free to ask in case of any issues with upgrade. We will be glad to help you.