Append Word Document Nodes using C# .NET Code & Copy All Required Styles to Destination Document & Generate Unique Style Names

Hi Team,

I’ve picked up some styling issues when importing source document nodes into a destination document. I will try to clearly explain my scenario below:

My Destination document has 2 styles, MyStyle1 and MyStyle2. The Source document has MyStyle1, MyStyle2 and MyStyle3.

MyStyle1 - formatted the same in the source and destination document.
MyStyle2 - is formatted differently in the source and destination document.
MyStyle3 - exists only in the source document.

My goal is to import the Paragraph nodes from the Source doc into the Destination doc.

When using the NodeImporter and setting the ImportFormatMode to UseDestinationStyles, I get the expected output. However I’m getting issues when setting the ImportFormatMode to KeepSourceFormatting and KeepDifferentStyles.

When using ImportFormatMode.KeepSourceFormatting, I expected MyStyle2 from the source doc, to be imported into the destination doc under the name “MyStyle2_0” (as per “Remarks” in the documentation here) but it does not exist.

When using ImportFormatMode.KeepDifferentStyles, I expected MyStyle1 to be reused, however a new style called “MyStyle1_0” was created in the destination doc - and the paragraph node from the source doc uses that one instead.

Would you please be able to help me with this? Not sure if it’s a bug or a misunderstanding on my side.
Thank you!

DestinationDoc.zip (9.7 KB)
SourceDoc.zip (9.8 KB)

UseDestinationStyles.zip (19.4 KB)
KeepSourceFormatting.zip (19.5 KB)
KeepDifferentStyles.zip (19.6 KB)

@morrism,

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

Thank you Awais!

The issues you have found earlier (filed as WORDSNET-17538) have been fixed in this Aspose.Words for .NET 19.1 update and this Aspose.Words for Java 19.1 update.

Hi @awais.hafeez,

I’ve tested my issues on Aspose.Words V19.1 and it seems like the issue for KeepDifferentStyles ImportFormatMode was fixed.
However I think there’s still a bug with the KeepSourceFormatting ImportFormatMode. I’ve also tested on the latest version (19.9), and got the same result.

I’ve attached some unit test projects for testing the ImportFormatMode across different versions of Aspose. The SourceDocument.docx and DestinationDocument.docx are included in the “Shared” project.

Please let me know if you need any other info, or if you’re having issues with the unit test projects.
Thank you!

ImportFormatModeIssues.zip (90.8 KB)

@morrism,

We are working over your query and will get back to you soon.

Thanks Awais!

@morrism,

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

Hi Awais,

Thank you for the update.

@morrism,

Regarding WORDSNET-19268, please note that in this case, Aspose.Words mimics the behavior of MS Word.

The unexpected result occurs while asserting the following conditions:

// MyStyle1 – same formatting as Destination
Paragraph sourceMyStyle1Para = allParagraphs.FirstOrDefault(p => p.Range.Text.Contains("MyStyle1 – same formatting as Destination"));
AssertParagraphStyle(sourceMyStyle1Para, "MyStyle1_0");

// MyStyle2 – Different formatting to Destination
Paragraph sourceMyStyle2Para = allParagraphs.FirstOrDefault(p => p.Range.Text.Contains("MyStyle2 – Different formatting to Destination"));
AssertParagraphStyle(sourceMyStyle2Para, "MyStyle2_0");

I.e. you expect that “MyStyle1_0” and “MyStyle2_0” should be applied to the paragraphs accordingly. Actually, if we will try to import such document using the MS Word then we will not see any copies in the destination. MS Word imports styles and lists always using “UseDestinationMode” first, i.e. it does not override existing style.

i.e. no copies, no overrides just re-using for existing styles. Note - you are copying paragraphs one by one.

foreach (Node sourceNode in sourceDoc.FirstSection.Body)
{
  Node destinationNode = nodeImporter.ImportNode(sourceNode, importChildren);
  destinationDoc.FirstSection.Body.AppendChild(destinationNode);
}

So, paragraphs should be copied in the similar manner while checking MS Word’s behavior and results will be equal to Aspose.Words:

  1. MyStyle1 applied to the first imported paragraph.
  2. Second imported paragraph has not style in the formatting (i.e. Normal style).
  3. MyStyle3 applied to the third imported paragraph.

So, this turns out to be not a bug, but just an expected behavior. If we can help you with anything else, please feel free to ask.

Hi Awais,

Thank you for the response.

We could not find anywhere that Microsoft exposes flags that control styles during import - this looks like an Aspose feature.

The attached tests show how Aspose behaviour with regards to ImportFormatMode has changed over releases, although the documentation relating to ImportFormatMode has not. The Aspose documentation here goes into detail about how the styles get imported when using the different ImportFormatMode enums.
If this is controlled solely by Microsoft, then it does not makes sense to have this documentation.

We have taken a dependency on this functionality in our product, and since then our clients have developed thousands of templates that also depend on this functionality and how it used to work in previous versions (up to v17.6) as described in the Aspose documentation.

If we upgrade to any of the latest Aspose versions, our client templates display differently due to how the styles are now being imported. We cannot ask our clients to re-design their templates, and for us to attempt to do it would be too great a task, so upgrading Aspose at this stage is not an option.

Since we can’t upgrade Aspose, this prevents us from receiving other Aspose bug fixes or features.

Please let me know if I’m just misunderstanding - and thank you for the help thus far.

1 Like

I suspect the issue I raised here List styles when using ImportFormatMode.KeepSourceFormatting is related to your issue.

My organisation’s use-case also requires this functionality. 17.6 seems to be the last release that this feature worked in. Unfortunately, staying at 17.6 isn’t an option for us because v17.6 doesn’t support .NET Standard 2.0/.NET Core and all our code moving forward is built for .NET Standard 2.0/.NET Core, which is in line with Microsoft’s recommendations for modern .NET development.

1 Like

@morrism,

We have logged your concerns/questions in our issue tracking system and will keep you posted on any further updates.

Thanks Awais.

Hi @awais.hafeez,

I hope you are well. Just wanted to ask if you have any feedback for me regarding this issue? This is really a big issue for us and we’re hoping that you guys can help out soon, since it has been an issue for a very long time.

Thanks.

@morrism,

Regarding WORDSNET-19268, unfortunately this issue is not resolved yet and there is no further news about this issue. We will keep you posted on any further updates and let you know via this thread when this issue will be resolved in future. We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-19268) have been fixed in this Aspose.Words for .NET 22.1 update also available on NuGet.