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

Free Support Forum - aspose.com

importNode method on a document causes loss of the Style

Hi,

i’m trying to import the content of a sdt of a Document A to a sdt of a Document B using the importNode method, but i 've found two strange behaviors.
  1. on the origin sdt content i set the word style “Strong” (from Word ribbon bar - Home - Styles), but after import that nodes, in the destination sdt, despite the formatting was kept properly, if i looking for the style, the selected one is “Normal” and not “Strong” as i expected.
  2. In the origin sdt there was 3 paragraphs and i can selected each of them and change their styles, but in the destination sdt seems to be a single block and i can’t select the paragraphs individually.
Any idea why?

here it is the piece of code, i attach also the source, the destination and the output documents.

<pre style=“background-color: rgb(255, 255, 255); font-family: “Courier New”;”>public class AsposeImportStyleTest {
public static void main(String[] args) {
try {
Document dstDoc = new Document( destinationDocumentPath”);
Document srcDoc = new Document( sourceDocumentPath”);
Node referencedSdt = srcDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
StructuredDocumentTag sdt = (StructuredDocumentTag)dstDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
sdt.removeAllChildren();
NodeCollection childNodes = ((StructuredDocumentTag) referencedSdt).getChildNodes();
for (Node child: childNodes) {
Node importedNode = dstDoc.importNode(child, true, ImportFormatMode.USE_DESTINATION_STYLES);
sdt.appendChild(importedNode);
}
dstDoc.save(importNodeOuputPath”);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

Here it is another strange behavior.

The source sdt have the “ASF Text Style” style, but after importing its content into the destination document sdt, the style of its content become “Normal”. I expected it to be “ASF Text Style”. How can we keep the style?
Debugging it i’ve noticed that the child of the source have the right formatting, but after import it in the source document the imported node loss the formatting. For example the font is not “Time new roman anymore”, but become “Calibri”. It seems that if the content control is of level “INLINE” the import node loss the style.
I attach the documents with wich you can reproduce the issue (sourceDocument2, destinationDocument2, importNodeOutput2).

Thanks.

Hi Metteo,

m4tte:
Hi,
i'm trying to import the content of a sdt of a Document A to a sdt of a Document B using the importNode method, but i 've found two strange behaviors.
  1. on the origin sdt content i set the word style "Strong" (from Word ribbon bar - Home - Styles), but after import that nodes, in the destination sdt, despite the formatting was kept properly, if i looking for the style, the selected one is "Normal" and not "Strong" as i expected.
  2. In the origin sdt there was 3 paragraphs and i can selected each of them and change their styles, but in the destination sdt seems to be a single block and i can't select the paragraphs individually.
Any idea why?

here it is the piece of code, i attach also the source, the destination and the output documents.

public class AsposeImportStyleTest {
public static void main(String[] args) {
try {
Document dstDoc = new Document( "destinationDocumentPath");
Document srcDoc = new Document( "sourceDocumentPath");
Node referencedSdt = srcDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
StructuredDocumentTag sdt = (StructuredDocumentTag)dstDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
sdt.removeAllChildren();
NodeCollection childNodes = ((StructuredDocumentTag) referencedSdt).getChildNodes();
for (Node child: childNodes) {
Node importedNode = dstDoc.importNode(child, true, ImportFormatMode.USE_DESTINATION_STYLES);
sdt.appendChild(importedNode);
}
dstDoc.save("importNodeOuputPath");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}


Thanks for your inquriy. Please try following code snippet, it will help you to import the Structured Document Tag(SDT) as expected. It imports correct style(Strong) and each paragraph separately.

com.aspose.words.Document dstDoc = new com.aspose.words.Document( "destinationDocument.DOCX");

com.aspose.words.Document srcDoc = new com.aspose.words.Document( "sourceDocument.DOCX");

StructuredDocumentTag sdt = (StructuredDocumentTag)dstDoc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);

sdt.removeAllChildren();

for (Object t: srcDoc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true)) {

StructuredDocumentTag child = (StructuredDocumentTag)t;

Node importedNode = dstDoc.importNode(child, true, ImportFormatMode.USE_DESTINATION_STYLES);

sdt.appendChild(importedNode);

}

dstDoc.save("importNodeOuputPath.docx");


Best Regards,


Hi Matteo,


m4tte:
Here it is another strange behavior.
The source sdt have the “ASF Text Style” style, but after importing its content into the destination document sdt, the style of its content become “Normal”. I expected it to be “ASF Text Style”. How can we keep the style?
Debugging it i’ve noticed that the child of the source have the right formatting, but after import it in the source document the imported node loss the formatting. For example the font is not “Time new roman anymore”, but become “Calibri”. It seems that if the content control is of level “INLINE” the import node loss the style.
I attach the documents with wich you can reproduce the issue (sourceDocument2, destinationDocument2, importNodeOutput2).

Thanks.


I have tested your scenario with above shared documents and noticed the SDT style import issue, so logged a ticket WORDSNET-15111 in our issue tracking system for further investigation and resolution. We will notify you as soon as it is resolved.

We are sorry for the inconvenience.

Best Regards,

Hi Matteo,


Thanks for your patience. Please note we have investigated the issue and noticed that it is not Aspose.Words bug. It is expected behavior.

Please note that Aspose.Words mimics the same behavior as Microsoft Word does. If you try the scenario with Microsoft Word then you will see there is no way to preserve style during copying content into SDT. Only If the styles are specified directly inside paragraph(located inside SDT) then Aspose.Words and Microsoft Word can preserve styles during copying of SDT contents.

Best Regards,

The issues you have found earlier (filed as ) have been fixed in this update. This message was posted using BugNotificationTool from Downloads module by MuzammilKhan