当前位置:网站首页>Insert text watermark in PDF
Insert text watermark in PDF
2022-07-28 17:31:00 【InfoQ】
C#/VB.NET: stay PDF Insert a text watermark into the document
install Spire.PDF for .NET
by PDF Add a single line text watermark
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddTextWatermarkToPdf
{
class Program
{
static void Main(string[] args)
{
// establish PdfDocument Class object
PdfDocument pdf = new PdfDocument();
// Load one PDF Document instance
pdf.LoadFromFile(@"sample.pdf");
// establish PdfTrueTypeFont Class object
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(" Song style ", 55f), true);
// Set the text content of the watermark
string text = " Please don't embezzle ";
// Set the text watermark 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);
// Traverse all pages in the document
foreach (PdfPageBase page in pdf.Pages)
{
// Set page transparency
page.Canvas.SetTransparency(0.8f);
// Translate the coordinate system to the lower right corner
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 a text watermark on the page
page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0);
}
// Save changes to another file
pdf.SaveToFile("TextWatermark.pdf");
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace AddTextWatermarkToPdf
Class Program
Shared Sub Main(ByVal args() As String)
' establish PdfDocument Class object
Dim pdf As PdfDocument = New PdfDocument()
' Load one PDF Document instance
pdf.LoadFromFile("C: sample.pdf")
' establish PdfTrueTypeFont Class object
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(" Song style ",55f),True)
' Set the text content of the watermark
Dim text As String = " Please don't embezzle "
' Set the text watermark 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)
' Traverse all pages in the document
Dim page As PdfPageBase
For Each page In pdf.Pages
' Set page transparency
page.Canvas.SetTransparency(0.8f)
' Translate the coordinate system to the lower right corner
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)
' Add a text watermark on the page
page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0)
Next
' Save changes to another file
pdf.SaveToFile("TextWatermark.pdf")
End Sub
End Class
End Namespace

by PDF Add multi line text watermark
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddMultiLineTextWatermark
{
class Program
{
static void Main(string[] args)
{
// establish PdfDocument Class object
PdfDocument pdf = new PdfDocument();
// Load one PDF file
pdf.LoadFromFile(@"sample.pdf");
// establish PdfTrueTypeFont Class object
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font(" Song style ", 20f), true);
// Traverse document pages
for (int i = 0; i < pdf.Pages.Count; i++)
{
// Add a text watermark on a specific page
InsertTextWatermark(pdf.Pages[i], " Do not copy ", font, 3, 3);
}
// Save the file
pdf.SaveToFile("MultiLineTextWaterMark.pdf");
System.Diagnostics.Process.Start("MultiLineTextWaterMark.pdf");
}
static void InsertTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum)
{
// Set the text watermark size
SizeF textSize = font.MeasureString(watermarkText);
// 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);
// Create a tile brush
PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.ActualSize.Width / columnNum, page.ActualSize.Height / rowNum));
brush.Graphics.SetTransparency(0.5f);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2);
brush.Graphics.RotateTransform(-45);
// Draw a text watermark on the tile brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0);
brush.Graphics.Restore();
// Use the tile brush to draw a rectangle covering the entire page
page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.ActualSize));
}
}
}
Imports System
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Imports System.Drawing
Namespace AddMultiLineTextWatermark
Class Program
Shared Sub Main(ByVal args() As String)
' establish PdfDocument Class object
Dim pdf As PdfDocument = New PdfDocument()
' Load one PDF file
pdf.LoadFromFile("sample.pdf")
' establish PdfTrueTypeFont Class object
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font(" Song style ",20f),True)
' Traverse document pages
Dim i As Integer
For i = 0 To pdf.Pages.Count- 1 Step i + 1
' Add a text watermark on a specific page
InsertTextWatermark(pdf.Pages(i), " Do not copy ", font, 3, 3)
Next
' Save the file
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)
' Set the text watermark size
Dim textSize As SizeF = font.MeasureString(watermarkText)
' 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);
' Create a tile brush
Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF(page.ActualSize.Width / columnNum,page.ActualSize.Height / rowNum))
brush.Graphics.SetTransparency(0.5f)
brush.Graphics.Save()
brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2)
brush.Graphics.RotateTransform(-45)
' Draw a text watermark on the tile brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0)
brush.Graphics.Restore()
' Use the tile brush to draw a rectangle covering the entire page
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.ActualSize))
End Sub
End Class
End Namespace

边栏推荐
- Shell脚本之免交互操作
- C # traversal set
- C语言实现扫雷小游戏
- 技术面轻松通过,HR:只有三年大厂经验的不值20K
- Uparse rich text style of uni app
- High speed circuit design practice -- Overview
- Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
- Role of Fortress machine
- Verilog daily question (vl4 shift operation and multiplication)
- Differences between CNSA and CASC and CASIC
猜你喜欢

SNAT、DNAT 防火墙规则的备份和还原

利用SQL Server代理作业对数据库进行定时还原

Selection and application of inductors in high speed circuits

LNMP source code compilation and installation

Janus series article 3 API usage guide videoroom creating a new video room

我为什么选择使用Go语言?

net框架

Verilog 每日一题(VL29 单端口RAM)

Redis源码剖析,狠狠地拿捏了,赶紧码住

零基础利用Unity3D开发AR应用并远程下载3D模型
随机推荐
2021 年全国大学生数据统计与分析竞赛
异步FIFO基本原理(基于Verilog的简单实现)
Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
Export word according to the template, generate compound format tables and variable column tables
Zero foundation uses unity3d to develop AR applications and download 3D models remotely
【atlas】atlas 编译报错整理(全)
Goweb开发之Iris框架实战:项目总结与回顾
【impala】【报错解决】 Impala cannot read or execute the parent directory of dfs.domain.socket.path的解决方法
堡垒机的作用
Linear algebra and matrix theory (VIII)
Visual Studio 2012/2015发布Web应用连同.cs源码一起发布
深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向
MySQL数据库增删改查(基础操作命令详解)
The actual combat of the beego framework of goweb development: Section III program execution process analysis
2021年4月份自考
The actual combat of the beego framework of goweb development: Section III program execution process analysis
Convert multidimensional object array to one-dimensional array
Asynchronous circuit design -- principle and example of synchronous pulser
Andthen of function interface
线性代数及矩阵论(七)