C#/VB.NET——向PDF文档添加文本水印

水印是出现在文档中现有内容前面或后面的文本或图像。它通常用于防止文档被非法使用。在下文中,我将介绍如何使用Spire.PDF for .NET在C#和VB.NET程序中向PDF添加单行或多行文本水印。

1. 添加单行文本水印到PDF

2. 添加多行文本水印到PDF

安装Spire.PDF

首先,你需要从这个链接下载Spire.PDF的最新版本,并手动将DLL文件添加到你的应用程序中作为引用。或者,你可以通过NuGet直接安装它。

示例1 添加单行文本水印到PDF

[C#]

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

namespace AddTextWatermarkToPdf

{

    class Program

    {

        static void Main(string[] args)

        {

            //创建一个PdfDocument 对象

            PdfDocument pdf = new PdfDocument();

            //加载一个示例PDF文档

            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //创建一个PdfTrueTypeFont 对象

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 55f), true);

            //设置水印文本

            string text = "Internal Use";

            //测量文本大小

            SizeF textSize =

font.MeasureString(text);

            //计算两个偏移变量的值,用于计算坐标系的平移量

float offset1 = (float)(textSize.Width * System.Math.Sqrt(2)

/ 4);

            float offset2 = (float)(textSize.Height *

System.Math.Sqrt(2) / 4);

            //遍历文档中的所有页面

            foreach (PdfPageBase page in pdf.Pages)

            {

                //设置页面透明度

                page.Canvas.SetTransparency(0.8f);

                //将坐标系平移到右下角

                page.Canvas.TranslateTransform(page.Canvas.Size.Width

/ 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);

                //将坐标系逆时针旋转45度

                page.Canvas.RotateTransform(-45);

                //在页面上绘制水印文本

                page.Canvas.DrawString(text,

font, PdfBrushes.DarkGray, 0, 0);

            }

            //将更改保存到另一个文件

            pdf.SaveToFile("TextWatermark.pdf");

        }

    }

}


[VB.NET]

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

Imports System.Drawing


Namespace AddTextWatermarkToPdf

    Class Program

        Shared  Sub Main(ByVal args() As String)

            '创建一个PdfDocument对象

            Dim pdf As PdfDocument =  New PdfDocument()


            '加载一个示例 PDF文档

            pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")


            '创建一个PdfTrueTypeFont对象

            Dim font As PdfTrueTypeFont =  New PdfTrueTypeFont(New Font("Arial",55f),True)


            '设置水印文本

            Dim text As String =  "Internal Use"


            '测量文本大小

            Dim textSize As SizeF = 

font.MeasureString(text)


            '计算两个偏移变量的值,用于计算坐标系的平移量

            Dim offset1 As single = CType((textSize.Width *

System.Math.Sqrt(2) / 4), single)

            Dim offset2 As single = CType((textSize.Height * System.Math.Sqrt(2)

/ 4), single)


            '遍历文档中的所有页面

            Dim page As PdfPageBase

            For Each page In pdf.Pages

                '设置页面透明度

                page.Canvas.SetTransparency(0.8f)


                '将坐标系平移到右下角

                page.Canvas.TranslateTransform(page.Canvas.Size.Width

/ 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2)


                '将坐标系逆时针旋转45度

                page.Canvas.RotateTransform(-45)


                '在页面上绘制水印文本

                page.Canvas.DrawString(text,

font, PdfBrushes.DarkGray, 0, 0)

            Next


            '将更改保存到另一个文件

            pdf.SaveToFile("TextWatermark.pdf")

        End Sub

    End Class

End Namespace

示例2 添加多行文本水印到PDF

[C#]

using System;

using Spire.Pdf;

using Spire.Pdf.Graphics;

using System.Drawing;

namespace AddMultiLineTextWatermark

{

    class Program

    {

        static void Main(string[] args)

        {

            //创建一个PdfDocument 实例

            PdfDocument pdf = new PdfDocument();

            //加载一个 PDF文档

            pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");

            //创建一个PdfTrueTypeFont对象

            PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 20f), true);

            //遍历文档中的所有页面

            for (int i = 0; i <

pdf.Pages.Count; i++)

            {

                //向指定页面添加文本水印

                InsertTextWatermark(pdf.Pages[i],"DotNetFiles", font, 3, 3);

            }

            //将文档保存到文件

            pdf.SaveToFile("MultiLineTextWaterMark.pdf");

            System.Diagnostics.Process.Start("MultiLineTextWaterMark.pdf");

        }

        static void InsertTextWatermark(PdfPageBase

page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum)

        {

            //测量文本大小

            SizeF textSize =

font.MeasureString(watermarkText);

            //计算两个偏移变量的值,用于计算坐标系的平移量

            float offset1 = (float)(textSize.Width *

System.Math.Sqrt(2) / 4);

            float offset2 = (float)(textSize.Height *

System.Math.Sqrt(2) / 4);

            //创建一个PdfTilingBrush对象

            PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.ActualSize.Width

/ columnNum, page.ActualSize.Height / rowNum));

            brush.Graphics.SetTransparency(0.3f);

            brush.Graphics.Save();

            brush.Graphics.TranslateTransform(brush.Size.Width

/ 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2);

            brush.Graphics.RotateTransform(-45);

            //在画刷上绘制水印文本

            brush.Graphics.DrawString(watermarkText,

font, PdfBrushes.Violet, 0, 0);

            brush.Graphics.Restore();


            //使用画刷绘制一个覆盖整个页面的矩形

            page.Canvas.DrawRectangle(brush,new RectangleF(new PointF(0, 0), page.ActualSize));

        }

    }

}


[VB.NET]

Imports System

Imports Spire.Pdf

Imports Spire.Pdf.Graphics

Imports System.Drawing


Namespace AddMultiLineTextWatermark

    Class Program

        Shared  Sub Main(ByVal args() As String)

            '创建一个 PdfDocument 实例

            Dim pdf As PdfDocument =  New PdfDocument()


            '加载一个 PDF文档

            pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")


            '创建一个PdfTrueTypeFont对象

            Dim font As PdfTrueTypeFont =  New PdfTrueTypeFont(New Font("Arial",20f),True)


            '遍历文档中的所有页面

            Dim i As Integer

            For  i = 0 To  pdf.Pages.Count- 1  Step  i + 1

                '向指定页面添加文本水印

                InsertTextWatermark(pdf.Pages(i),"DotNetFiles", font, 3, 3)

            Next


            '将文档保存到文件

            pdf.SaveToFile("MultiLineTextWaterMark.pdf")

            System.Diagnostics.Process.Start("MultiLineTextWaterMark.pdf")

        End Sub


        Shared  Sub InsertTextWatermark(ByVal page As PdfPageBase, ByVal watermarkText As String, ByVal font As PdfTrueTypeFont, ByVal rowNum As Integer, ByVal columnNum As Integer)


            '测量文本大小

            Dim textSize As SizeF = 

font.MeasureString(watermarkText)


            '计算两个偏移变量的值,用于计算坐标系的平移量

            Dim offset1 As single = CType((textSize.Width *

System.Math.Sqrt(2) / 4), single)

            Dim offset2 As single = CType((textSize.Height *

System.Math.Sqrt(2) / 4), single)


            '创建一个PdfTilingBrush对象

            Dim brush As PdfTilingBrush =  New PdfTilingBrush(New SizeF(page.ActualSize.Width

/ columnNum,page.ActualSize.Height / rowNum))

            brush.Graphics.SetTransparency(0.3f)

            brush.Graphics.Save()

            brush.Graphics.TranslateTransform(brush.Size.Width

/ 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2)

            brush.Graphics.RotateTransform(-45)


            '在画刷上绘制水印文本

            brush.Graphics.DrawString(watermarkText,

font, PdfBrushes.Violet, 0, 0)

            brush.Graphics.Restore()


            '使用画刷绘制一个覆盖整个页面的矩形

            page.Canvas.DrawRectangle(brush,New RectangleF(New PointF(0, 0),

page.ActualSize))

        End Sub

    End Class

End Namespace

你可能感兴趣的:(C#/VB.NET——向PDF文档添加文本水印)