Table of Contents

Move whole blocks

A heading with everything under it, a clause of a contract, a row of sections in a report: MoveBlocks takes a run of consecutive blocks and puts it somewhere else among the blocks of its container. The paragraphs and tables go as they are, in one DocumentEdit that undo takes back exactly.

The code

using DocWright.Dom;
using DocWright.Dom.Editing;

using var document = new WordDocument();
document.AppendParagraph("Introduction");
Paragraph about = document.AppendParagraph("What the report is about.");
Paragraph results = document.AppendParagraph("Results");
Paragraph numbers = document.AppendParagraph("The numbers, in a table:");
Paragraph method = document.AppendParagraph("Method");
document.AppendParagraph("How they were measured.");
document.InsertTable(DocumentPosition.EndOf(numbers), 2, 2);

Section body = document.Sections[0];
string Order() => string.Join(" | ", body.Blocks.Select(block => block is Paragraph paragraph ? "\"" + paragraph.GetText().TrimEnd('\n') + "\"" : "[table]"));
Console.WriteLine("Before:       " + Order());

// "Method" and the paragraph under it, two blocks, go to before "Results".
long id = method.NodeId.Value;
DocumentEdit first = document.MoveBlocks(method, 2, results);
Console.WriteLine("Method first: " + Order());
Console.WriteLine($"The same node: {method.NodeId.Value == id}; the caret goes to \"{first.After.Paragraph.GetText().TrimEnd('\n')}\"");

// Undo puts the blocks back.
first.Undo();
Console.WriteLine("Undone:       " + Order());

// "Results", its paragraph and its table, three blocks, go to the end. The document would end in a table, so
// an empty paragraph is added after it, as Word keeps one there.
document.MoveBlocks(results, 3, null);
Console.WriteLine("Results last: " + Order());

// Two tables are never brought together (Word reads two tables with nothing between them as one).
document.InsertTable(DocumentPosition.EndOf(about), 1, 1);
Console.WriteLine("A table more: " + Order());
string refused = "not refused";
try
{
    // The blocks between the two tables, taken away to the start.
    Table one = body.Blocks.OfType<Table>().First();
    Table two = body.Blocks.OfType<Table>().Last();
    int from = body.Blocks.IndexOf(one) + 1;
    document.MoveBlocks(body.Blocks[from], body.Blocks.IndexOf(two) - from, body.Blocks[0]);
}
catch (InvalidOperationException e)
{
    refused = e.Message;
}

Console.WriteLine("Refused:      " + refused);

Output

Before:       "Introduction" | "What the report is about." | "Results" | "The numbers, in a table:" | [table] | "" | "Method" | "How they were measured."
Method first: "Introduction" | "What the report is about." | "Method" | "How they were measured." | "Results" | "The numbers, in a table:" | [table] | ""
The same node: True; the caret goes to "Method"
Undone:       "Introduction" | "What the report is about." | "Results" | "The numbers, in a table:" | [table] | "" | "Method" | "How they were measured."
Results last: "Introduction" | "What the report is about." | "" | "Method" | "How they were measured." | "Results" | "The numbers, in a table:" | [table] | ""
A table more: "Introduction" | "What the report is about." | [table] | "" | "" | "Method" | "How they were measured." | "Results" | "The numbers, in a table:" | [table] | ""
Refused:      The move would put two tables next to each other, which Word joins into one.

What moves, and where

MoveBlocks(first, count, before) moves count blocks, starting at first, to before the block before. Pass null to move them to the container's end. The run and its destination are blocks of one container: a section, a table cell, a header or a footer, a note.

The blocks are the same nodes afterwards. Their node ids, formatting, tables, pictures, footnote references and fields go with them, and a position you hold in one of them is still a position in it. That is the difference from copy and paste: a fragment puts copies in and the originals are deleted, and a span that covers a table cannot be made into a fragment at all.

Finding the run is yours to do. For a heading and what is under it, walk the blocks after the heading until a paragraph whose outline level is the heading's or above.

Two rules Word keeps

  • A table is followed by a paragraph. When a move would leave the container ending in a table, MoveBlocks adds an empty paragraph after it, as part of the same edit, and undo removes it with the move.
  • Two tables with nothing between them are one table. Word joins them when it opens the file. A move that would put two tables next to each other is refused with InvalidOperationException, and nothing changes. Move the paragraph between them along with the run, or put one there first.

What to know

  • A move to where the run already stands is an empty edit (DocumentEdit.IsEmpty).
  • DocumentEdit.After is the start of the run's first paragraph, where it now is.
  • ChangedParagraphKeys names the run's paragraphs and the ones that got another neighbour, for LayoutSession.Relayout.
  • A count under one or past the container's end is refused with ArgumentOutOfRangeException; a destination in another container or inside the run with ArgumentException.
  • To delete a run instead, delete its tables with DeleteTable and its text with DeleteRange: they take care of the notes, fields and bookmarks that go with it.