Free Support Forum - aspose.com

Adding a run to a bookmark ends up behind bookmark?!

Hello,

short and probably easy question. I have a word document containing a bookmark, which in turns contains some text. I want to remove the text and add a heading and a run to the bookmark by doing the following:

Bookmark bm = Document.Range.Bookmarks["REQSPEC_UseCases_REQSPEC"];

bm.Text = "";

bool bMove = builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", true, true);

AddHeading("Some Heading", 2);

_docBuilder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

_docBuilder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

var run = new Run(_document.Document, "Some Text");

_docBuilder.InsertParagraph().Runs.Add(run);

The result is: The heading is contained in the bookmark, the run is placed AFTER the bookmark (outside of it). I cannot figure out why. Any tipps?

Thanks in advance,

Marcel

Hi Marcel,

Thanks for your inquiry. To ensure a timely and accurate response please supply us with the following information.

  • What version of Aspose.Words for .NET are you using?
  • Please supply us with the input document that is causing the issue
  • Please supply us with the output document showing the undesired behavior
  • Please supply us with the document showing the desired behavior (you can create this document using Microsoft Word).

As soon as you get these pieces of information to us we'll start our investigation into your issue.

Many thanks,

We are using Aspose.NET Version 11.3.0.0 according to the reference VS.NET 2010. I attached the three documents. I used the following code for this test:

DocumentBuilder builder = new DocumentBuilder(Document);

Bookmark bm = Document.Range.Bookmarks["REQSPEC_UseCases_REQSPEC"];

bm.Text = "";

builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", true, true);

var run = new Run(Document, "teststring");

builder.InsertParagraph().Runs.Add(run);

Thanks in advance,

Marcel

Hi Marcel,


Thanks for the additional information. Sure, you can achieve this by using the following code snippet:

Document doc = new
Document(@“C:\Temp\Input.docx”);

doc.Range.Bookmarks[0].Text = string.Empty;

DocumentBuilder builder = new DocumentBuilder(doc);

builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", true, false);

builder.Writeln("Before the bookmark");

builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", true, true);

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1;

builder.Writeln("Heading");

builder.ParagraphFormat.StyleIdentifier = StyleIdentifier.Normal;

builder.Writeln("Inside the Bookmark");

builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", false, true);

builder.Writeln("After the bookmark");

doc.Save(@"C:\Temp\out.docx");


I hope, this helps.

Best regards,

Thank you very much! It works! However what is wrong about using runs as I tried?

Hi Marcel,


Thanks for your inquiry. Here is the modified version of your code that you can use to achieve the same thing using Run nodes:

Document doc = new
Document(@“C:\Temp\input.docx”);

DocumentBuilder builder = new DocumentBuilder(doc);

Bookmark bm = doc.Range.Bookmarks["REQSPEC_UseCases_REQSPEC"];

bm.Text = "";

builder.MoveToBookmark("REQSPEC_UseCases_REQSPEC", true, true);

var run = new Run(doc, "teststring");

builder.InsertNode(run);

//builder.InsertParagraph().Runs.Add(run);

doc.Save(@"C:\Temp\out.docx");


Additionally, in your case the problem occurs because the way that the content of Run is inserted causes the cursor to move to the end of a new paragraph and appends the Run after the BookmarkEnd node which is not your expected output. I hope, the above code helps.

Best regards,