RangeCopyMove.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This sample shows how to copy or move ranges within a document,
    // or between documents, using the RangeBase.CopyTo()/MoveTo() methods.
    //
    // The original SampleParagraphs.docx used in this sample can be
    // seen by running the SampleParagraphs sample.
    public class RangeCopyMove
    {
        public GcWordDocument CreateDocx()
        {
            const string p1start = "This is the first paragraph of the original document";
            const string p2start = "This is the second paragraph of the original document";
            const string p3start = "This is the third paragraph of the original document";
            const string p4start = "This is the fourth paragraph of the original document";

            // Load the sample DOCX file (see SampleParagraphs) into a GcWordDocument instance:
            var srcDoc = new GcWordDocument();
            srcDoc.Load(Path.Combine("Resources", "WordDocs", "SampleParagraphs.docx"));

            // Get the range of paragraphs to copy:
            var srcRng = srcDoc.Body.GetPersistentRange(srcDoc.Body.Paragraphs[2], srcDoc.Body.Paragraphs.Last);

            // We use another document here to demonstrate that CopyTo()/MoveTo() methods
            // can be used to copy/move content between documents:
            var doc = new GcWordDocument();
            srcRng.CopyTo(doc.Body, InsertLocation.Start);

            // Find individual paragraphs inside the new document to manipulate:
            Paragraph p1 = null, p2 = null, p3 = null, p4 = null;
            foreach (var p in doc.Body.Paragraphs)
            {
                var t = p.GetRange().Text;
                if (t.StartsWith(p1start))
                    p1 = p;
                else if (t.StartsWith(p2start))
                    p2 = p;
                else if (t.StartsWith(p3start))
                    p3 = p;
                else if (t.StartsWith(p4start))
                    p4 = p;
            }
            if (p1 == null || p2 == null || p3 == null || p4 == null)
                throw new Exception("Unexpected: could not find paragraphs.");

            // Move first paragraph to the end of the document with default formatting option
            // (preserve formatting):
            p1.GetRange().MoveTo(doc.Body, InsertLocation.End);

            // Move second paragraph to the end of the document, clearing formatting:
            p2.GetRange().MoveTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear);

            // Copy third paragraph to the end of the document, clearing formatting:
            p3.GetRange().CopyTo(doc.Body, InsertLocation.End, FormattingCopyStrategy.Clear);

            // Add a note at the end of the document:
            doc.Body.Sections.Last.GetRange().Paragraphs.Add($"Created by DsWord on {Util.TimeNow():R}.");

            // Done:
            return doc;
        }
    }
}