|
| 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 | + |
| 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) |
0 commit comments