C#给PDF文件添加水印

作者:DODONG 时间:2023-11-10 17:08:36 

本文实例为大家分享了C#添加PDF文件水印的具体代码,供大家参考,具体内容如下


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using iTextSharp.text;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Drawing;

//itextsharp.dll version:5.1.10

protected void Button1_Click(object sender, EventArgs e)
 {
 string source =@"D:\My.Sample\C#NET\Exoport2PDF\Web2\Chap1011.pdf"; //模板路径
 string output = @"D:\My.Sample\C#NET\Exoport2PDF\Web2\Chap1012.pdf"; //导出水印背景后的PDF
 string watermark = @"D:\My.Sample\C#NET\Exoport2PDF\Web2\gp0.jpg"; // 水印图片

bool isSurrcess = PDFWatermark(source, output, watermark, 10, 10);

}

public bool PDFWatermark(string inputfilepath, string outputfilepath, string ModelPicName, float top, float left)

{
 //throw new NotImplementedException();
 PdfReader pdfReader = null;
 PdfStamper pdfStamper = null;
 try
 {
  pdfReader = new PdfReader(inputfilepath);

int numberOfPages = pdfReader.NumberOfPages;

iTextSharp.text.Rectangle psize = pdfReader.GetPageSize(1);

float width = psize.Width;

float height = psize.Height;

pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));

PdfContentByte waterMarkContent;

iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(ModelPicName);

image.GrayFill = 20;//透明度,灰色填充
  //image.Rotation//旋转
  //image.RotationDegrees//旋转角度
  //水印的位置
  if (left < 0)
  {
   left = width - image.Width + left;
  }

image.SetAbsolutePosition(left, (height - image.Height) - top);

//每一页加水印,也可以设置某一页加水印
  for (int i = 1; i <= numberOfPages; i++)
  {
   waterMarkContent = pdfStamper.GetUnderContent(i);

waterMarkContent.AddImage(image);
  }
  //strMsg = "success";
  return true;
 }
 catch (Exception ex)
 {
   ex.Message.Trim();
  return false;
 }
 finally
 {

if (pdfStamper != null)
   pdfStamper.Close();

if (pdfReader != null)
   pdfReader.Close();
 }
标签:C#,PDF,水印
0
投稿

猜你喜欢

  • Android实现记住账号密码功能

    2021-10-02 01:51:24
  • 详解commons-pool2池化技术

    2021-09-10 10:56:29
  • C++ deque与vector对比的优缺点

    2023-08-28 13:19:16
  • java服务器的简单实现过程记录

    2023-01-18 06:44:34
  • Java中Request请求转发详解

    2021-05-25 20:53:42
  • 通过C#程序操作Config文件

    2023-11-30 14:24:06
  • C#实现类似新浪微博长URL转短地址的方法

    2023-06-02 12:59:28
  • 浅谈java中对集合对象list的几种循环访问

    2022-02-11 02:32:33
  • java面向对象编程类的内聚性分析

    2022-02-24 23:43:00
  • c#在程序中定义和使用自定义事件方法总结

    2022-07-12 01:45:30
  • Android 文件读写操作方法总结

    2023-12-22 22:52:29
  • java中unicode和中文相互转换的简单实现

    2021-12-18 09:46:17
  • C#中list用法实例

    2022-07-02 13:57:00
  • 浅谈Java解释器模式

    2021-08-23 23:45:59
  • java多线程Future和Callable类示例分享

    2021-09-02 09:49:37
  • java实现支付宝退款功能

    2021-09-25 14:00:00
  • java并发编程专题(三)----详解线程的同步

    2022-03-18 05:35:42
  • 使用android隐藏api实现亮度调节的方法

    2022-10-16 23:37:36
  • 详解C++ bitset用法

    2022-10-30 08:57:16
  • Android 自定义组件成JAR包的实现方法

    2023-04-08 17:56:39
  • asp之家 软件编程 m.aspxhome.com