Adding Watermark with PDFsharp

sahin kaya 20 Reputation points
2023-09-15T18:44:02.8833333+00:00

How do I add my pdf file on my computer to the code below (to add watermark to pdf) I use pdfshap sdk

// Variation 1: Draw a watermark as a text string.<font></font>
 <font></font>
// Get an XGraphics object for drawing beneath the existing content.<font></font>
var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);<font></font>
 <font></font>
// Get the size (in points) of the text.<font></font>
var size = gfx.MeasureString(watermark, font);<font></font>
 <font></font>
// Define a rotation transformation at the center of the page.<font></font>
gfx.TranslateTransform(page.Width / 2, page.Height / 2);<font></font>
gfx.RotateTransform(-Math.Atan(page.Height / page.Width) * 180 / Math.PI);<font></font>
gfx.TranslateTransform(-page.Width / 2, -page.Height / 2);<font></font>
 <font></font>
// Create a string format.<font></font>
var format = new XStringFormat();<font></font>
format.Alignment = XStringAlignment.Near;<font></font>
format.LineAlignment = XLineAlignment.Near;<font></font>
 <font></font>
// Create a dimmed red brush.<font></font>
XBrush brush = new XSolidBrush(XColor.FromArgb(128, 255, 0, 0));<font></font>
 <font></font>
// Draw the string.<font></font>
gfx.DrawString(watermark, font, brush,<font></font>
    new XPoint((page.Width - size.Width) / 2, (page.Height - size.Height) / 2),<font></font>
    format);<font></font>
Community Center Not monitored
{count} votes

1 answer

Sort by: Most helpful
  1. Olaf Helper 47,436 Reputation points
    2023-09-18T05:46:55.3366667+00:00

    Ask the author or community at https://github.com/topics/pdfsharp

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.