歡迎您光臨本站 註冊首頁

c#添加圖片、文本水印到PDF文件

←手機掃碼閱讀     ml5rwbikls @ 2020-06-12 , reply:0

概述

一般我們在向文檔添加水印時,會分為直接添加文字水印和加載圖片添加圖片水印兩種情況。常見的,在添加文字水印時會多以聲明文檔版權、權威性的文字、標語或者名稱等;同樣的,圖片水印也通常可以是某組織的LOGO、印章、或者其他能夠指示性的圖片等。在下面的文檔中,將介紹通過C#編程來添加PDF水印的方法,包括:

1 添加文本水印

2 添加圖片水印

使用工具

Spire.PDF for .NET

C#代碼示例(供參考)

【示例1】添加PDF文本水印

  using Spire.Pdf;  using Spire.Pdf.Annotations;  using Spire.Pdf.Annotations.Appearance;  using Spire.Pdf.Graphics;  using System;  using System.Drawing;  namespace TextWatermark   {      class Program      {           static void Main(string[] args)       {       //創建PdfDocument對象             PdfDocument pdf = new PdfDocument();       //加載現有PDF文檔       pdf.LoadFromFile("sample.pdf");              //創建True Type字體             PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("宋體", 20f), true);       //水印文字             string text = "版權所有 侵權必究";              //測量文字所佔的位置大小,即高寬             SizeF fontSize = font.MeasureString(text);              //計算兩個偏移量             float offset1 = (float)(fontSize.Width * System.Math.Sqrt(2) / 4);             float offset2 = (float)(fontSize.Height * System.Math.Sqrt(2) / 4);       //遍歷文檔每一頁             foreach (PdfPageBase page in pdf.Pages)             {                 //創建PdfTilingBrush對象                 PdfTilingBrush brush = new PdfTilingBrush(new SizeF(page.Canvas.Size.Width / 2, page.Canvas.Size.Height / 2));         //設置畫刷透明度                 brush.Graphics.SetTransparency(0.8f);                  //將畫刷中座標系向右下平移                 brush.Graphics.TranslateTransform(brush.Size.Width / 2 - offset1 - offset2, brush.Size.Height / 2 + offset1 - offset2);         //將座標系逆時針旋轉45度                 brush.Graphics.RotateTransform(-45);                  //在畫刷上繪製文本                 brush.Graphics.DrawString(text, font, PdfBrushes.DarkGray, 0, 0);                  //在PDF頁面繪製跟頁面一樣大小的矩形,並使用定義的畫刷填充                 page.Canvas.DrawRectangle(brush, new RectangleF(new PointF(0, 0), page.Canvas.Size));              }              //保存文檔             pdf.SaveToFile("output.pdf");             System.Diagnostics.Process.Start("output.pdf");               }       }   }

 

完成代碼後,調試運行程序,生成文檔,如下:

注:如果只想設置單頁的水印效果,只需獲取指定頁,並添加水印效果即可。

【示例2】添加PDF圖片水印

  using Spire.Pdf;  using System.Drawing;   namespace ImageWaterMark  {     class Program     {      static void Main(string[] args)        {        //創建PdfDocument對象        PdfDocument pdf = new PdfDocument();        //加載現有PDF文檔              pdf.LoadFromFile("sample.pdf");               //加載圖片到System.Drawing.Image對象              Image image = Image.FromFile("logo.png");               //遍歷文檔每一頁              foreach (PdfPageBase page in pdf.Pages)              {                 //設置背景圖的位置及大小                 page.BackgroundRegion = new RectangleF((page.ActualSize.Width - 250) / 2, (page.ActualSize.Height - 250) / 2, 250, 250);                 //設置背景圖                 page.BackgroundImage = image;              }               //保存並打開文檔              pdf.SaveToFile("output.pdf");              System.Diagnostics.Process.Start("output.pdf");          }       }   }

 

測試結果:

以上是本次關於C#添加PDF水印的全部內容。

 

                                                     

   


[ml5rwbikls ] c#添加圖片、文本水印到PDF文件已經有236次圍觀

http://coctec.com/docs/c/language/show-post-238173.html