当前位置:网站首页>C#/VB. Net to add text / image watermarks to PDF documents
C#/VB. Net to add text / image watermarks to PDF documents
2022-07-01 22:37:00 【51CTO】
When we share on the Internet PDF When you file , It is important to convince people on the other side of the screen that the information released is correct . After all , Any file can be intercepted and modified . Watermark with your logo or specific text PDF Will prove the authenticity of the documents , And prove its security for everyone who will send it .
We will introduce the application of watermarking in detail in this article PDF Methods . This article will be divided into three parts , I will introduce you in detail how to pass C#/VB.NET Code will text / Image watermark is added to PDF file . Want to realize this function , Just perform a few simple steps . Please read the following for details .
Program environment :
In this test , Introduce... Into the program Spire.PDF.dll file .
Method 1:
take Free Spire.PDF for .NET Download to local , decompression , find BIN Under folder Spire.PDF.dll. And then in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ Add reference ”, The local path BIN Under folder dll Add a reference to the program .
Method 2:
adopt NuGet install . This can be done by 2 Methods of installation :
1. Can be in Visual Studio Open in “ Solution explorer ”, Right click “ quote ”,“ management NuGet package ”, And then the search “Free Spire.PDF”, Click on “ install ”. Wait for the program installation to complete .
2. Copy the following to PM Console installation .
Install-Package FreeSpire.PDF -Version 8.6.0
to PDF Add text watermark
Specific steps :
- establish PdfDocument Object and use PdfDocument.LoadFromFile() Method to load the sample document .
- use PdfFontBase.MeasureString() Method to set the watermark text and measure the text size .
- Browse all pages in the document .
- Use PdfPageBase.Canvas.TraslateTransform() Method to translate the coordinate system of a page to the specified coordinates , Use PdfPageBase.Canvas.RotateTransform() Method rotate the coordinate system counterclockwise 45 degree .
- Use PdfPageBase.Canvas.DrawString() Method draw watermark text on the page .
- use PdfDocument.SaveToFile() Method as PDF file .
Complete code :
【C#】
using
Spire.
Pdf;
using
Spire.
Pdf.
Graphics;
using
System.
Drawing;
namespace
AddTextWatermarkToPdf
{
class
Program
{
static
void
Main(
string[]
args)
{
// establish PdfDocument object
PdfDocument
pdf
=
new
PdfDocument();
// load PDF file
pdf.
LoadFromFile(
"Sample.pdf");
// establish PdfTrueTypeFont object
PdfTrueTypeFont
font
=
new
PdfTrueTypeFont(
new
Font(
"Arial",
50f),
true);
// Set watermark text
string
text
=
"CONFIDENTIAL";
// Measure text size
SizeF
textSize
=
font.
MeasureString(
text);
// Calculate the value of two offset variables , Used to calculate the translation of the coordinate system
float
offset1
= (
float)(
textSize.
Width
*
System.
Math.
Sqrt(
2)
/
4);
float
offset2
= (
float)(
textSize.
Height
*
System.
Math.
Sqrt(
2)
/
4);
// Browse all pages in the document .
foreach (
PdfPageBase
page
in
pdf.
Pages)
{
// Set page transparency
page.
Canvas.
SetTransparency(
0.8f);
// Translate the coordinate system by specifying coordinates page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);
// Rotate the coordinate system counterclockwise 45 degree
page.
Canvas.
RotateTransform(
-
45);
// Draw watermark text on the page
page.
Canvas.
DrawString(
text,
font,
PdfBrushes.
DarkGray,
0,
0);
}
// Save the changes as a new file
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)
' establish PdfDocument object
Dim
pdf
As
PdfDocument =
New
PdfDocument
' load PDF file
pdf
.LoadFromFile(
"Sample.pdf")
' establish PdfTrueTypeFont object
Dim
font
As
PdfTrueTypeFont =
New
PdfTrueTypeFont(
New
Font(
"Arial",
50
!),
true)
' Set watermark text
Dim
text
As
String =
"CONFIDENTIAL"
' Measure text size
Dim
textSize
As
SizeF =
font
.MeasureString(
text)
' Calculate the value of two offset variables , Used to calculate the translation of the coordinate system
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)
' Browse all pages in the document .
For
Each
page
As
PdfPageBase
In
pdf
.Pages
' Set page transparency
page
.Canvas
.SetTransparency(
0.8
!)
' Translate the coordinate system by specifying coordinates
page
.Canvas
.TranslateTransform(((
page
.Canvas
.Size
.Width
/
2)
_
- (
offset1
-
offset2)), ((
page
.Canvas
.Size
.Height
/
2)
_
+ (
offset1
-
offset2)))
' Rotate the coordinate system counterclockwise 45 degree
page
.Canvas
.RotateTransform(
-
45)
' Draw watermark text on the page
page
.Canvas
.DrawString(
text,
font,
PdfBrushes
.DarkGray,
0,
0)
Next
' Save the changes as a new file
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.
design sketch :

towards PDF Add multi line text watermark
Specific steps :
- establish PDF Document combination PdfDocument.LoadFromFile() Method to load the file .
- Get the first page .
- Draw text watermark . Use PdfCanvas.TranslateTransform() Method to set the text size . Use PdfCanvas.RotateTransform() Method to set the font angle . And use PdfCanvas.DrawString() Method to draw the text content you want .
- Use PdfDocument.SaveToFile() Method to save the document to a new PDF file
Complete code :
【C#】
using
System.
Drawing;
using
Spire.
Pdf;
using
Spire.
Pdf.
Graphics;
namespace
TextWaterMark
{
class
Program
{
static
void
Main(
string[]
args)
{
// establish PDF Document and load the file
PdfDocument
doc
=
new
PdfDocument();
doc.
LoadFromFile(
"Sample1.pdf");
// Get the first page
PdfPageBase
page
=
doc.
Pages[
0];
// Draw text watermark
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));
// Save as PDF file
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)
' establish PDF Document and load the file
Dim
doc
As
PdfDocument =
New
PdfDocument
doc
.LoadFromFile(
"Sample1.pdf")
' Get the first page
Dim
page
As
PdfPageBase =
doc
.Pages(
0)
' Draw text watermark
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))
' Save as PDF file
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.
design sketch :

to PDF Add image watermark
Specific steps :
- Create a PdfDocument Object and use PdfDocument.LoadFromFile() Method to load an example PDF file .
- Use Image.FromFile() Method to load the image .
- Get the picture size .
- Cycle through the pages in the document , adopt PdfDocument.Pages() Property to get the specific page .
- adopt PdfPageBase.BackgroundImage Property to set the image as the watermark image of the current page . adopt PdfPageBase.BackgroundRegion Property to set the image position and size .
- Use PdfDocument.SaveToFile() Method to save the document to a file .
Complete code :
【C#】
using
Spire.
Pdf;
using
System.
Drawing;
namespace
AddImageWatermark
{
class
Program
{
static
void
Main(
string[]
args)
{
// Create a PdfDocument object
PdfDocument
document
=
new
PdfDocument();
// Load an example PDF file
document.
LoadFromFile(
"sample.pdf");
// Load image
Image
image
=
Image.
FromFile(
"logo.png");
// Get the picture size
int
imgWidth
=
image.
Width;
int
imgHeight
=
image.
Height;
// Browse the pages in the document
for (
int
i
=
0;
i
<
document.
Pages.
Count;
i
++)
{
// Get page width and height
float
pageWidth
=
document.
Pages[
i].
ActualSize.
Width;
float
pageHeight
=
document.
Pages[
i].
ActualSize.
Height;
// Set background opacity
document.
Pages[
i].
BackgroudOpacity
=
0.3f;
// Set the image as the watermark image of the current page
document.
Pages[
i].
BackgroundImage
=
image;
// Place the background image in the center of the page
Rectangle
rect
=
new
Rectangle((
int)(
pageWidth
-
imgWidth)
/
2, (
int)(
pageHeight
-
imgHeight)
/
2,
imgWidth,
imgHeight);
document.
Pages[
i].
BackgroundRegion
=
rect;
}
// Save the document as PDF file
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)
' Create a PdfDocument object
Dim
document
As
PdfDocument =
New
PdfDocument
' Load an example PDF file
document
.LoadFromFile(
"sample.pdf")
' Load image
Dim
image
As
Image =
Image
.FromFile(
"logo.png")
' Get the picture size
Dim
imgWidth
As
Integer =
image
.Width
Dim
imgHeight
As
Integer =
image
.Height
' Browse the pages in the document
Dim
i
As
Integer =
0
Do
While (
i
<
document
.Pages
.Count)
' Get page width and height
Dim
pageWidth
As
Single =
document
.Pages(
i)
.ActualSize
.Width
Dim
pageHeight
As
Single =
document
.Pages(
i)
.ActualSize
.Height
' Set background opacity
document
.Pages(
i)
.BackgroudOpacity =
0.3
!
' Set the image as the watermark image of the current page
document
.Pages(
i)
.BackgroundImage =
image
' Place the background image in the center of the page
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
' Save the document as PDF file
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.
design sketch :

边栏推荐
- Matlab traverses images, string arrays and other basic operations
- How to write a performance test plan
- Clean up system cache and free memory under Linux
- 【目标跟踪】|单目标跟踪指标
- MySQL series transaction log redo log learning notes
- Object memory layout
- LIS (longest ascending subsequence) problem that can be understood [easy to understand]
- [noip2013] building block competition [noip2018] road laying greed / difference
- Use of vscode
- Airserver mobile phone third-party screen projection computer software
猜你喜欢

flink sql-client 使用 对照并熟悉官方文档

Business visualization - make your flowchart'run'up

Is PMP certificate really useful?

Little p weekly Vol.11

Clean up system cache and free memory under Linux

MySQL learning notes - SQL optimization of optimization

【目标跟踪】|单目标跟踪指标

功能测试报告的编写

What is the difference between PMP and NPDP?

Classify boost libraries by function
随机推荐
Medium pen test questions: flip the string, such as ABCD, print out DCBA
内存导致的电脑游戏中显示hdmi无信号 从而死机的情况
Unity 使用Sqlite
Airserver mobile phone third-party screen projection computer software
多种智能指针
CIO's discussion and Analysis on the definition of high-performance it team
#yyds干货盘点# 解决名企真题:扭蛋机
MySQL数据库详细学习教程
Matlab traverses images, string arrays and other basic operations
Why must digital transformation strategies include continuous testing?
详解Volatile关键字
91.(cesium篇)cesium火箭发射模拟
高攀不起的希尔排序,直接插入排序
[monomer] recommended configuration of streaming information i-bpsv3 server
详解Kubernetes网络模型
[STM32] stm32cubemx tutorial II - basic use (new projects light up LED lights)
The difference between NiO and traditional IO
LC501. 二叉搜索树中的众数
【juc学习之路第8天】Condition
Gaussdb (DWS) active prevention and troubleshooting