如何在现有pdf文件上添加水印

How to add watermark on existing pdf file

我正在尝试使用 PdfSharp 在 pdf 文件上添加水印,我尝试过 link

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

但我无法了解如何获取现有的 pdf 文件页面对象以及如何在该页面上添加水印。

帮忙?

基本上,示例只是片段。您可以下载源代码并获得大量示例,包括这个水印示例。

以下来自PDFSharp-MigraDocFoundation-1_32/PDFsharp/samples/Samples C#/Based on GDI+/Watermark/Program.cs

非常简单,真的...我只展示遍历每一页的 for 循环之前的代码。你应该看看完整的文件。

  [...]
  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];
  [...]