Hi Aspose Words Team,
I have a problem to use Aspose.Words.Markup.StructuredDocumentTag
I want to use this feature, to insert some text from another document and protect it
from being modified by users.
I didn’t find any sample code to do that so I tryed several things like :
- using SDT.PlaceHolder.FirstSection.Body and insert nodes inside
it doesn’t work, when I open the produced doc with word the StructuredDocumentTag
is present but with “Click here to enter text” default message
If I unzip the docx I found my text inside subfolder glossary ???!
I tryed to compare with a SDT created directly from word
and when I access it with aspose.words library
The text is correct, exemple with SDT.GetText()
When I try to replace text in the word inserted SDT
I doesn’t work (exemple : Range.Replace(…))
Please Help … I’m getting mad with this
I think it’s exactly the feature I need
regards
Hi,
Thanks for your query. Please use following code snippet to insert text into StructuredDocumentTag and protect document. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document(MyDir + "SDTs.docx");
foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true, true))
{
if (sdt.SdtType == SdtType.PlainText)
{
sdt.RemoveAllChildren();
Paragraph para = sdt.AppendChild(new Paragraph(doc)) as Paragraph;
Run run = new Run(doc, "new text goes here");
para.AppendChild(run);
}
else if (sdt.SdtType == SdtType.DropDownList)
{
SdtListItem secondItem = sdt.ListItems[2];
sdt.ListItems.SelectedValue = secondItem;
}
}
doc.Protect(ProtectionType.ReadOnly, "password");
doc.Save(MyDir + "AsposeOut.Docx");
Sorry but I get this error message :
Cannot insert a node of this type at this location.
à Aspose.Words.CompositeNode.xef23aa45e7612fdd(Node x40e458b3a58f5782, Node x
ff5adbb92d63bf52, Boolean x7f43f79506f73a73)
à Aspose.Words.CompositeNode.InsertAfter(Node newChild, Node refChild)
à Aspose.Words.CompositeNode.AppendChild(Node newChild)
My SdtType is RichText but I tryed with your code in PlainText : same result
regards
Hi,
It would be great if you please share your document for investigation purposes.
I create a new document like this (exception raise on red line) :
Document doc = new Document(); Aspose.Words.Markup.StructuredDocumentTag sdt = new Aspose.Words.Markup.StructuredDocumentTag(doc, Aspose.Words.Markup.SdtType.RichText, Aspose.Words.Markup.MarkupLevel.Inline);
sdt.RemoveAllChildren();
Paragraph para = sdt.AppendChild(new Paragraph(nvDoc)) as Paragraph;
Run run = new Run(nvDoc, "new text goes here");
para.AppendChild(run);
doc.Save(@"c:\test\test.docx");
Hi,
Please use the following code snippet for your requirement. Hope this helps you. Please let us know if you have any more queries.
Document doc = new Document();
DocumentBuilder db = new DocumentBuilder(doc);
db.Write("AAA");
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
db.InsertNode(sdt1);
sdt1.Tag = "sdt1";
sdt1.RemoveAllChildren();
Run r1 = new Run(doc);
r1.Text = "Run1";
sdt1.AppendChild(r1);
db.Write("BBB");
StructuredDocumentTag sdt2 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Inline);
db.InsertNode(sdt2);
sdt2.Tag = "sdt2";
sdt2.RemoveAllChildren();
Run r2 = new Run(doc);
r2.Text = "Run2";
sdt2.AppendChild(r2);
db.Write("CCC");
doc.Save(MyDir + "sdt.docx", Aspose.Words.SaveFormat.Docx);
Thanks for your reply,
it works for simple text insert, but do you plan to full support the word feature of sdt in next update ?
I mean the ability to insert all node types like tables, graphics etc.
regards
Hi,
Please use the following code snippet to add block level nodes. Please read following documentation links for your kind reference. Please let us know if you have any more queries.
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell1");
builder.InsertCell();
builder.Write("Cell2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell3");
builder.InsertCell();
builder.Write("Cell4");
builder.EndRow();
builder.EndTable();
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(sdt1);
sdt1.Tag = "sdt1";
sdt1.RemoveAllChildren();
sdt1.AppendChild(table);
doc.Save(MyDir + "AsposeOut.docx");
Thanks, for the sample code,
that’s the solution, and it works fine,
I can change existing SDT, and add richtext SDT to a document…
but, your sample works only to append at the end of the body
I tryed to insert the SDT at a specified position
in body but not at the end (between paragraphs)
or inside a cell of a table
but I always get an error : you cannot insert this type of node… or The reference node is not a child of this node.
I search inside all your doc, and I didn’t find any sample code to do that
and links provided up, give no more information how to do that (in remarks it seem’s that there’s some missing words ???)
I promise this is my last question
best regards
Hi,
You can use the insertAfter/insertBefore method for your requirement. Please see the code below. Please let us know if you have any more queries.
Document doc = new Document(MyDir + “in.docx”);
DocumentBuilder builder = new DocumentBuilder(doc);
Table table = builder.StartTable();
builder.InsertCell();
builder.Write("Cell1");
builder.InsertCell();
builder.Write("Cell2");
builder.EndRow();
builder.InsertCell();
builder.Write("Cell3");
builder.InsertCell();
builder.Write("Cell4");
builder.EndRow();
builder.EndTable();
StructuredDocumentTag sdt1 = new StructuredDocumentTag(doc, SdtType.RichText, MarkupLevel.Block);
doc.FirstSection.Body.InsertAfter(sdt1, doc.FirstSection.Body.FirstParagraph);
//builder.InsertNode(sdt1);
sdt1.Tag = "sdt1";
sdt1.RemoveAllChildren();
sdt1.AppendChild(table);
doc.Save(MyDir + "AsposeOut.docx");
That’s perfect, it works fine
thanks a lot for your help