首页 文章

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

提问于
浏览
-1

我正在尝试使用PdfSharp在pdf文件上添加水印,我尝试从此链接

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

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

救命?

1 回答

  • 1

    基本上,样本只是片段 . 您可以下载源代码,然后获得大量示例,包括此水印示例 .

    以下来自 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];
      [...]
    

相关问题