当前位置:网站首页>C#/VB.NET 给PDF文档添加文本/图像水印
C#/VB.NET 给PDF文档添加文本/图像水印
2022-07-01 21:46:00 【51CTO】
当我们在网上共享PDF文件时,重要的是要让屏幕另一侧的人相信发布的信息是正确的。毕竟,任何文件都可以被截获并进行修改。带有你的标志或特定文本的水印 PDF 将证明文件的真实性,并为将要发送给的每个人证明其安全性。
我们将在本文中详细介绍应用水印PDF的方法。本文将分为三个部分,详细为您介绍如何通过 C#/VB.NET代码将文本/图像水印添加到PDF文档。想要实现此功能,只需执行几个简单的步骤。详情请阅读以下内容。
程序环境:
本次测试时,在程序中引入 Spire.PDF.dll 文件。
方法 1:
将 Free Spire.PDF for .NET 下载到本地,解压,找到 BIN 文件夹下的 Spire.PDF.dll。然后在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“添加引用”,将本地路径 BIN 文件夹下的 dll 文件添加引用至程序。
方法 2:
通过 NuGet 安装。可通过以下 2 种方法安装:
1. 可以在 Visual Studio 中打开“解决方案资源管理器”,鼠标右键点击“引用”,“管理 NuGet 包”,然后搜索“Free Spire.PDF”,点击“安装”。等待程序安装完成。
2. 将以下内容复制到 PM 控制台安装。
Install-Package FreeSpire.PDF -Version 8.6.0
给PDF添加文本水印
具体步骤:
- 创建 PdfDocument 对象并用PdfDocument.LoadFromFile()方法加载示例文档。
- 用PdfFontBase.MeasureString()方法设置水印文字和测量文字大小。
- 浏览文档中的所有页面。
- 使用 PdfPageBase.Canvas.TraslateTransform()方法将某个页面的坐标系平移指定坐标,使用 PdfPageBase.Canvas.RotateTransform() 方法将坐标系逆时针旋转 45 度。
- 使用PdfPageBase.Canvas.DrawString()方法在页面上绘制水印文字。
- 用PdfDocument.SaveToFile()方法保存为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( "Sample.pdf");
//创建PdfTrueTypeFont对象
PdfTrueTypeFont font = new PdfTrueTypeFont( new Font( "Arial", 50f), true);
//设置水印文本
string text = "CONFIDENTIAL";
//测量文本尺寸
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");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
【VB.NET】
Imports Spire .Pdf
Imports Spire .Pdf .Graphics
Imports System .Drawing
Namespace AddTextWatermarkToPdf
Class Program
Private Shared Sub Main( ByVal args() As String)
'创建 PdfDocument 对象
Dim pdf As PdfDocument = New PdfDocument
'加载PDF文档
pdf .LoadFromFile( "Sample.pdf")
'创建PdfTrueTypeFont 对象
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont( New Font( "Arial", 50 !), true)
'设置水印文本
Dim text As String = "CONFIDENTIAL"
'测量文本尺寸
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)
'浏览文档中的所有页面。
For Each page As PdfPageBase In pdf .Pages
'设置页面透明度
page .Canvas .SetTransparency( 0.8 !)
'通过指定坐标平移坐标系
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
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
效果图:
向PDF添加多行文本水印
具体步骤:
- 创建PDF文档并用PdfDocument.LoadFromFile()方法加载文件。
- 获取第一页。
- 绘制文字水印。 使用 PdfCanvas.TranslateTransform() 方法设置文本大小。 使用PdfCanvas.RotateTransform()方法设置字体角度。并使用 PdfCanvas.DrawString() 方法绘制您想要的文本内容。
- 使用 PdfDocument.SaveToFile() 方法将文档保存到新的PDF文件
完整代码:
【C#】
using System. Drawing;
using Spire. Pdf;
using Spire. Pdf. Graphics;
namespace TextWaterMark
{
class Program
{
static void Main( string[] args)
{
//创建PDF文档并加载文件
PdfDocument doc = new PdfDocument();
doc. LoadFromFile( "Sample1.pdf");
//获取第一页
PdfPageBase page = doc. Pages[ 0];
//绘制文字水印
PdfTilingBrush brush
= new PdfTilingBrush( new SizeF( page. Canvas. ClientSize. Width / 2, page. Canvas. ClientSize. Height / 3));
brush. Graphics. SetTransparency( 0.3f);
brush. Graphics. Save();
brush. Graphics. TranslateTransform( brush. Size. Width / 2, brush. Size. Height / 2);
brush. Graphics. RotateTransform( - 45);
brush. Graphics. DrawString( "internal use",
new PdfFont( PdfFontFamily. Helvetica, 24), PdfBrushes. Violet, 0, 0,
new PdfStringFormat( PdfTextAlignment. Center));
brush. Graphics. Restore();
brush. Graphics. SetTransparency( 1);
page. Canvas. DrawRectangle( brush, new RectangleF( new PointF( 0, 0), page. Canvas. ClientSize));
//保存为PDF文件
doc. SaveToFile( "TextWaterMark1.pdf");
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
【VB.NET】
Imports System .Drawing
Imports Spire .Pdf
Imports Spire .Pdf .Graphics
Namespace TextWaterMark
Class Program
Private Shared Sub Main( ByVal args() As String)
'创建PDF文档并加载文件
Dim doc As PdfDocument = New PdfDocument
doc .LoadFromFile( "Sample1.pdf")
'获取第一页
Dim page As PdfPageBase = doc .Pages( 0)
'绘制文字水印
Dim brush As PdfTilingBrush = New PdfTilingBrush( New SizeF(( page .Canvas .ClientSize .Width / 2), ( page .Canvas .ClientSize .Height / 3)))
brush .Graphics .SetTransparency( 0.3 !)
brush .Graphics .Save
brush .Graphics .TranslateTransform(( brush .Size .Width / 2), ( brush .Size .Height / 2))
brush .Graphics .RotateTransform( - 45)
brush .Graphics .DrawString( "internal use", New PdfFont( PdfFontFamily .Helvetica, 24), PdfBrushes .Violet, 0, 0, New PdfStringFormat( PdfTextAlignment .Center))
brush .Graphics .Restore
brush .Graphics .SetTransparency( 1)
page .Canvas .DrawRectangle( brush, New RectangleF( New PointF( 0, 0), page .Canvas .ClientSize))
'保存为PDF文件
doc .SaveToFile( "TextWaterMark1.pdf")
End Sub
End Class
End Namespace
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
效果图:
给PDF添加图像水印
具体步骤:
- 创建一个PdfDocument 对象并使用PdfDocument.LoadFromFile() 方法加载一个示例PDF 文档。
- 使用 Image.FromFile() 方法加载图像。
- 获取图片尺寸。
- 循环浏览文档中的页面,通过PdfDocument.Pages() 属性获取具体页面。
- 通过PdfPageBase.BackgroundImage 属性设置图片为当前页面的水印图片。通过 PdfPageBase.BackgroundRegion 属性设置图像位置和大小。
- 使用 PdfDocument.SaveToFile() 方法将文档保存到文件.
完整代码:
【C#】
using Spire. Pdf;
using System. Drawing;
namespace AddImageWatermark
{
class Program
{
static void Main( string[] args)
{
//创建一个PdfDocument 对象
PdfDocument document = new PdfDocument();
//加载一个示例PDF 文档
document. LoadFromFile( "sample.pdf");
//加载图像
Image image = Image. FromFile( "logo.png");
//获取图片尺寸
int imgWidth = image. Width;
int imgHeight = image. Height;
//浏览文档中的页面
for ( int i = 0; i < document. Pages. Count; i ++)
{
//获取页面宽高
float pageWidth = document. Pages[ i]. ActualSize. Width;
float pageHeight = document. Pages[ i]. ActualSize. Height;
//设置背景不透明度
document. Pages[ i]. BackgroudOpacity = 0.3f;
//设置图片为当前页面的水印图片
document. Pages[ i]. BackgroundImage = image;
//将背景图片放在页面的中心
Rectangle rect = new Rectangle(( int)( pageWidth - imgWidth) / 2, ( int)( pageHeight - imgHeight) / 2, imgWidth, imgHeight);
document. Pages[ i]. BackgroundRegion = rect;
}
//将文档保存为PDF文件
document. SaveToFile( "AddImageWatermark.pdf");
document. Close();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
【VB.NET】
Imports Spire .Pdf
Imports System .Drawing
Namespace AddImageWatermark
Class Program
Private Shared Sub Main( ByVal args() As String)
'创建一个PdfDocument 对象
Dim document As PdfDocument = New PdfDocument
'加载一个示例PDF 文档
document .LoadFromFile( "sample.pdf")
'加载图像
Dim image As Image = Image .FromFile( "logo.png")
'获取图片尺寸
Dim imgWidth As Integer = image .Width
Dim imgHeight As Integer = image .Height
'浏览文档中的页面
Dim i As Integer = 0
Do While ( i < document .Pages .Count)
'获取页面宽高
Dim pageWidth As Single = document .Pages( i) .ActualSize .Width
Dim pageHeight As Single = document .Pages( i) .ActualSize .Height
'设置背景不透明度
document .Pages( i) .BackgroudOpacity = 0.3 !
'设置图片为当前页面的水印图片
document .Pages( i) .BackgroundImage = image
'将背景图片放在页面的中心
Dim rect As Rectangle = New Rectangle(( CType(( pageWidth - imgWidth), Integer) / 2), ( CType(( pageHeight - imgHeight), Integer) / 2), imgWidth, imgHeight)
document .Pages( i) .BackgroundRegion = rect
i = ( i + 1)
Loop
'将文档保存为PDF文件
document .SaveToFile( "AddImageWatermark.pdf")
document .Close
End Sub
End Class
End Namespace
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
效果图:
边栏推荐
- Make a three digit number of all daffodils "recommended collection"
- 详解LockSupport的使用
- Little p weekly Vol.11
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- Airserver mobile phone third-party screen projection computer software
- GenICam GenTL 标准 ver1.5(4)第五章 采集引擎
- What is the difference between consonants and Initials? (difference between initials and consonants)
- Can you get a raise? Analysis on gold content of PMP certificate
- [live broadcast review] the first 8 live broadcasts of battle code Pioneer have come to a perfect end. Please look forward to the next one!
- MySQL的视图练习题
猜你喜欢
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
PyTorch磨刀篇|argmax和argmin函数
Microsoft, Columbia University | Godel: large scale pre training of goal oriented dialogue
100年仅6款产品获批,疫苗竞争背后的“佐剂”江湖
EasyExcel 复杂数据导出
并发编程系列之FutureTask源码学习笔记
YOLOv5.5 调用本地摄像头
比较版本号[双指针截取自己想要的字串]
Clean up system cache and free memory under Linux
News classification based on LSTM model
随机推荐
黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,
牛客月赛-分组求对数和
Application of real estate management based on 3D GIS
The difference between NiO and traditional IO
MIT|256KB 内存下的设备上训练
AirServer2022最新版功能介绍及下载
CSDN购买的课程从哪里可以进入
Learning notes on futuretask source code of concurrent programming series
使用闭包实现点击按钮切换 toggle
灵动微 MM32 多路ADC-DMA配置
Icml2022 | interventional contrastive learning based on meta semantic regularization
LIS (longest ascending subsequence) problem that can be understood [easy to understand]
并发编程系列之FutureTask源码学习笔记
首席信息官对高绩效IT团队定义的探讨和分析
2020-ViT ICLR
Make a three digit number of all daffodils "recommended collection"
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
Basic operation of binary tree
信标委云原生专题组组长,任重道远!
Recent public ancestor offline practice (tarjan)