Wrapping the entire Bookmark content in a paragraph

Hi,
I am trying to merge 2 documents. First is the original document and second is the processed document. The second processed document content is wrapped in a bookmark.

Visually something like below -

  1. First Document Content
  2. documentbuilder.MoveToDocumentEnd();
  3. documentbuilder.InsertBreak(BrakType.PageBreak)
  4. documentbuilder.StartBookmark("ProcessedDocumentBookmark")
  5. documentbuilder.InsertDocument(ProcessedDocument)
  6. documentbuilder.EndBookmark("ProcessedDocumentBookmark")

Could you please let me know how i can wrap the ProcessedDocumentBookmark into a Paragraph Node. It should be in order like below -

  1. First Document Content
  2. documentbuilder.MoveToDocumentEnd();
  3. documentbuilder.InsertBreak(BrakType.PageBreak)
  4. Insert Paragraph Start tag
  5. documentbuilder.StartBookmark("ProcessedDocumentBookmark")
  6. documentbuilder.InsertDocument(ProcessedDocument)
  7. documentbuilder.EndBookmark("ProcessedDocumentBookmark")
  8. Insert Paragraph End tag so that 5,6,7 is wrapped entirely in a parent paragraph tag.

Thanks.

@chetan85 I am afraid, it is not possible to wrap content of the inserted document into a paragraph. Paragraphs can contain only inline nodes as children - Run, Shape etc. Please see our documentation to learn more about Document Object Model.
Also, you can use DocumentExplorer demo application to investigate your documents’ internal representation.

Yes I also thought the same. Thanks for confirming Alexey.

Is it possible to put first document content in one section and processed document content in another section? Like shown below - step 1 to 3 in one section and step 4-6 in another. I am looking for this option is because when I convert the final word document to ooxml and send to front end. The front-end developers have js code to alter the final word document and when they try to remove the other (not the one I have inserted) bookmarks in document then my bookmark start range is getting automatically shifted to first document start index.

  1. First Document Content
  2. documentbuilder.MoveToDocumentEnd();
  3. documentbuilder.InsertBreak(BrakType.PageBreak)
  4. documentbuilder.StartBookmark("ProcessedDocumentBookmark")
  5. documentbuilder.InsertDocument(ProcessedDocument)
  6. documentbuilder.EndBookmark("ProcessedDocumentBookmark")

Thanks

@chetan85 Since you are adding source ProcessedDocument at the end of your target document, I would suggest you to use Document.AppendDocument method instead of DocumentBuilder.InsertDocument. In this case section of the appended document will be retained. For example you can use the following code to wrap whole content of the source document into a bookmark and then append it to the target document:

Document target = new Document(@"C:\Temp\target.docx");
Document src = new Document(@"C:\Temp\src.docx");

// Wrap source document into a bookmark.
string bkName = "my_cool_bookmakr";
src.FirstSection.Body.PrependChild(new BookmarkStart(src, bkName));
src.LastSection.Body.AppendChild(new BookmarkEnd(src, bkName));

// Append source document to the target document.
target.AppendDocument(src, ImportFormatMode.KeepSourceFormatting);

target.Save(@"C:\Temp\out.docx");
1 Like

Thanks Alexey.

1 Like