Assign Style when Inserting Document into Another

Using this forum we have written a "InsertDocument" function that imports nodes from one document into another document for the purpose of merging files. I am trying to assign a style to the paragraphs after they are inserted into the destination document. The style is succesfully getting assigned however, the font size is getting overrided. For example:

1. Source.rtf is inserted into Destination document.
2. Each paragraph is assigned a style of "MyStyle" which is defined in the destination document as 8pt Gray text.
3. When opening the destination document in word the text that was inserted now has a style of "MyStyle + 10pt"

How can I prevent the +10pt from being added to the style that I am assigning to these inserted paragraphs? Here is the code:

Public Sub InsertDocument(ByVal oDestinationNode As Node, ByVal oDocToInsert As Document, Optional ByVal StyleName As String = Nothing)

'Validate Input
If oDestinationNode Is Nothing Then Exit Sub

'Determine the proper Destination & Parent Nodes by going up through the tree
Dim oParentNode As CompositeNode = oDestinationNode.ParentNode

Do While True

'The Destination Node Must have a Parent Node
If oParentNode Is Nothing Then Throw New Exception("Document cannot be inserted after the specified node.")

'Keep going UP the tree until we find a Story, Cell or Shape
If TypeOf oParentNode Is Story OrElse TypeOf oParentNode Is Cell OrElse TypeOf oParentNode Is Drawing.Shape Then Exit Do

'Move up the node tree 1 level
oDestinationNode = oParentNode
oParentNode = oDestinationNode.ParentNode

Loop

'Get the Index of the destination node so we can use the Insert method
Dim iDestinationIndex As Integer = oDestinationNode.ParentNode.ChildNodes.IndexOf(oDestinationNode)

'Get the Document so we can easily Import Nodes
Dim oDestinationDoc As Document = oDestinationNode.Document
Dim oInsertedSection As Section

'Get the Style so we can easily assign
Dim oStyle As Style
If Not StyleName Is Nothing Then oStyle = oDestinationDoc.Styles(StyleName)

'Go through all the Sections of the document being inserted
For Each oSection As Section In oDocToInsert.Sections

'Import the section into the document
oInsertedSection = CType(oDestinationDoc.ImportNode(oSection, True, ImportFormatMode.KeepSourceFormatting), Section)

'Can't insert the section because all the section breaks will mess the document
'up. We must go through all the child nodes and insert them individually.
For Each oInsertedNode As Node In oInsertedSection.Body.ChildNodes

'Do not insert node if it is a last empty paragarph in the section.
If TypeOf oInsertedNode Is Paragraph AndAlso oInsertedNode Is oSection.Body.LastChild AndAlso oInsertedNode.ToTxt.Equals(String.Empty) Then Exit For

'Copy the node (and children)
Dim oNewNode As Node = oInsertedNode.Clone(True)

'Insert the Node and increment index counter
oParentNode.ChildNodes.Insert(iDestinationIndex, oNewNode)
iDestinationIndex += 1

'Assign the Style if it is a paragraph node
If Not oStyle Is Nothing AndAlso oStyle.Type = StyleType.Paragraph AndAlso oNewNode.NodeType = NodeType.Paragraph Then CType(oNewNode, Paragraph).ParagraphFormat.Style = oStyle

Next

Next

End Sub

Hi,

Please attach both source and destination documents to let us reproduce the issue and figure out a solution.

Source Document

My mistake ... the previous attachment was the Template ... the DESTINATION document being inserted in TO.

The document attached to THIS post is the document being inserted. Please note that this file is an RTF file but the forum would not allow me to post a file with that extension so I renamed it with a TXT extension.

Thank you for attachment. It seems like you have to clear direct font formatting taken from the source document to allow inherit font attributes from the destination style:

'Assign the Style if it is a paragraph node

If Not oStyle Is Nothing AndAlso oStyle.Type = StyleType.Paragraph AndAlso oNewNode.NodeType = NodeType.Paragraph Then

Dim para As Paragraph = CType(oNewNode, Paragraph)

para.ParagraphFormat.Style = oStyle

For Each run As Run In para.Runs

run.Font.ClearFormatting()

Next

End If

That will clear all the formatting on the lines but I only want to clear the formatting that is already defined in the Style so it doesn't override the style.

For example if this

The quick brown fox jumped over the lazy dog...

is inserted into another document and then the style is set to "MyStyle" which is defined as:

MyStyle: Normal + Font Size: 8pt + Font Style: Verdana

(but bold is not defined as on or off) then the result should be that the whole paragraph gets formatted with the style but the word jumped would stay bold.

This is the way Word works when you set the style of a paragraph it maintains those things unless the style has them defined.

As far as I know, we do not have the automatic "match destination formatting" functionality, i.e. when the text being imported maintains its formatting such as bold or italic but gets font family and font size of the insertion point. If I'm right, then you should set those manually:

For Each run As Run In para.Runs

run.Font.Name = oStyle.Font.Name

run.Font.Size = oStyle.Font.Size

Next

But let me make sure first.

Just to clarify it would be something like this:

For Each run As Run In para.Runs

For Each property In Style.Properties

If oStyle.IsPropertyDefined(property) Then SetProperty(run, style, property)

Next

Next

I know you don't have that exact syntax but the way it works with word is that if a style does not define something then it is not changed when you set the style on the paragraph.

Thank you for the clarification. Since I couldn't see such functionality implemented, I have logged the request as #1481.

See ImportFormatMode in the API Reference.

There are KeepSourceFormatting and UseDestinationStyles options available now. It is true, there is a third way of doing this MatchDestinationFormatting, but Aspose.Words does not support it yet. Planned for the future. No estimate at this stage.

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