Use DocumentBuilder to move the cursor to the specified position within C# and .NET


   To move the cursor in a document, use the DocumentBuilderMoveTo method. Below are the most commonly used methods of moving.

   To move to the beginning of the document, call DocumentBuilderMoveToDocumentStart method. If you need to move to the end of the document, call DocumentBuilderMoveToDocumentEnd.
   To move data to the Header or Footer use DocumentBuilderMoveToHeaderFooter method.
   Use DocumentBuilderMoveToParagraph method to move the cursor to a desired paragraph in the current section.
   Use DocumentBuilderMoveToCell method to move the cursor to a table cell in the current section.
   To move to a Bookmark, use DocumentBuilderMoveToBookmark method. isStart when true, moves the cursor to the beginning of the bookmark. When false, moves the cursor to the end of the bookmark. isAfter when true, moves the cursor to be after the bookmark start or end position. When false, moves the cursor to be before the bookmark start or end position.

Complete code

using System;
using SautinSoft.Document;
using System.Text;
using SautinSoft.Document.Drawing;
using SautinSoft.Document.Tables;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/
            MovingCursor();
        }
        /// <summary>
        /// Moving the current cursor position in the document using DocumentBuilder.
        /// </summary>
        /// <remarks>
        /// Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-moving-cursor.php
        /// </remarks>

        static void MovingCursor()
        {
            DocumentCore dc = new DocumentCore();
            DocumentBuilder db = new DocumentBuilder(dc);

            db.MoveToHeaderFooter(HeaderFooterType.HeaderDefault);
            db.Writeln("Moved the cursor to the header and inserted this text.");

            db.MoveToDocumentStart();
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Blue;
            db.Writeln("Moved the cursor to the start of the document.");

            // Marks the current position in the document as a 1st bookmark start.
            db.StartBookmark("Firstbookmark");
            db.CharacterFormat.Italic = true;
            db.CharacterFormat.Size = 14;
            db.CharacterFormat.FontColor = Color.Red;
            db.Writeln("The text inside the 'Bookmark' is inserted by the DocumentBuilder.Writeln method.");
            // Marks the current position in the document as a 1st bookmark end.
            db.EndBookmark("Firstbookmark");

            db.MoveToBookmark("Firstbookmark", true, false);
            db.Writeln("Moved the cursor to the start of the Bookmark.");

            db.CharacterFormat.FontColor = Color.Black;
            Field f1 = db.InsertField("DATE");
            db.MoveToField(f1, false);
            db.Write("Before the field");

            // Moving to the Header and insert the table with three cells.
            db.MoveToHeaderFooter(HeaderFooterType.HeaderDefault);
            db.StartTable();
            db.TableFormat.PreferredWidth = new TableWidth(LengthUnitConverter.Convert(6, LengthUnit.Inch, LengthUnit.Point), TableWidthUnit.Point);
            db.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Green, 1);
            db.RowFormat.Height = new TableRowHeight(40, HeightRule.Exact);
            db.CharacterFormat.FontColor = Color.Green;
            db.CharacterFormat.Italic = false;
            db.InsertCell();
            db.Write("This is Row 1 Cell 1");
            db.InsertCell();
            db.Write("This is Row 1 Cell 2");
            db.InsertCell();
            db.Write("This is Row 1 Cell 3");
            db.EndTable();

            // Insert the text in the second cell in the sixth position.
            db.MoveToCell(0, 0, 1, 5);
            db.CharacterFormat.Size = 18;
            db.CharacterFormat.FontColor = Color.Orange;
            db.Write("InsertToCell");

            db.MoveToDocumentEnd();
            db.CharacterFormat.Size = 16;
            db.CharacterFormat.FontColor = Color.Blue;
            db.Writeln("Moved the cursor to the end of the document.");

            // Save our document into DOCX format.
            string resultPath = @"Result.docx";
            dc.Save(resultPath, new DocxSaveOptions());

            // Open the result for demonstration purposes.
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(resultPath) { UseShellExecute = true });
        }
    }
}

Download

Imports System
Imports SautinSoft.Document
Imports System.Text
Imports SautinSoft.Document.Drawing
Imports SautinSoft.Document.Tables

Namespace Example
	Friend Class Program
		Shared Sub Main(ByVal args() As String)
			MovingCursor()
		End Sub
                ''' Get your free 30-day key here:   
                ''' https://sautinsoft.com/start-for-free/
		''' <summary>
		''' Moving the current cursor position in the document using DocumentBuilder.
		''' </summary>
		''' <remarks>
		''' Details: https://www.sautinsoft.com/products/document/help/net/developer-guide/documentbuilder-moving-cursor.php
		''' </remarks>

		Private Shared Sub MovingCursor()
			Dim dc As New DocumentCore()
			Dim db As New DocumentBuilder(dc)

			db.MoveToHeaderFooter(HeaderFooterType.HeaderDefault)
			db.Writeln("Moved the cursor to the header and inserted this text.")

			db.MoveToDocumentStart()
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Blue
			db.Writeln("Moved the cursor to the start of the document.")

			' Marks the current position in the document as a 1st bookmark start.
			db.StartBookmark("Firstbookmark")
			db.CharacterFormat.Italic = True
			db.CharacterFormat.Size = 14
			db.CharacterFormat.FontColor = Color.Red
			db.Writeln("The text inside the 'Bookmark' is inserted by the DocumentBuilder.Writeln method.")
			' Marks the current position in the document as a 1st bookmark end.
			db.EndBookmark("Firstbookmark")

			db.MoveToBookmark("Firstbookmark", True, False)
			db.Writeln("Moved the cursor to the start of the Bookmark.")

			db.CharacterFormat.FontColor = Color.Black
			Dim f1 As Field = db.InsertField("DATE")
			db.MoveToField(f1, False)
			db.Write("Before the field")

			' Moving to the Header and insert the table with three cells.
			db.MoveToHeaderFooter(HeaderFooterType.HeaderDefault)
			db.StartTable()
			db.TableFormat.PreferredWidth = New TableWidth(LengthUnitConverter.Convert(6, LengthUnit.Inch, LengthUnit.Point), TableWidthUnit.Point)
			db.CellFormat.Borders.SetBorders(MultipleBorderTypes.Outside, BorderStyle.Single, Color.Green, 1)
			db.RowFormat.Height = New TableRowHeight(40, HeightRule.Exact)
			db.CharacterFormat.FontColor = Color.Green
			db.CharacterFormat.Italic = False
			db.InsertCell()
			db.Write("This is Row 1 Cell 1")
			db.InsertCell()
			db.Write("This is Row 1 Cell 2")
			db.InsertCell()
			db.Write("This is Row 1 Cell 3")
			db.EndTable()

			' Insert the text in the second cell in the sixth position.
			db.MoveToCell(0, 0, 1, 5)
			db.CharacterFormat.Size = 18
			db.CharacterFormat.FontColor = Color.Orange
			db.Write("InsertToCell")

			db.MoveToDocumentEnd()
			db.CharacterFormat.Size = 16
			db.CharacterFormat.FontColor = Color.Blue
			db.Writeln("Moved the cursor to the end of the document.")

			' Save our document into DOCX format.
			Dim resultPath As String = "result.docx"
			dc.Save(resultPath, New DocxSaveOptions())

			' Open the result for demonstration purposes.
			System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(resultPath) With {.UseShellExecute = True})
		End Sub
	End Class
End Namespace

Download


If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below:



Questions and suggestions from you are always welcome!

We are developing .Net components since 2002. We know PDF, DOCX, RTF, HTML, XLSX and Images formats. If you need any assistance with creating, modifying or converting documents in various formats, we can help you. We will write any code example for you absolutely free.