I am trying to enclose a content control (text in content control) inside a bookmark. But I am getting exception saying
Revisions: System.ArgumentException: Cannot insert a node of this type at this location.
at Aspose.Words.CompositeNode.(Node , Node , Boolean )
at Aspose.Words.CompositeNode.InsertBefore(Node newChild, Node refChild)
at DocumentComparison.DocumentComparisonUtil.Convert(String PsDocument
Attached two document
SimpleINDoc (2).zip (1.1 MB)
- SimpleINDoc.docx : Causing excpetion for bookmark creation
- SimpleINDocPara.docx : Not causing excpetion
Only difference between these documents is first document contains SDT in the body and the second document contains SDT inside Paragraph.
Below is the code I am using for creating bookmark.
Document LobjDocument = new Document(PsDocument);
string pdfDocument = PsDocument;
pdfDocument = pdfDocument.Replace(".docx", ".pdf");
DocumentBuilder builder = new DocumentBuilder(LobjDocument);
NodeCollection nodes = LobjDocument.GetChildNodes(NodeType.StructuredDocumentTag, true);
int iCount = nodes.Count;
foreach (StructuredDocumentTag sdt in nodes)
{
Debug.WriteLine("Bookmark : " + sdt.Tag);
builder.MoveTo(sdt);
BookmarkStart start = builder.StartBookmark(sdt.Tag);
BookmarkEnd end = builder.EndBookmark(sdt.Tag);
sdt.ParentNode.InsertBefore(start, sdt);
sdt.ParentNode.InsertAfter(end, sdt);
}
PdfSaveOptions options = new PdfSaveOptions();
options.OutlineOptions.DefaultBookmarksOutlineLevel = 9;
LobjDocument.UpdateFields();
LobjDocument.Save(pdfDocument, options);