Creating bookmarks in Word document

Hi,

To create Bookmarks in word document and earlier i recieved a code, but unable to understand and run the code.

Earlier Code Recieved

Aspose.Words.Document doc = new Aspose.Words.Document(@“D:\CCDS DOCS\Required Documents/SourceDocument.docx”);
// Aspose.Words.Document doc = new Aspose.Words.Document(MyDir + @“Source+Document.docx”);
// @“D:\Sreedhar\10 September 2014\pdftohtml\Data/codeine ccds.html”
DocumentBuilder builder = new DocumentBuilder(doc);

BookmarkStart bm1 = builder.StartBookmark(“Bookmark1”);
builder.EndBookmark(“Bookmark1”);

BookmarkStart bm2 = builder.StartBookmark(“Bookmark2”);
builder.EndBookmark(“Bookmark2”);

BookmarkStart bm3 = builder.StartBookmark(“Bookmark3”);
builder.EndBookmark(“Bookmark3”);

//Place Bookmark1 start and end nodes at target places in DOM
Run run = doc.FirstSection.Body.Paragraphs[1].Runs[0];
run.ParentNode.InsertAfter(bm1, run); //Error here

run = doc.FirstSection.Body.Paragraphs[5].Runs[0];
run.ParentNode.InsertBefore(bm1.Bookmark.BookmarkEnd, run);

//Place Bookmark2 start and end nodes at target places in DOM
run = doc.FirstSection.Body.Paragraphs[6].Runs[0];
run.ParentNode.InsertBefore(bm2, run);

doc.FirstSection.Body.Paragraphs[13].AppendChild(bm2.Bookmark.BookmarkEnd);

//Place Bookmark3 start and end nodes at target places in DOM
//run = doc.FirstSection.Body.Paragraphs[14].Runs[0];
//run.ParentNode.InsertBefore(bm3, run);

doc.FirstSection.Body.Paragraphs[17].AppendChild(bm3.Bookmark.BookmarkEnd);

doc.Save(@“E:\CCDS Docs\Required Documents/putDocument.docx”);


Sending again source document as an attachment to this mail

Thanks

Naveen

Hi Naveen,


Thanks for your inquiry. I am afraid, there is no generic code and it can vary from document to document. I suggest you please go through the following articles to learn about the DOM of Aspose.Words and moving cursor to a specific location in document to be able to insert Bookmark.

http://www.aspose.com/docs/display/wordsjava/Aspose.Words+Document+Object+Model
http://www.aspose.com/docs/display/wordsjava/Moving+the+Cursor

I hope, this helps.

Secondly, could you please attach your expected Word document here for our reference. You can create expected document using Microsoft Word. We will then provide you code to achieve the same using Aspose.Words.

Best regards,

Hi Hafeez,

Thankyou for your reply.

Iam sending required document as an attachment to this mail.

The code must be dynamic as we can't mention line numbers statically in the code.

Atlest we need code to create bookmark to content(Content may contain text, Tables, Images) before saving that content in the document.


Please check it and update ASAP.




Thanks

Naveen

Hi Naveen,


Thanks for your inquiry. There are a lot of bookmarks in your expected document. I suppose you have inserted those bookmarks using MS Word. To insert a Bookmark in document using MS Word, you need to place cursor at the beginning of Paragraph for example, then select content forward and insert bookmark by going through Insert|Bookmark option. The same steps are required in Aspose.Words to mark content with a Bookmark i.e. place BookmarkStart and BookmarkEnd nodes before and after selected content respectively. So, I am afraid, there is no dynamic way to achieve this. If we can help you with anything else, please feel free to ask.

Best regards,