MK3007
MK3007

Reputation: 107

How to add watermark on existing pdf file

I am trying to add watermark on pdf file using PdfSharp, I tried from this link

http://www.pdfsharp.net/wiki/Watermark-sample.ashx

but am not able to get how to get the existing pdf file page object and how to watermark on that page.

Help?

Upvotes: 0

Views: 2535

Answers (1)

thecarpy
thecarpy

Reputation: 863

Basically, the samples are only snippets. You can download the source and with that you get a bunch of samples, including this watermark example.

The following comes from PDFSharp-MigraDocFoundation-1_32/PDFsharp/samples/Samples C#/Based on GDI+/Watermark/Program.cs

Quite simple, really ... I am only showing the code up to the for loop that goes over each page. You should have a look at the full file.

  [...]
  const string watermark = "PDFsharp";
  const int emSize = 150;

  // Get a fresh copy of the sample PDF file
  const string filename = "Portable Document Format.pdf";
  File.Copy(Path.Combine("../../../../../PDFs/", filename),
    Path.Combine(Directory.GetCurrentDirectory(), filename), true);

  // Create the font for drawing the watermark
  XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);

  // Open an existing document for editing and loop through its pages
  PdfDocument document = PdfReader.Open(filename);

  // Set version to PDF 1.4 (Acrobat 5) because we use transparency.
  if (document.Version < 14)
    document.Version = 14;

  for (int idx = 0; idx < document.Pages.Count; idx++)
  {
    //if (idx == 1) break;
    PdfPage page = document.Pages[idx];
  [...]

Upvotes: 1

Related Questions