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.
@hro_jordy If you would like to test new version of Aspose.Words without evaluation version limitations you can request a temporary 30-days license.
Hi @alexey.noskov,
Currently I’m testing the newest version of Aspose (22.9). Unfortunately I have the following issue, which I hope you can guide me with. In the attached sample project, there are 2 cases:
- Confirm that the bug is solved which was present in 21.5, with my original setup. Running this case in 22.9 confirms the fix, but in the TOC’s not all the styling in the output document is present which is present in the template documents (the TOC gets underlined and is not green).
- Using your suggestion “UpdateFields” is not usable, as it does fix the styling, but unfortunately combines the content of both documents in the TOC, which is not desired.
Can you clarify why the styling of the TOC is lost? Or how I can fix this with the current setup?
Sample2.4.zip (248.5 KB)
@hro_jordy Thank you for additional information.
- I have managed to reproduce the problem on my side. It has been logged as WORDSNET-24332. We will et you know once it is resolved. The problem occurs only when
ImportFormatMode.KeepSourceFormatting
so as a workaround you can use ImportFormatMode.KeepDifferentStyles
or ImportFormatMode.UseDestinationStyles
.
- This is expected behavior since whole TOC field is updated and all heading paragraphs are counted.
Hi @alexey.noskov, thank you for confirming the issue. Currently we will wait for this issue to be fixed. Currently we will stay on version 21.5, which results in the lost bullet icons. When this TOC issue is fixed we will review the upgrade again.
1 Like
@hro_jordy Each TOC entry has TOCX
paragraph style and HYPERLINK
character style (except page number). MS Word ignores HYPERLINK
style when displays TOC field result, but does not ignore when copies TOC
field with Keep Source Formatting
option. Aspose.Words behaves the same way and expands HYPERLINK
style. So the issue is considered as Not a bug
.
Actually, TOC fields may only include entries from original document. Appended documents should be wrapped with bookmark and the FieldToc.BookmarkName property should be specified accordingly:
private static void InjectDocument1(Document destination, Document source)
{
AdjustTocFields(source);
...
}
private static void AdjustTocFields(Document document)
{
var fieldTocs = document.Range.Fields
.OfType<FieldToc>()
.Where(p => string.IsNullOrEmpty(p.BookmarkName))
.ToList();
if (!fieldTocs.Any())
return;
var bookmarkName = "_" + Guid.NewGuid();
foreach (var fieldToc in fieldTocs)
fieldToc.BookmarkName = bookmarkName;
document.FirstSection.Body.InsertAfter(new BookmarkStart(document, bookmarkName), null);
document.LastSection.Body.AppendChild(new BookmarkEnd(document, bookmarkName));
}
The issues you have found earlier (filed as WORDSNET-24332) have been fixed in this Aspose.Words for .NET 24.1 update also available on NuGet.