NuGet을 통해 설치됨
PM> Install-Package Spire.PDF
관련된 링크들
PDF 문서가 무단으로 사용되는 것을 방지하기 위해 문서에 텍스트나 이미지를 워터마크할 수 있습니다. 이 문서에서는 프로그래밍 방식으로 PDF에 텍스트 워터마크(한 줄 및 여러 줄 워터마크) 추가 Spire.PDF for .NET 사용하여 C# 및 VB.NET에서.
Spire.PDF for .NET 설치
먼저 Spire.PDF for .NET 패키지에 포함된 DLL 파일을 .NET 프로젝트의 참조로 추가해야 합니다. DLL 파일은 이 링크 에서 다운로드하거나 NuGet을 통해 설치할 수 있습니다.
- Package Manager
PM> Install-Package Spire.PDF
PDF에 텍스트 워터마크 추가
Spire.PDF는 PDF 파일의 워터마크를 처리하기 위한 인터페이스나 클래스를 제공하지 않습니다. 그러나 워터마크 효과를 모방하기 위해 각 페이지에 "기밀", "내부용" 또는 "초안"과 같은 텍스트를 그릴 수 있습니다. 다음은 PDF 문서의 모든 페이지에 텍스트 워터마크를 추가하는 주요 단계입니다.
- PdfDocument 개체를 만들고 PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 문서를 로드합니다.
- PdfTrueTypeFont 개체를 만들고 워터마크 텍스트를 지정하고 PdfFontBase.MeasureString() 메서드를 사용하여 텍스트 크기를 측정합니다.
- 문서의 모든 페이지를 탐색합니다.
- PdfPageBase.Canvas.TraslateTransform() 메서드를 사용하여 특정 페이지의 좌표계를 지정된 좌표로 변환하고 PdfPageBase.Canvas.RotateTransform() 메서드를 사용하여 좌표계를 시계 반대 방향으로 45도 회전합니다. 이렇게 하면 워터마크가 페이지 중앙에 45도 각도로 나타납니다.
- PdfPageBase.Canvas.DrawString() 메서드를 사용하여 페이지에 워터마크 텍스트를 그립니다.
- PdfDocument.SaveToFile() 메서드를 사용하여 문서를 다른 PDF 파일로 저장합니다.
- C#
- VB.NET
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddTextWatermarkToPdf
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument object
PdfDocument pdf = new PdfDocument();
//Load a sample PDF document
pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Create a PdfTrueTypeFont object
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 50f), true);
//Set the watermark text
string text = "CONFIDENTIAL";
//Measure the text size
SizeF textSize = font.MeasureString(text);
//Calculate the values of two offset variables,
//which will be used to calculate the translation amount 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 the pages in the document
foreach (PdfPageBase page in pdf.Pages)
{
//Set the page transparency
page.Canvas.SetTransparency(0.8f);
//Translate the coordinate system by specified coordinates
page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2);
//Rotate the coordinate system 45 degrees counterclockwise
page.Canvas.RotateTransform(-45);
//Draw watermark text on the page
page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0);
}
//Save the 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)
'Create a PdfDocument object
Dim pdf As PdfDocument = New PdfDocument()
'Load a sample PDF document
pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
'Create a PdfTrueTypeFont object
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial",50f),True)
'Set the watermark text
Dim text As String = "CONFIDENTIAL"
'Measure the text size
Dim textSize As SizeF = font.MeasureString(text)
'Calculate the values of two offset variables,
'which will be used to calculate the translation amount 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 the pages in the document
Dim page As PdfPageBase
For Each page In pdf.Pages
'Set the page transparency
page.Canvas.SetTransparency(0.8f)
'Translate the coordinate system by specified coordinates
page.Canvas.TranslateTransform(page.Canvas.Size.Width / 2 - offset1 - offset2, page.Canvas.Size.Height / 2 + offset1 - offset2)
'Rotate the coordinate system 45 degrees counterclockwise
page.Canvas.RotateTransform(-45)
'Draw watermark text on the page
page.Canvas.DrawString(text, font, PdfBrushes.DarkGray, 0, 0)
Next
'Save the changes to another file
pdf.SaveToFile("TextWatermark.pdf")
End Sub
End Class
End Namespace

PDF에 여러 줄 텍스트 워터마크 추가
문서에 두 줄 이상의 텍스트 워터마크를 추가하려는 경우가 있습니다. 타일 워터마크 효과를 얻으려면 그래픽 영역을 채우기 위해 반복되는 타일 패턴을 생성하는 PdfTilingBrush 클래스를 사용할 수 있습니다. 다음은 PDF 문서에 여러 줄 워터마크를 추가하는 주요 단계입니다.
- PdfDocument 개체를 만들고 PdfDocument.LoadFromFile() 메서드를 사용하여 샘플 PDF 문서를 로드합니다.
- 사용자 지정 메서드 InsertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum)를 만들어 여러 줄 텍스트 워터마크를 PDF 페이지에 추가합니다. 매개변수 rowNum 및 columnNum은 타일 워터마크의 행 및 열 번호를 지정합니다.
- 문서의 모든 페이지를 탐색하고 사용자 지정 메서드 InsertMultiLineTextWatermark()를 호출하여 각 페이지에 워터마크를 적용합니다.
- PdfDocument.SaveToFile() 메서드를 사용하여 문서를 다른 파일로 저장합니다.
- C#
- VB.NET
using System;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using System.Drawing;
namespace AddMultiLineTextWatermark
{
class Program
{
static void Main(string[] args)
{
//Create a PdfDocument instance
PdfDocument pdf = new PdfDocument();
//Load a sample PDF document
pdf.LoadFromFile(@"C:\Users\Administrator\Desktop\sample.pdf");
//Create a PdfTrueTypeFont object
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", 20f), true);
//Traverse all the pages
for (int i = 0; i < pdf.Pages.Count; i++)
{
//Call InsertMultiLineTextWatermark() method to add text watermarks to the specified page
InsertMultiLineTextWatermark(pdf.Pages[i], "E-ICEBLUE CO LTD", font, 3, 3);
}
//Save the document to another file
pdf.SaveToFile("MultiLineTextWatermark.pdf");
}
//Create a custom method to insert multi-line text watermarks to a page
static void InsertMultiLineTextWatermark(PdfPageBase page, String watermarkText, PdfTrueTypeFont font, int rowNum, int columnNum)
{
//Measure the text size
SizeF textSize = font.MeasureString(watermarkText);
//Calculate the values of two offset variables, which will be used to calculate the translation amount of 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.3f);
brush.Graphics.Save();
brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2);
brush.Graphics.RotateTransform(-45);
//Draw watermark text on the tile brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0);
brush.Graphics.Restore();
//Draw a rectangle (that covers the whole page) using the tile brush
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)
'Create a PdfDocument instance
Dim pdf As PdfDocument = New PdfDocument()
'Load a sample PDF document
pdf.LoadFromFile("C:\Users\Administrator\Desktop\sample.pdf")
'Create a PdfTrueTypeFont object
Dim font As PdfTrueTypeFont = New PdfTrueTypeFont(New Font("Arial",20f),True)
'Traverse all the pages
Dim i As Integer
For i = 0 To pdf.Pages.Count- 1 Step i + 1
'Call InsertMultiLineTextWatermark() method to add text watermarks to the specified page
InsertMultiLineTextWatermark(pdf.Pages(i), "E-ICEBLUE CO LTD", font, 3, 3)
Next
'Save the document to another file
pdf.SaveToFile("MultiLineTextWatermark.pdf")
End Sub
'Create a custom method to insert multi-line text watermarks to a page
Shared Sub InsertMultiLineTextWatermark(ByVal page As PdfPageBase, ByVal watermarkText As String, ByVal font As PdfTrueTypeFont, ByVal rowNum As Integer, ByVal columnNum As Integer)
'Measure the text size
Dim textSize As SizeF = font.MeasureString(watermarkText)
'Calculate the values of two offset variables, which will be used to calculate the translation amount of 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)
'Create a tile brush
Dim brush As PdfTilingBrush = New PdfTilingBrush(New SizeF(page.ActualSize.Width / columnNum,page.ActualSize.Height / rowNum))
brush.Graphics.SetTransparency(0.3f)
brush.Graphics.Save()
brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2)
brush.Graphics.RotateTransform(-45)
'Draw watermark text on the tile brush
brush.Graphics.DrawString(watermarkText, font, PdfBrushes.Violet, 0, 0)
brush.Graphics.Restore()
'Draw a rectangle (that covers the whole page) using the tile brush
page.Canvas.DrawRectangle(brush, New RectangleF(New PointF(0, 0), page.ActualSize))
End Sub
End Class
End Namespace

임시 면허 신청
생성된 문서에서 평가 메시지를 제거하거나 기능 제한을 제거하려면 다음을 수행하십시오 30일 평가판 라이선스 요청 자신을 위해.