Welcome
Guest
•
Login
PDFsharp and MigraDoc Wiki
Navigation
¶
Main Page
Random Page
Create a new Page
All Pages
Categories
Navigation Paths
File Management
Create Account
Quick Search
»
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Back
PDFsharp Sample: Watermark
Modified on 2018/07/11 11:27
by
Thomas Hövel
Categorized as
PDFsharp Samples
,
Samples
{s:navigationPrevUpNext|UnprotectDocument-sample|PDFsharpSamples|WorkOnPdfObjects-sample} This sample shows three variations how to add a watermark to an existing PDF file. {s:note}Technically the watermarks in this sample are simple graphical output. They have nothing to do with the Watermark Annotations introduced in PDF 1.6. ==PDF Output File== See the PDF file created by this sample: {s:PdfLink|Watermark-sample%2fWatermark-GDI.pdf|Watermark.pdf} (112 kB){br} This sample requires Acrobat Reader 5.0 or higher because it uses / demonstrates transparency. ==Screen Shots== Here are screen shots for the three variations of watermarks implemented in this sample (click image to enlarge): {s:ImageThumbLink|Draw watermark as text string|Watermark-sample%2fwatermark1th.png|Watermark-sample%2fwatermark1.png} {s:ImageThumbLink|Draw watermark as outlined graphical path|Watermark-sample%2fwatermark2th.png|Watermark-sample%2fwatermark2.png} {s:ImageThumbLink|Draw watermark as transparent graphical path above text|Watermark-sample%2fwatermark3th.png|Watermark-sample%2fwatermark3.png}{br} Click on the images to enlarge. ==Source Code== Here's the code that creates the first kind of watermarks: {s:beginCsharp} // Variation 1: Draw a watermark as a text string. // Get an XGraphics object for drawing beneath the existing content. var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend); // Get the size (in points) of the text. var size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page. gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a string format. var format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; // Create a dimmed red brush. XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0)); // Draw the string. gfx.DrawString(watermark, font, brush, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), format); {s:endCsharp} Here's the code that creates the second kind of watermarks: {s:beginCsharp} // Variation 2: Draw a watermark as an outlined graphical path. // NYI: Does not work in Core build. // Get an XGraphics object for drawing beneath the existing content. var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend); // Get the size (in points) of the text. var size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page. gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a graphical path. var path = new XGraphicsPath(); // Create a string format. var format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; // Add the text to the path. // AddString is not implemented in PDFsharp Core. path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), format); // Create a dimmed red pen. var pen = new XPen(XColor.FromArgb(128, 255, 0, 0), 2); // Stroke the outline of the path. gfx.DrawPath(pen, path); {s:endCsharp} Here's the code that creates the third kind of watermarks: {s:beginCsharp} // Variation 3: Draw a watermark as a transparent graphical path above text. // NYI: Does not work in Core build. // Get an XGraphics object for drawing above the existing content. var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Append); // Get the size (in points) of the text. var size = gfx.MeasureString(watermark, font); // Define a rotation transformation at the center of the page. gfx.TranslateTransform(page.Width / 2, page.Height / 2); gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI); gfx.TranslateTransform(-page.Width / 2, -page.Height / 2); // Create a graphical path. var path = new XGraphicsPath(); // Create a string format. var format = new XStringFormat(); format.Alignment = XStringAlignment.Near; format.LineAlignment = XLineAlignment.Near; // Add the text to the path. // AddString is not implemented in PDFsharp Core. path.AddString(watermark, font.FontFamily, XFontStyle.BoldItalic, 150, new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2), format); // Create a dimmed red pen and brush. var pen = new XPen(XColor.FromArgb(50, 75, 0, 130), 3); XBrush brush = new XSolidBrush(XColor.FromArgb(50, 106, 90, 205)); // Stroke the outline of the path. gfx.DrawPath(pen, brush, path); {s:endCsharp} {s:sampleSourceCode}
Meta Keywords:
Meta Description:
Change Comment:
Visit the new
Website for PDFsharp & MigraDoc Foundation 6.0 for .NET 6
and find information about the new version for Windows, Linux, and other platforms.
Miscellaneous
Home
PDFsharp
FAQ
Samples
Articles
MigraDoc
FAQ
Samples
Articles
ScrewTurn Wiki version 3.0.5.600. Some of the icons created by
FamFamFam
.
Impressum - Privacy Policy, Data Protection Declaration, Legal Notice