Как вставить текстовый водяной знак в документ с помощью C# и .NET

  1. Добавьте SautinSoft.Document из Nuget.
  2. Загрузите документ DOCX.
  3. Создайте водяной знак.
  4. Вставьте водяной знак в заголовок документа.

В этом примере кода показано, как вставить текстовый водяной знак в существующий 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/

            InsertTextAsWatermark();
        }
        /// <summary>
        /// How to insert a text watermark in the existing PDF, DOCX, any document to the each page.
        /// </summary>
        /// <remarks>
        /// Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-watermark-in-the-existing-pdf-docx-document-to-each-page-net-csharp-vb.php
        /// </remarks>
        static void InsertTextAsWatermark()
        {
            // Let us say, we want to insert a textual "Watermark" at the each page of the
            // DOCX document and specify angle of 45 degree for it.
            
            // If we'll insert the text at the document header (or footer), so it will appear on the each page.
            
            // Also let's insert our Watermark behind the main content.
            string inpFile = @"..\..\..\example.docx";
            string outFile = @"Result.docx";

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

            // 2. Create a Shape with our Watermark text.
            // Place the watermark:
            // 30mm - from the page left;            
            // 150mm - from the page top.
            // 60 - angle.
            float posFromLeft = 30f;
            float posFromTop = 150f;
            float angle = -60f;
            // Size of the Shape, 200mm x 40mm.            
            SautinSoft.Document.Drawing.Size size = new Size(200f, 40f, LengthUnit.Millimeter);
            Shape watermark = new Shape(dc, new FloatingLayout(new HorizontalPosition(posFromLeft, LengthUnit.Millimeter, HorizontalPositionAnchor.Page),
            new VerticalPosition(posFromTop, LengthUnit.Millimeter, VerticalPositionAnchor.Page), size));
            // Rotate shape.
            watermark.Rotation = angle;
            // Create the text.
            Run text = new Run(dc, "Watermark!", new CharacterFormat()
            {
                Size = 100f,
                FontColor = Color.Black,
                FontName = "Arial"
            });
            watermark.Text.Blocks.Add(new Paragraph(dc, text));
            // Set shape Behind the main document contents.
            (watermark.Layout as FloatingLayout).WrappingStyle = WrappingStyle.BehindText;
            // Remove the shape borders.
            watermark.Outline.Fill.SetEmpty();

            // 3. Iterate through Sections, and insert our Watermark to the default header of the each section.
            foreach (Section section in dc.Sections)
            {
                // 2.1. Check the document header, maybe is it already exist?
                var header = dc.Sections[0].HeadersFooters[HeaderFooterType.HeaderDefault];

                if (header == null)
                {
                    // Create a new header, add it into the section.
                    header = new HeaderFooter(dc, HeaderFooterType.HeaderDefault);
                    // Add the header to the section.
                    section.HeadersFooters.Add(header);
                }
                // Add the watermark to the header.
                header.Content.End.Insert(watermark.Content);
            }

            // 4. Save the document back.
            dc.Save(outFile);
            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(outFile) { UseShellExecute = true });
        }
    }
}

Download

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

Module Sample
    Sub Main()
        InsertTextAsWatermark()
    End Sub
    ''' Get your free 30-day key here:   
    ''' https://sautinsoft.com/start-for-free/
    ''' <summary>
    ''' How to insert a text watermark in the existing PDF, DOCX, any document to the each page.
    ''' </summary>
    ''' <remarks>
    ''' Details: https://sautinsoft.com/products/document/help/net/developer-guide/insert-text-watermark-in-the-existing-pdf-docx-document-to-each-page-net-csharp-vb.php
    ''' </remarks>
    Sub InsertTextAsWatermark()
        ' Let us say, we want to insert a textual "Watermark" at the each page of the
        ' DOCX document and specify angle of 45 degree for it.

        ' If we'll insert the text at the document header (or footer), so it will appear on the each page.

        ' Also let's insert our Watermark behind the main content.
        Dim inpFile As String = "..\..\..\example.docx"
        Dim outFile As String = "Result.docx"

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

        ' 2. Create a Shape with our Watermark text.
        ' Place the watermark:
        ' 30mm - from the page left;            
        ' 150mm - from the page top.
        ' 60 - angle.
        Dim posFromLeft As Single = 30.0F
        Dim posFromTop As Single = 150.0F
        Dim angle As Single = -60.0F
        ' Size of the Shape, 200mm x 40mm.            
        Dim size As SautinSoft.Document.Drawing.Size = New Size(200.0F, 40.0F, LengthUnit.Millimeter)
        Dim watermark As New Shape(dc, New FloatingLayout(New HorizontalPosition(posFromLeft, LengthUnit.Millimeter, HorizontalPositionAnchor.Page), New VerticalPosition(posFromTop, LengthUnit.Millimeter, VerticalPositionAnchor.Page), size))
        ' Rotate shape.
        watermark.Rotation = angle
        ' Create the text.
        Dim text As New Run(dc, "Watermark!", New CharacterFormat() With {
                .Size = 100.0F,
                .FontColor = Color.Black,
                .FontName = "Arial"
            })
        watermark.Text.Blocks.Add(New Paragraph(dc, text))
        ' Set shape Behind the main document contents.
        TryCast(watermark.Layout, FloatingLayout).WrappingStyle = WrappingStyle.BehindText
        ' Remove the shape borders.
        watermark.Outline.Fill.SetEmpty()

        ' 3. Iterate through Sections, and insert our Watermark to the default header of the each section.
        For Each section As Section In dc.Sections
            ' 2.1. Check the document header, maybe is it already exist?
            Dim header = dc.Sections(0).HeadersFooters(HeaderFooterType.HeaderDefault)

            If header Is Nothing Then
                ' Create a new header, add it into the section.
                header = New HeaderFooter(dc, HeaderFooterType.HeaderDefault)
                ' Add the header to the section.
                section.HeadersFooters.Add(header)
            End If
            ' Add the watermark to the header.
            header.Content.End.Insert(watermark.Content)
        Next section

        ' 4. Save the document back.
        dc.Save(outFile)
        System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(outFile) With {.UseShellExecute = True})
    End Sub

End Module

Download


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



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

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