Bind Custom XML to content controls

Hi there,
Thanks for your inquiry.
I think in your case you will still need to use mergefields, as you want the content controls removed which means the content you replaced within the will be removed as well. Also using Range.Replace can cause problems if you have a two string the same or one a substring of the other.
Please try using the modified code below:

// Try to copy a content control format to a merge field
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.GetChild(NodeType.StructuredDocumentTag, 0, true);
Node insertNode = null;
if (sdt.Level == MarkupLevel.Block)
{
    insertNode = new Paragraph(doc);
    sdt.ParentNode.InsertBefore(insertNode, sdt);
}
else if (sdt.Level == MarkupLevel.Inline)
{
    insertNode = new Run(doc);
    sdt.ParentNode.InsertBefore(insertNode, sdt);
}
else
{
    // Extra code here for converting a Row or Cell sdt to a merge field if needed
}
// Use the MERGEFORMAT switch to ensure formatting is copied over during mail merge.
MergeField mergeField = new MergeField(insertNode, "MyField", "\\* MERGEFORMAT");
mergeField.CopyFormattingToResult(sdt.GetChildNodes(NodeType.Run, true).ToArray());
sdt.Remove();
doc.MailMerge.Execute(new string[] { "MyField" }, new string[] { "Value" });
public void CopyFormattingToResult(Node[] nodeList)
{
    // Remove all existing nodes from the field result 
    Result = String.Empty;
    foreach (Node node in nodeList)
    {
        // Only move inline nodes.
        if (node.ParentNode.NodeType == NodeType.Paragraph || node.NodeType == NodeType.Run)
            mField.End.ParentParagraph.InsertBefore(node.Clone(false), mField.End);
    }
}

Thanks,

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


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

Would you please send me some java sample code for the following features. I m trying to build a java solution and wanted to use aspose word java api

Access, create and modify Custom XML, SmartTags and Structured Document Tags (Content Controls).
Access and modify all document elements using XmlDocument -like classes and methods.

My email id is javaatgilbert@gmail.com

Hi Srinivas,

Thanks for your inquiry. First of all, please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
https://docs.aspose.com/words/java/aspose-words-document-object-model/
https://reference.aspose.com/words/java/com.aspose.words/Node

The Document is a root node of a tree that contains all other nodes of the document. The tree is a Composite design pattern and in many ways similar to XmlDocument.
vallambhatla:
Access and modify all document elements using XmlDocument -like classes and methods.
Please check the two code examples for your kind reference. Please check the code example related to your query from following documentation link.
https://reference.aspose.com/words/java/com.aspose.words/Document

You may read the details of Aspose.Words for Java from here:
https://docs.aspose.com/words/java/

It would be great if you please explain your query related to specific issue. We will share the code according to your requirements.

Document doc = new Document(getMyDir() + "Document.doc");
// Let's pick a node that we can't be quite sure of what type it is.
// In this case lets pick the first node of the first paragraph in the body of the document
Node node = doc.getFirstSection().getBody().getFirstParagraph().getFirstChild();
System.out.println("NodeType of first child: " + Node.nodeTypeToString(node.getNodeType()));
// This time let's pick a node that we know the type of. Create a new paragraph and a table node.
Paragraph para = new Paragraph(doc);
Table table = new Table(doc);
// Access to NodeType for typed nodes will always return their specific NodeType.
// i.e A paragraph node will always return NodeType.Paragraph, a table node will always return NodeType.Table.
System.out.println("NodeType of Paragraph: " + Node.nodeTypeToString(para.getNodeType()));
System.out.println("NodeType of Table: " + Node.nodeTypeToString(table.getNodeType()));

// Create an "empty" document. Note that like in Microsoft Word,
// the empty document has one section, body and one paragraph in it.
Document doc = new Document();
// This truly makes the document empty. No sections (not possible in Microsoft Word).
doc.removeAllChildren();
// Create a new section node.
// Note that the section has not yet been added to the document,
// but we have to specify the parent document.
Section section = new Section(doc);

// Append the section to the document.
doc.appendChild(section);

// Lets set some properties for the section.
section.getPageSetup().setSectionStart(SectionStart.NEW_PAGE);
section.getPageSetup().setPaperSize(PaperSize.LETTER);
// The section that we created is empty, lets populate it. The section needs at least the Body node.
Body body = new Body(doc);
section.appendChild(body);

// The body needs to have at least one paragraph.
// Note that the paragraph has not yet been added to the document,
// but we have to specify the parent document.
// The parent document is needed so the paragraph can correctly work
// with styles and other document-wide information.
Paragraph para = new Paragraph(doc);
body.appendChild(para);

// We can set some formatting for the paragraph
para.getParagraphFormat().setStyleName("Heading 1");
para.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);
// So far we have one empty paragraph in the document.
// The document is valid and can be saved, but lets add some text before saving.
// Create a new run of text and add it to our paragraph.
Run run = new Run(doc);
run.setText("Hello World!");
run.getFont().setColor(Color.RED);
para.appendChild(run);
// As a matter of interest, you can retrieve text of the whole document and
// see that \x000c is automatically appended. \x000c is the end of section character.
System.out.println(doc.getText());
// Save the document.
doc.save(getMyDir() + "Section.CreateFromScratch Out.doc");

vallambhatla:
Access, create and modify Custom XML, SmartTags and Structured Document Tags (Content Controls).

I am working over this query and will update you asap.

Hi Srinivas,

Please check the following code examples to work with SmartTag. It would be great if you please explain your query related to specific issue. We will share the code according to your requirements.

//Create new document
Document doc = new Document();
//Create new smart tag
SmartTag personName = new SmartTag(doc);
personName.setElement("PersonName");
personName.setUri("urn:schemas-microsoft-com:office:smarttags");
SmartTag firstName = new SmartTag(doc);
firstName.setElement("GivenName");
firstName.setUri("urn:schemas:contacts");
Run fname = new Run(doc, "Ben");
firstName.appendChild(fname);
SmartTag lastName = new SmartTag(doc);
lastName.setElement("Sn");
lastName.setUri("urn:schemas:contacts");
Run lname = new Run(doc, "Smith");
lastName.appendChild(lname);
personName.appendChild(firstName);
//Insert white space
personName.appendChild(new Run(doc, " "));
personName.appendChild(lastName);
//Insert smart trag into the document
doc.getFirstSection().getBody().getFirstParagraph().appendChild(personName);
//Save document
doc.save(MyDir + "AsposeOut.docx", SaveFormat.DOCX);
public void RemoveSmartTags(Document doc)
{
    //Get collection of SmartTags from the document
    Node[] nodes = doc.getChildNodes(NodeType.SMART_TAG, true).toArray();
    //Loop while there is SmartTags in the document
    while (nodes.length > 0)
    {
        SmartTag tag = (SmartTag)nodes[0];
        //Get parent node of smartTag.
        //we should move all content from smatrTag to its parent to preserve documents content
        CompositeNode parent = tag.getParentNode();
        //Loop through all nodes inside smartTag and move its convent to parent node
        while (tag.hasChildNodes())
            parent.insertBefore(tag.getFirstChild(), tag);
        //Remove smartTag
        tag.remove();
    }
}

Hi Srinivas,

Please check the following code examples to work with StructuredDocumentTag. It would be great if you please explain your query related to specific issue. We will share the code according to your requirements.

Document doc = new Document(MYDir+ "in.docx");
StructuredDocumentTag sdt = (StructuredDocumentTag)doc.getChild(NodeType.STRUCTURED_DOCUMENT_TAG, 0, true);
if (sdt.getSdtType() == SdtType.DATE)
{
    sdt.setDateDisplayLocale(1065);
    sdt.setDateDisplayFormat("yyyy/MM/dd hh:mm am/pm");
    sdt.setDateStorageFormat(SdtDateStorageFormat.DATE);
    sdt.removeAllChildren();
    Run r1 = new Run(doc);
    r1.setText("17/12/2012 02:02:00");
    r1.getFont().setBidi(true);
    sdt.appendChild(r1);
}
doc.save(MYDir + "AsposeOut-Java.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
Document doc = new Document();
// Create list.
List list = doc.getLists().add(ListTemplate.NUMBER_DEFAULT);
Paragraph para = new Paragraph(doc);
Run run = new Run(doc, "This is the first item");
para.appendChild(run);
para.getListFormat().setList(list);
Paragraph para2 = new Paragraph(doc);
run = new Run(doc, "This is the second item");
para2.appendChild(run);
para2.getListFormat().setList(list);
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RICH_TEXT, MarkupLevel.BLOCK);
doc.getFirstSection().getBody().appendChild(sdt1);
sdt1.setTag("sdt1");
sdt1.removeAllChildren();
sdt1.appendChild(para);
sdt1.appendChild(para2);
doc.save(MyDir + "AsposeOut.docx");