Inserting paragraphs into content controls in Text frames breaks the document

Environment: Aspose: 16.7.0

The following code causes very unexpected behavior.
See attached files for details.

We are not using text boxes since we require some of the functionality only available in text frames.
The problem also occurs if I try to insert the paragraph after the sdt.
The only thing that “works” is cloning the sdt and trying to reuse/manipulate the paragraph inside.

@Test
public void testPositionsrahmen() throws Exception {

    Document document = new Document("d:\\Positionsrahmen.docx");

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

    Paragraph paragraph = new Paragraph(document);
    paragraph.appendChild(new Run(document, "new Content"));
    sdt.appendChild(paragraph);

    String fileName = "D:\\out.docx";
    document.save(fileName);

    Desktop.getDesktop().open(new File(fileName));
}

Hi Alexander,

Thanks for your inquiry. We have tested the scenario and have managed to reproduce the same issue at our side. For the sake of correction, we have logged this problem in our issue tracking system as WORDSNET-14142. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Hi Alexander,

It is to inform you that the issue which you are facing is actually not a bug in Aspose.Words. So, we have closed this issue (WORDSNET-14142) as ‘Not a Bug’.

MS Word inserts new paragraph into SDT with the same attributes as in already existing paragraph. The existing paragraph at the input document has frame and style. This affects how result document is looking. Please insert new paragraph without any attributes set.

Please use following code example to get the desired output.

Document document = new Document(MyDir + "Positionsrahmen.docx");

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

Paragraph template = (Paragraph)sdt.getFirstChild();
Paragraph paragraph = (Paragraph)template.deepClone(false);

paragraph.appendChild(new Run(document, "new Content"));
sdt.appendChild(paragraph);

document.save(MyDir + "17.2.0.docx");

Desktop.getDesktop().open(new File(MyDir + "17.2.0.docx"));

Hi Tahir,

thank you for this information.

However, we are trying to import a paragraph from another document. How can we achieve the same behavior without losing paragraph formatting?

Node insertAfterNode = sdt;
CompositeNode dstStory = insertAfterNode.getParentNode();

NodeImporter importer = new NodeImporter(srcDoc, insertAfterNode.getDocument(), ImportFormatMode.USE_DESTINATION_STYLES);

Node newNode = null;

for (Section srcSection : srcDoc.getSections())
{
    // Loop through all block level nodes (paragraphs and tables) in the body of the section.
    for (Node srcNode : srcSection.getBody())
    {
        // Let’s skip the node if it is a last empty paragraph in a section.
        if (srcNode.getNodeType() == NodeType.PARAGRAPH)
        {
            Paragraph para = (Paragraph)srcNode;
            if (para.isEndOfSection() && !para.hasChildNodes())
            {
                continue;
            }
        }

        // This creates a clone of the node, suitable for insertion into the destination document.
        newNode = importer.importNode(srcNode, true);

        // Insert new node after the reference node.
        dstStory.insertAfter(newNode, insertAfterNode);
        insertAfterNode = newNode;
    }
}

Hi Alexander,

Thanks for your inquiry. You need to use the same approach to insert the paragraph into content control. Following code example imports the first paragraph of source document into destination document in the content control. Hope this helps you.

Document document = new Document(MyDir + "Positionsrahmen.docx");

StructuredDocumentTag sdt = (StructuredDocumentTag)document.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
Paragraph template = (Paragraph)sdt.getFirstChild();

Document input = new Document(MyDir + "input.docx");
Paragraph para = (Paragraph)input.getChild(NodeType.PARAGRAPH, 0, true);
NodeImporter importer = new NodeImporter(input, document, ImportFormatMode.USE_DESTINATION_STYLES);
Paragraph newPara = (Paragraph)importer.importNode(para, true);

Paragraph paragraph = (Paragraph)template.deepClone(false);
for (Node node : (Iterable)newPara.getChildNodes(NodeType.ANY, true))
{
    paragraph.appendChild(node);
}

sdt.appendChild(paragraph);

document.save(MyDir + "17.3.0.docx");

Hi Tahir,

but what about actual paragraph formatting? It won’t be copied.

e.g.:

Paragraph p;
p.getListFormat();
p.getParagraphBreakFont();
p.getParagraphFormat();

Hi Alexander,

Thanks for your inquiry. Could you please share your input and expected output documents here for our reference? We will then provide you more information about your query along with code.