Skip to content

Commit 1e94819

Browse files
Merge pull request #431 from telerik/new-kb-add-watermark-pdf-radpdfprocessing-148c1282b51b44c38dd649ca2925a3e3
Added new kb article add-watermark-pdf-radpdfprocessing
2 parents 0d0baa3 + dceb258 commit 1e94819

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: Adding a Watermark to PDF Files Using RadPdfProcessing
3+
description: Learn how to add custom watermarks to PDF documents using the RadPdfProcessing library.
4+
type: how-to
5+
page_title: How to Add Watermarks to PDF Documents with RadPdfProcessing
6+
slug: add-watermark-pdf-radpdfprocessing
7+
tags: radpdfprocessing, document processing, watermark, pdf, text watermark
8+
res_type: kb
9+
ticketid: 1653970
10+
---
11+
12+
## Environment
13+
14+
| Version | Product | Author |
15+
| --- | --- | ---- |
16+
| 2024.2.426| RadPdfProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|
17+
18+
## Description
19+
20+
This KB article demonstrates how to add a text watermark across all pages of a PDF document using RadPdfProcessing.
21+
22+
## Solution
23+
24+
To add a watermark to PDF pages using RadPdfProcessing, follow these steps:
25+
26+
1. Import the PDF document using [PdfFormatProvider]({%slug radpdfprocessing-formats-and-conversion-pdf-pdfformatprovider%}).
27+
2. Iterate through each page of the document.
28+
3. Use [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%}) to add a watermark text block to each page.
29+
4. Customize the watermark's text properties, color, and position.
30+
5. [Export](https://docs.telerik.com/devtools/document-processing/libraries/radpdfprocessing/formats-and-conversion/pdf/pdfformatprovider/pdfformatprovider#export) the document with watermarks back to a PDF file.
31+
32+
Here is a complete code snippet demonstrating these steps:
33+
34+
```csharp
35+
static void Main(string[] args)
36+
{
37+
string fileName = "sample.pdf";
38+
PdfFormatProvider provider = new PdfFormatProvider();
39+
RadFixedDocument document = provider.Import(File.ReadAllBytes(fileName));
40+
41+
foreach (RadFixedPage page in document.Pages)
42+
{
43+
AddWatermarkText(page, "Watermark text!", 100);
44+
}
45+
46+
string exportFileName = "testWatermarks.pdf";
47+
File.Delete(exportFileName);
48+
49+
File.WriteAllBytes(exportFileName, new PdfFormatProvider().Export(document));
50+
ProcessStartInfo psi = new ProcessStartInfo()
51+
{
52+
FileName = exportFileName,
53+
UseShellExecute = true
54+
};
55+
Process.Start(psi);
56+
}
57+
58+
private static void AddWatermarkText(RadFixedPage page, string text, byte transparency)
59+
{
60+
FixedContentEditor editor = new FixedContentEditor(page);
61+
62+
Block block = new Block();
63+
block.TextProperties.FontSize = 80;
64+
block.TextProperties.TrySetFont(new FontFamily("Arial"), FontStyles.Normal, FontWeights.Bold);
65+
block.HorizontalAlignment = Telerik.Windows.Documents.Fixed.Model.Editing.Flow.HorizontalAlignment.Center;
66+
block.GraphicProperties.FillColor = new RgbColor(transparency, 255, 0, 0);
67+
block.InsertText(text);
68+
69+
double angle = -45;
70+
editor.Position.Rotate(angle);
71+
editor.Position.Translate(0, page.Size.Width);
72+
editor.DrawBlock(block, new Size(page.Size.Width / Math.Abs(Math.Sin(angle)), double.MaxValue));
73+
}
74+
```
75+
76+
![Pdf Watermark](images/pdf-watermark.png)
77+
78+
## See Also
79+
80+
- [FixedContentEditor]({%slug radpdfprocessing-editing-fixedcontenteditor%})
81+
- [SDK Example: Add Watermark](https://github.com/telerik/document-processing-sdk/blob/master/PdfProcessing/AddWatermark/Program.cs)
154 KB
Loading

libraries/radpdfprocessing/editing/fixedcontenteditor.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ __FixedContentEditor__ has some properties and methods that affect how it will b
300300
* [Clipping]({%slug radpdfprocessing-concepts-clipping%})
301301
* [Table]({%slug radpdfprocessing-editing-table%})
302302
* [How to Generate a PDF Document from Images with FixedContentEditor]({%slug pdf-from-images-with-fixedcontenteditor%})
303+
* [Adding a Watermark to PDF Files Using RadPdfProcessing]({%slug add-watermark-pdf-radpdfprocessing%})
303304
* [Adding Images with a Shadow in PDF Documents]({%slug add-shadow-image-radpdfprocessing%})
304305
* [Splitting a Large Image Across Multiple PDF Pages]({%slug split-export-large-image-multiple-pdf-pages-radpdfprocessing%})
305306
* [Resizing Large Images to Fit in the PDF Page]({%slug resize-images-radpdfprocessing%})

0 commit comments

Comments
 (0)