How to Add Text Watermark in PDF Document
Digital watermarks are usually used to help secure critical documents, whether they're business documents, creative property, or personal records. Mostly watermarks are text or an image logo which identifies the owner of the content. Watermark is one of the most suitable methods for most of us to protect our files including PDF documents. However, we can also use watermark as other usages like to be a nice background with information. Here we are talking about how to use C# add text Watermark in PDF document.
Tools: Visual Studio, Spire.PDF, basic programming knowledge.
Main Tool Introduction:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. It does not use any external libraries and is built from scratch in C#.
Friendly Reminder: Please make sure Spire.PDF and Visual Studio are correctly installed on your system.
Instructions:
Step 1 Create a Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using Spire.Pdf;
namespace Textwatermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create PDF Document
Spire.PDF enables to create PDF documents with customized content. We can create a PDF document and set margins for later using on watermarks.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
Step 3 C# Add Text Watermark in PDF
Create a PDF page and set text watermark. Spire.PDF enables developers to set transparency, rotate and size of the watermark.
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
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("Spire.Pdf Demo",
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));
Step 4 Save and Preview
After finish designing watermarks, use the code below to save and preview.
//Save pdf file.
doc.SaveToFile("TextWaterMark.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("TextWaterMark.pdf");
Press F5 to start the whole project and preview the output PDF file:
Tools: Visual Studio, Spire.PDF, basic programming knowledge.
Main Tool Introduction:
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. It does not use any external libraries and is built from scratch in C#.
Friendly Reminder: Please make sure Spire.PDF and Visual Studio are correctly installed on your system.
Instructions:
Step 1 Create a Project
Create a C# project in Visual Studio and Add Spire.Pdf.dll as reference. The default setting of Spire.Pdf.dll is placed under "C:\Program Files\e-iceblue\Spire.Pdf\Bin”. Select assembly Spire.Pdf.dll and click OK to add it to the project.
using System;
using Spire.Pdf;
namespace Textwatermark
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Step 2 Create PDF Document
Spire.PDF enables to create PDF documents with customized content. We can create a PDF document and set margins for later using on watermarks.
//Create a pdf document.
PdfDocument doc = new PdfDocument();
//margin
PdfUnitConvertor unitCvtr = new PdfUnitConvertor();
PdfMargins margin = new PdfMargins();
margin.Top = unitCvtr.ConvertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Bottom = margin.Top;
margin.Left = unitCvtr.ConvertUnits(3.17f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point);
margin.Right = margin.Left;
Step 3 C# Add Text Watermark in PDF
Create a PDF page and set text watermark. Spire.PDF enables developers to set transparency, rotate and size of the watermark.
// Create one page
PdfPageBase page = doc.Pages.Add(PdfPageSize.A4, margin);
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("Spire.Pdf Demo",
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));
Step 4 Save and Preview
After finish designing watermarks, use the code below to save and preview.
//Save pdf file.
doc.SaveToFile("TextWaterMark.pdf");
doc.Close();
//Launching the Pdf file.
System.Diagnostics.Process.Start("TextWaterMark.pdf");
Press F5 to start the whole project and preview the output PDF file:
More about Spire.PDF
Spire.PDF is a PDF document creation component that enables your .NET/Silverlight applications to read, write and manipulate PDF documents without using Adobe Acrobat. Click to learn more…