Как вставить текст в существующий документ по определенным координатам (x,y) на C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Загрузите документ DOCX.
  3. Получите страницы документа.
  4. На определенной странице вставьте текст по x,y позициям.

В этом примере кода показано, как вставить отформатированный текст в существующий example.docx документ на второй и третьей страницах по указанным координатам.

Загрузите полученный файл: result.docx

Полный код

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


Если вам нужен пример кода или у вас есть вопрос: напишите нам по адресу [email protected] или спросите в онлайн-чате (правый нижний угол этой страницы) или используйте форму ниже:



Вопросы и предложения всегда приветствуются!

Мы разрабатываем компоненты .Net с 2002 года. Мы знаем форматы PDF, DOCX, RTF, HTML, XLSX и Images. Если вам нужна помощь в создании, изменении или преобразовании документов в различных форматах, мы можем вам помочь. Мы напишем для вас любой пример кода абсолютно бесплатно.