When inserting document into inline SDT- Aspose applies paragraph formatting instead of run formatting

Hello,

Aspose Version: 15.8.0

please see attached documents for clarification:

  • Document_WithContentControl_4Aspose.docx: Base document
  • ParagraphWithFormatting.docx: Imported document
  • outInsert.docx: Result, with addition of manual copy/paste to compare

When using the code below to import data into an inline SDT, all run formatting of this paragraph is ignored. Only the paragraph formatting is used.
Wenn using word copy/paste (strg+a,c,v) to insert the data into the SDT, exactly the opposite behaviour can be observed.

Is there a way to reproduce word behaviour?

Thanks,
Alexander

public void shouldTestImportParagraphIntoSdt() throws Exception {

    Path tempDirPath = Files.createTempDirectory("shouldTestImportParagraphIntoSdt");
    String tempDir = tempDirPath + File.separator;
    System.out.println(tempDirPath);

    Document doc1 = new Document(getResourceAsStream(RESOURCE_DOCX_TEXT_WithContentControl4Aspose));
    Document doc2 = new Document(getResourceAsStream(RESOURCE_DOCX_PARAGRAPHWITHFORMATTING));
    // Remofe Aspose Watermark to properly test formatting;
    doc2.getFirstSection().getBody().getFirstParagraph().remove();

    DocumentBuilder documentBuilder = new DocumentBuilder(doc1);

    NodeCollection collection = doc1.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);

    for (StructuredDocumentTag sdt : collection) {

        switch (sdt.getTag()) {
            case "sdtBlock":
                sdt.removeAllChildren();
                Paragraph para = new Paragraph(doc1);
                sdt.appendChild(para);
                documentBuilder.moveTo(para);
                documentBuilder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);
                break;
            case "sdtInline":

                sdt.removeAllChildren();
                Run r = new Run(doc1);
                sdt.appendChild®;
                documentBuilder.moveTo®;
                documentBuilder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);
                break;
        }
    }

public Node insertDocument(Document srcDoc, int importFormatMode) throws java.lang.Exception

Inserts content of the document into the current position of DocumentBuilder’s cursor.
This method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document.
Parameters:

srcDoc - Source document for inserting.
importFormatMode - A ImportFormatMode value. Specifies how to merge style formatting that clashes.
Returns:
First inserted node.

Hi Alexander,

Thanks for your inquiry. There are three content controls in your document. The type of all content controls is “RichText”. So, please use following code snippet for all content controls.

sdt.removeAllChildren();
Paragraph para = new Paragraph(doc1);
sdt.appendChild(para);
documentBuilder.moveTo(para);
documentBuilder.insertDocument(doc2, ImportFormatMode.USE_DESTINATION_STYLES);

In sdtBlock switch case, you are not adding the paragraph in content control. Please add an empty paragraph to content control after remove all children of content control.

If you still face problem, please share following detail for investigation purposes.

  • Please create a standalone/runnable simple Java application that demonstrates the code (Aspose.Words code) you used to generate your output document

  • Please attach the output Word file that shows the undesired behavior.

  • Please attach your target Word document showing the desired behavior. You can
    use Microsoft Word to create your target Word document. I will investigate as to how you are expecting your final document be generated like.

As soon as you get these pieces of information to
us we’ll start our investigation into your issue.

Hi Tahir,

thank you for your reply.

So, please use following code snippet for all content controls.

This is not possible since you cannot add a paragraph to a inline level content control.

In sdtBlock switch case, you are not adding the paragraph in content control.

Thank you for pointing this out. I have corrected the sample code above. The problem persists since I made this error when writing this post, not when writing my test.

As to your requested details:

Please create a standalone/runnable simple Java application that demonstrates

The code provided should be completely standalone, you only have to insert the references to the documents for your system.

  • Please attach the output Word file that shows the undesired behavior.
  • Please attach your target Word document showing the desired behavior.

See attached outInsert.docx in original posting. it shows both the undesired and the desired result. I have revised all attached documents to further clarify my problem.

Best regards,
Alexander

Hi Alexander,

Thanks for sharing the detail. Please note that Aspose.Words mimics the same behavior as MS Word does. If you copy the contents of ParagraphWithFormatting.docx into content controls using MS Word, you will get the same output.

DocumentBuilder.InsertDocument method inserts content of the document into the current position of DocumentBuilder’s cursor.

This method mimics the MS Word behavior, as if CTRL+‘A’ (select all content) was pressed, then CTRL+‘C’ (copy selected into the buffer) inside one document and then CTRL+‘V’ (insert content from the buffer) inside another document.

Hi Tahir,

thank you for your reply.

However, it is not correct. If you paste the content into an inline content control, the result differs between Aspose and Word.

Output of Aspose:
The following inline level SDT will show the undesired data

<w:sdt docpart="83E6CDEE0FBF4CE4AA5460CF12908633" title="sdtInline" sdttag="sdtInline" id="1380046053"> 
This Paragraph is formatted as Heading 1 Mit Fußnote. However Mit Endnote, several runs have different formatting.
</w:sdt>

Output of Word:
This is the expected result for inline level SDT:

<w:sdt showingplchdr="t" docpart="851865987ABD4715A5143C3B3FB77D7E" title="sdtExpected" sdttag="sdtExpected" id="-1379087463">
This Paragraph is formatted as Heading 1 [1][^1]. **However [i][^2]**, several runs have different formatting.
</w:sdt>

[1] Mit Fußnote

[i] Mit Endnote

[^1]:

[^2]:

Hi Alexander,

Thanks
for sharing the detail. I have tested the scenario and have managed to reproduce the same issue at my side. For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-12528. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

The issues you have found earlier (filed as WORDSNET-12528) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.