I’m getting an error java.lang.IllegalArgumentException: Cannot insert a node of this type at this location. when trying to create and insert a StructuredDocumentTag using the DocumentBuilder.
I have used the following sample as a guide for the syntax.
https://docs.aspose.com/words/net/working-with-content-control-sdt/
Can you please give an example of how to correctly add the SDT using DocumentBuilder?
I’m currently using Aspose.Words.for.Java ver 14.7.0.
Sample code:
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
StructuredDocumentTag pref = new StructuredDocumentTag(document, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
Paragraph para = new Paragraph(document);
Run run = new Run(document);
run.setText("abc");
para.getRuns().add(run);
pref.getChildNodes().add(para);
pref.setTag("Pinpoint_Control");
pref.setLockContents(true);
// Insert content control into the document
builder.insertNode(pref); // getting error here
Hi Linda,
Thanks for your inquiry. StructuredDocumentTag class represents a structured document tag (SDT or content control) in a document.
Structured document tags (SDTs) allow to embed customer-defined semantics as well as its behavior and appearance into a document.
In this version of Aspose.Words provides a number of public methods and properties to manipulate the behavior and content of StructuredDocumentTag. However databinding of SDT nodes to custom xml packages within a Document is not supported in this version of public API.
StructuredDocumentTag can occur in a document in the following places:
- Block-level - Among paragraphs and tables, as a child of a Body, HeaderFooter, Comment, Footnote or a Shape node.
- Row-level - Among rows in a table, as a child of a Table node.
- Cell-level - Among cells in a table row, as a child of a Row node.
- Inline-level - Among inline content inside, as a child of a Paragraph.
- Nested inside another StructuredDocumentTag.
Please use following code example to achieve your requirements and check code examples shared in following documentation link.
https://docs.aspose.com/words/java/working-with-content-control-sdt/
Document document = new Document();
DocumentBuilder builder = new DocumentBuilder(document);
StructuredDocumentTag pref = new StructuredDocumentTag(document, SdtType.PLAIN_TEXT, MarkupLevel.INLINE);
Run run = new Run(document);
run.setText("abc");
pref.getChildNodes().add(run);
pref.setTag("Pinpoint_Control");
pref.setLockContents(true);
builder.insertNode(pref);
document.save(MyDir + "Out.docx");
Please read following documentation link for your kind reference.
https://docs.aspose.com/words/java/aspose-words-document-object-model/
I tried the example given, which does work if we are creating an empty Document to begin with.
In our workflow, we are creating a document using a template, which I have attached a copy for your testing. When using the template, this is now throwing a NullPointerException which occurs when trying to construct the StructuredDocumentTag. This is when I specify the StdType.PLAIN_TEXT.
Also, I think the SdtType we want to use are BLOCK, not INLINE, which if I switch the type to SdtType.BLOCK it still throws the error “cannot create a node of this type at this location”.
Please advise on how to output a BLOCK level SDT while using DocumentBuilder that contains only plain text.
Thanks.
Updated sample coded:
Document document = new Document("GentTemplate.dotm");
DocumentBuilder builder = new DocumentBuilder(document);
StructuredDocumentTag pref = new StructuredDocumentTag(document, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
Run run = new Run(document);
run.setText("abc");
pref.getChildNodes().add(run);
pref.setTag("Pinpoint_Control");
pref.setLockContents(true);
builder.insertNode(pref);
document.save(MyDir + "Out.docx");
Hi Linda,
Thanks for your inquiry.
*treuters:
When using the template, this is now throwing a NullPointerException which occurs when trying to construct the StructuredDocumentTag. This is when I specify the StdType.PLAIN_TEXT.*
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-11759. 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.
*treuters:
Please advise on how to output a BLOCK level SDT while using DocumentBuilder that contains only plain text.*
Please note that Block-level content control must be among paragraphs and tables, as a child of a Body, HeaderFooter, Comment, Footnote or a Shape node.
Please use following code example to insert Block-level content control at the end of first section of document.
Document document = new Document(MyDir + "GentTemplate.dotm");
StructuredDocumentTag pref = new StructuredDocumentTag(document, SdtType.PLAIN_TEXT, MarkupLevel.BLOCK);
Run run = new Run(document);
run.setText("abc");
Paragraph para = new Paragraph(document);
para.appendChild(run);
pref.getChildNodes().add(para);
pref.setTag("Pinpoint_Control");
pref.setLockContents(true);
document.getFirstSection().getBody().appendChild(pref);
document.save(MyDir + "Out.docx");
The issues you have found earlier (filed as WORDSNET-11759) have been fixed in this .NET update and this Java update.
This message was posted using Notification2Forum from Downloads module by aspose.notifier.