How to insert a text in the existing document by specific (x,y) coordinates using C# and .NET


This code example shows how to insert formatted text into an existing example.docx document on the second and third pages at the specified coordinates.

Download the resulting file: result.docx

Complete code

using System;
using System.IO;
using System.Linq;
using SautinSoft.Document;
using SautinSoft.Document.Drawing;

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get your free 30-day key here:   
            // https://sautinsoft.com/start-for-free/

            InsertTextByCoordinates();
        }
        /// <summary>
        /// How to insert a text in the existing PDF, DOCX, any document by specific (x,y) coordinates
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-in-the-existing-pdf-docx-document-by-specific-x-y-coordinates-net-csharp-vb.php
        /// </remarks>
        static void InsertTextByCoordinates()
        {
            // Let us say, we want to insert the text "Hello World!" into:
            // the pages 2,3;
            // 50mm - from the left;
            // 30mm - from the top.
            // Also the text must be inserted Behind the existing text.
            string inpFile = @"..\..\..\example.docx";
            string outFile = @"Result.docx";

            // 1. Load an existing document
            DocumentCore dc = DocumentCore.Load(inpFile);

            // 2. Get document pages
            var paginator = dc.GetPaginator(new PaginatorOptions() { UpdateFields = true });
            var pages = paginator.Pages;

            // 3. Check that we at least 3 pages in our document.
            if (pages.Count < 3)
            {
                Console.WriteLine("The document contains less than 3 pages!");
                Console.ReadKey();
                return;
            }
            // 50mm - from the left;            
            // 30mm - from the top.
            float posFromLeft = 50f;
            float posFromTop = 30f;

            // Insert the text "Hello World!" into the page 2.
            Run text1 = new Run(dc, "Hello World!", new CharacterFormat() { Size = 36, FontColor = Color.Red, FontName = "Arial" });
            InsertShape(dc, pages[1], text1, posFromLeft, posFromTop);

            // Insert the text "Hej Världen!" into the page 3.
            Run text2 = new Run(dc, "Hej Världen!", new CharacterFormat() { Size = 36, FontColor = Color.Orange, FontName = "Arial" });
            InsertShape(dc, pages[2], text2, posFromLeft, posFromTop);

            // 4. Save the document back.
            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
        /// <summary>
        /// Inserts a Shape with text into the specific page using coordinates.
        /// </summary>
        /// <param name="dc">The document.</param>
        /// <param name="page">The specific page to insert the Shape.</param>
        /// <param name="text">The formatted text (Run object).</param>
        /// <param name="posFromLeftMm">The distance in mm from left corner of the page</param>
        /// <param name="posFromTopMm">The distance in mm from top corner of the page</param>
        static void InsertShape(DocumentCore dc, DocumentPage page, Run text, float posFromLeftMm, float posFromTopMm)
        {
            HorizontalPosition hp = new HorizontalPosition(posFromLeftMm, LengthUnit.Millimeter, HorizontalPositionAnchor.Page);
            VerticalPosition vp = new VerticalPosition(posFromTopMm, LengthUnit.Millimeter, VerticalPositionAnchor.Page);
            // 100 x 30 mm
            float shapeWidth = 100f;
            float shapeHeight = 30f;
            SautinSoft.Document.Drawing.Size size = new Size(shapeWidth, shapeHeight, LengthUnit.Millimeter);
            Shape shape = new Shape(dc, new FloatingLayout(hp, vp, size));
            shape.Text.Blocks.Add(new Paragraph(dc, text));
            // Set shape Behind the text.
            (shape.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;
            // Remove the shape borders.
            shape.Outline.Fill.SetEmpty();
            // Insert shape into the page.
            page.Content.End.Insert(shape.Content);
        }
    }
}

Download

Imports System
Imports System.IO
Imports System.Linq
Imports SautinSoft.Document
Imports SautinSoft.Document.Drawing

Module Sample
    Sub Main()
        InsertTextByCoordinates()
    End Sub
        ''' Get your free 30-day key here:   
        ''' https://sautinsoft.com/start-for-free/
	''' <summary>
	''' How to insert a text in the existing PDF, DOCX, any document by specific (x,y) coordinates
	''' </summary>
	''' <remarks>
	''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-in-the-existing-pdf-docx-document-by-specific-x-y-coordinates-net-csharp-vb.php
	''' </remarks>
	Sub InsertTextByCoordinates()
		' Let us say, we want to insert the text "Hello World!" into:
		' the pages 2,3;
		' 50mm - from the left;
		' 30mm - from the top.
		' Also the text must be inserted Behind the existing text.
		Dim inpFile As String = "..\..\..\example.docx"
		Dim outFile As String = "Result.docx"

		' 1. Load an existing document
		Dim dc As DocumentCore = DocumentCore.Load(inpFile)

		' 2. Get document pages
		Dim paginator = dc.GetPaginator(New PaginatorOptions() With {.UpdateFields = True})
		Dim pages = paginator.Pages

		' 3. Check that we at least 3 pages in our document.
		If pages.Count < 3 Then
			Console.WriteLine("The document contains less than 3 pages!")
			Console.ReadKey()
			Return
		End If
		' 50mm - from the left;            
		' 30mm - from the top.
		Dim posFromLeft As Single = 50.0F
		Dim posFromTop As Single = 30.0F

		' Insert the text "Hello World!" into the page 2.
		Dim text1 As New Run(dc, "Hello World!", New CharacterFormat() With {
			.Size = 36,
			.FontColor = Color.Red,
			.FontName = "Arial"
		})
		InsertShape(dc, pages(1), text1, posFromLeft, posFromTop)

		' Insert the text "Hej Världen!" into the page 3.
		Dim text2 As New Run(dc, "Hej Världen!", New CharacterFormat() With {
			.Size = 36,
			.FontColor = Color.Orange,
			.FontName = "Arial"
		})
		InsertShape(dc, pages(2), text2, posFromLeft, posFromTop)

		' 4. Save the document back.
		dc.Save(outFile)
		System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
	End Sub
	''' <summary>
	''' Inserts a Shape with text into the specific page using coordinates.
	''' </summary>
	''' <param name="dc">The document.</param>
	''' <param name="page">The specific page to insert the Shape.</param>
	''' <param name="text">The formatted text (Run object).</param>
	''' <param name="posFromLeftMm">The distance in mm from left corner of the page</param>
	''' <param name="posFromTopMm">The distance in mm from top corner of the page</param>
	Sub InsertShape(ByVal dc As DocumentCore, ByVal page As DocumentPage, ByVal text As Run, ByVal posFromLeftMm As Single, ByVal posFromTopMm As Single)
		Dim hp As New HorizontalPosition(posFromLeftMm, LengthUnit.Millimeter, HorizontalPositionAnchor.Page)
		Dim vp As New VerticalPosition(posFromTopMm, LengthUnit.Millimeter, VerticalPositionAnchor.Page)
		' 100 x 30 mm
		Dim shapeWidth As Single = 100.0F
		Dim shapeHeight As Single = 30.0F
		Dim size As SautinSoft.Document.Drawing.Size = New Size(shapeWidth, shapeHeight, LengthUnit.Millimeter)
		Dim shape As New Shape(dc, New FloatingLayout(hp, vp, size))
		shape.Text.Blocks.Add(New Paragraph(dc, text))
		' Set shape Behind the text.
		TryCast(shape.Layout, FloatingLayout).WrappingStyle = WrappingStyle.BehindText
		' Remove the shape borders.
		shape.Outline.Fill.SetEmpty()
		' Insert shape into the page.
		page.Content.End.Insert(shape.Content)
	End Sub

End Module

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.