Additional empty paragraph after document content insert

Hi there,

in my customer’s solution, I have the following situation

  • The program creates a new word document, which consists of specific placeholders.
  • Placeholders are found and replaced with the empty text in the first step (LoadText("")). It simulates a “cut out” of the placeholder.
  • After that, the document content of another doc file is inserted at this new position using the ContentPosition.InsertRange() method.

Problem:
The content of the second doc does not contain an empty paragraph at the beginning, but anyhow after the content insertion, there is a new empty paragraph above the inserted doc file content.

Is this a known problem or bug maybe?
Atm I try to find a workaround to remove that additional new empty paragraph above afterward, by using RemoveAt() method of the sections block collection. But it makes lots of follow up problems… and yes, it’s a dirty workaround.

The result in my generated new document is that I have 1 paragraph mark too much, which shouldn’t be there.

Thanks in advance for your feedback.

Hi Nina,

I’m afraid that without debugging this it’s hard to say.
Nevertheless, perhaps the problem is with the place where the content is being imported, maybe an existing Paragraph element is being split into two so that the new ContentRange can be placed inside of it.

Anyway, please consider creating a small Visual Studio project that demonstrates your use case and reproduces the mentioned issue so that we can investigate it.
You can submit the support ticket and attach your project to it.

Regards,
Mario

Ok, thanks. One other question.

I described using the ContentRange.LoadText("") method to replace the placeholder with empty text in the first step. If I would use ContentRange.Delete() instead, what is the best practice to get the new ContentPosition?

ContentRange.Delete() returns nothing and I need to know what is the valid ContentPosition after deletion of my range.

Again, thanks in advance!

Hi Nina,

The ContentRange.Delete() doesn’t add or import any new content so there is no new ContentRange nor ContentPosition. In other words, you should reference and use some position that will remain in the document.

This would depend on where exactly is your placeholder located. But as an example, you could perhaps use the start or end position of the parent Paragraph (e.g. ContentRange.Start.Parent.Parent.Content.Start).

However, I don’t think that changing from ContentRange.LoadText("") to ContentRange.Delete() will make any difference.

Instead, if you have a placeholder that is the only content of some Paragraph, then you could select that text together with the paragraph’s mark (¶) and thus include that Paragraph element in the found ContentRange. After that, you could use ContentRange.Set method to replace the current content with other content.

For example, something like this:

var document = DocumentModel.Load("input.docx");
var anotherDocument = DocumentModel.Load("another-input.docx");

// Notice the '\n' at the end of the searched text.
var content = document.Content.Find("MyPlaceholderText\n").First();
content.Set(anotherDocument.Sections[0].Blocks.Content);

document.Save("output.docx");

I hope this helps.

Regards,
Mario