当前位置:网站首页>C#/VB.NET 给PDF文档添加文本/图像水印
C#/VB.NET 给PDF文档添加文本/图像水印
2022-07-06 10:41:00 【InfoQ】
给PDF文档添加文本水印
- 创建 PdfDocument 对象并用PdfDocument.LoadFromFile()方法加载示例文档。
- 用PdfFontBase.MeasureString()方法设置水印文字和测量文字大小。
- 浏览文档中的所有页面。
- 使用 PdfPageBase.Canvas.TraslateTransform()方法将某个页面的坐标系平移指定坐标,使用 PdfPageBase.Canvas.RotateTransform() 方法将坐标系逆时针旋转 45 度。
- 使用PdfPageBase.Canvas.DrawString()方法在页面上绘制水印文字。
- 用PdfDocument.SaveToFile()方法保存为PDF文件。
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");
}
}
}
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

向PDF添加多行文本水印
- 创建PDF文档并用PdfDocument. LoadFromFile()方法加载文件。
- 获取第一页。
- 绘制文字水印。 使用 PdfCanvas.TranslateTransform() 方法设置文本大小。 使用PdfCanvas.RotateTransform()方法设置字体角度。并使用 PdfCanvas.DrawString() 方法绘制您想要的文本内容。
- 使用 PdfDocument.SaveToFile() 方法将文档保存到新的PDF文件。
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");
}
}
}
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

给PDF添加图像水印
- 创建一个PdfDocument 对象并使用PdfDocument.LoadFromFile() 方法加载一个示例PDF 文档。
- 使用 Image.FromFile() 方法加载图像。
- 获取图片尺寸。
- 循环浏览文档中的页面,通过PdfDocument.Pages() 属性获取具体页面。
- 通过PdfPageBase.BackgroundImage 属性设置图片为当前页面的水印图片。通过 PdfPageBase.BackgroundRegion 属性设置图像位置和大小。
- 使用 PdfDocument.SaveToFile() 方法将文档保存到文件中。
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();
}
}
}
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

边栏推荐
- Distill knowledge from the interaction model! China University of science and Technology & meituan proposed virt, which combines the efficiency of the two tower model and the performance of the intera
- C语言高校实验室预约登记系统
- SAP Fiori 应用索引大全工具和 SAP Fiori Tools 的使用介绍
- echart简单组件封装
- Coco2017 dataset usage (brief introduction)
- Implementation of queue
- Windows连接Linux上安装的Redis
- 【剑指 Offer】 60. n个骰子的点数
- JDBC驱动器、C3P0、Druid和JDBCTemplate相关依赖jar包
- Excellent open source fonts for programmers
猜你喜欢

The third season of Baidu online AI competition is coming in midsummer, looking for you who love AI!

Xu Xiang's wife Ying Ying responded to the "stock review": she wrote it!

Execution process of MySQL query request - underlying principle

44 colleges and universities were selected! Publicity of distributed intelligent computing project list

2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目

Blue Bridge Cup real question: one question with clear code, master three codes

TOP命令详解
![[the 300th weekly match of leetcode]](/img/a7/16b491656863e2c423ff657ac6e9c5.png)
[the 300th weekly match of leetcode]

Recommend easy-to-use backstage management scaffolding, everyone open source

传输层 拥塞控制-慢开始和拥塞避免 快重传 快恢复
随机推荐
Use cpolar to build a business website (1)
MySQL查询请求的执行过程——底层原理
Jerry is the custom background specified by the currently used dial enable [chapter]
Afnetworking framework_ Upload file or image server
第三季百度网盘AI大赛盛夏来袭,寻找热爱AI的你!
std::true_ Type and std:: false_ type
Markdown syntax for document editing (typera)
287. Find duplicates
D binding function
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
FMT open source self driving instrument | FMT middleware: a high real-time distributed log module Mlog
With the implementation of MapReduce job de emphasis, a variety of output folders
递归的方式
Self supervised heterogeneous graph neural network with CO comparative learning
Maixll dock camera usage
F200 - UAV equipped with domestic open source flight control system based on Model Design
POJ 2208 six lengths of tetrahedron are known, and the volume is calculated
Stm32+mfrc522 completes IC card number reading, password modification, data reading and writing
A method of sequentially loading Unity Resources
UDP协议:因性善而简单,难免碰到“城会玩”