调用直接这样用:
//PDFHelper.AddImageWatermarkPDF(path, "D://my.pdf", Server.MapPath("/HtmlToPdf/Tools/sy.bmp"), 0, 0);
string temp = DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
Guid guid = new Guid();
guid = Guid.NewGuid(); string tempGUID = guid.ToString();
File.Copy("E:\\1.pdf", "E:\\11.pdf", true);
PDFHelper.AddWordWatermark2PDF("E:\\11.pdf", "E:\\1.pdf", "西安咸阳国际机场南三指廊航站楼工程施工及施工管理总承包" + "_" + temp + "_" + tempGUID + "_7.1");
//PDFSetWaterMark.setWatermark("D://my.pdf", "D://my2.pdf", "TEST", "", "", 1); using iTextSharp.text;
using iTextSharp.text.pdf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace Test测试
{
public static class PDFHelper
{ /// <summary>
/// 加图片水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="ModelPicName"></param>
/// <param name="top"></param>
/// <param name="left"></param>
/// <returns></returns>
public static bool AddImageWatermarkPDF(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(); 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 = ;//透明度,灰色填充
//image.Rotation//旋转
//image.RotationDegrees//旋转角度
//水印的位置
if (left < )
{
left = width / - image.Width + left;
} //image.SetAbsolutePosition(left, (height - image.Height) - top);
image.SetAbsolutePosition(left, (height / - image.Height) - top); //每一页加水印,也可以设置某一页加水印
for (int i = ; i <= numberOfPages; i++)
{
//waterMarkContent = pdfStamper.GetUnderContent(i);//内容下层加水印
waterMarkContent = pdfStamper.GetOverContent(i);//内容上层加水印 waterMarkContent.AddImage(image);
}
//strMsg = "success";
return true;
}
catch (Exception ex)
{
throw ex; }
finally
{ if (pdfStamper != null)
pdfStamper.Close(); if (pdfReader != null)
pdfReader.Close();
}
}
/// <summary>
/// 添加普通偏转角度文字水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="permission"></param>
public static void AddWordWatermark2PDF(string inputfilepath, string outputfilepath, string waterMarkName)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
int total = pdfReader.NumberOfPages + ;
iTextSharp.text.Rectangle psize = pdfReader.GetPageSize();
float width = psize.Width;
float height = psize.Height;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = ; i < total; i++)
{
content = pdfStamper.GetOverContent(i);//在内容上方加水印
//content = pdfStamper.GetUnderContent(i);//在内容下方加水印
//透明度
gs.FillOpacity = 0.5f;
content.SetGState(gs);
content.SetGrayFill(0.5f);
//开始写入文本
content.BeginText();
//content.SetColorFill(BaseColor.LIGHT_GRAY);
//content.SetFontAndSize(font, 100);
//content.SetTextMatrix(0, 0);
//content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / 2 - 50, height / 2 - 50, 55);
content.SetColorFill(BaseColor.GRAY);
content.SetFontAndSize(font, );
content.ShowTextAligned(Element.ALIGN_CENTER, waterMarkName, width / - , height / - , );
content.EndText();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{ if (pdfStamper != null)
pdfStamper.Close(); if (pdfReader != null)
pdfReader.Close();
}
} /// <summary>
/// 添加倾斜水印
/// </summary>
/// <param name="inputfilepath"></param>
/// <param name="outputfilepath"></param>
/// <param name="waterMarkName"></param>
/// <param name="userPassWord"></param>
/// <param name="ownerPassWord"></param>
/// <param name="permission"></param>
public static void setWatermark(string inputfilepath, string outputfilepath, string waterMarkName, string userPassWord, string ownerPassWord, int permission)
{
PdfReader pdfReader = null;
PdfStamper pdfStamper = null;
try
{
pdfReader = new PdfReader(inputfilepath);
pdfStamper = new PdfStamper(pdfReader, new FileStream(outputfilepath, FileMode.Create));
// 设置密码
//pdfStamper.SetEncryption(false,userPassWord, ownerPassWord, permission); int total = pdfReader.NumberOfPages + ;
PdfContentByte content;
BaseFont font = BaseFont.CreateFont(@"C:\WINDOWS\Fonts\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfGState gs = new PdfGState();
gs.FillOpacity = 0.2f;//透明度 int j = waterMarkName.Length;
char c;
int rise = ;
for (int i = ; i < total; i++)
{
rise = ;
content = pdfStamper.GetOverContent(i);//在内容上方加水印
//content = pdfStamper.GetUnderContent(i);//在内容下方加水印 content.BeginText();
content.SetColorFill(BaseColor.DARK_GRAY);
content.SetFontAndSize(font, );
// 设置水印文字字体倾斜 开始
if (j >= )
{
content.SetTextMatrix(, );
for (int k = ; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= ;
}
}
else
{
content.SetTextMatrix(, );
for (int k = ; k < j; k++)
{
content.SetTextRise(rise);
c = waterMarkName[k];
content.ShowText(c + "");
rise -= ;
}
}
// 字体设置结束
content.EndText();
// 画一个圆
//content.Ellipse(250, 450, 350, 550);
//content.SetLineWidth(1f);
//content.Stroke();
} }
catch (Exception ex)
{
throw ex;
}
finally
{ if (pdfStamper != null)
pdfStamper.Close(); if (pdfReader != null)
pdfReader.Close();
}
} } }

C#向pdf 添加水印的更多相关文章

  1. 如何在PDF中添加水印,PDF添加水印技巧

    PDF文件现在的使用很是普遍,不管是工作中还是学习中都会使用到PDF文件,制作一个PDF文件就很辛苦的,我们要是想把PDF文件中添加水印防止抄袭的时候应该要怎么做呢,其实吧PDF文件添加水印还挺简单的 ...

  2. PDF如何添加水印,PDF添加水印工具的使用方法

    PDF文件在编辑修改的时候是需要借助工具才可以编辑,PDF文件不像普通的文件可以直接打开编辑,PDF编辑工具是PDF文件进行编辑的重要工具,就以添加水印为例,能够在PDF中添加水印的工具有哪些呢?要怎 ...

  3. PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题

    ---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...

  4. C#使用iTextSharp给PDF添加水印

    代码: /// <summary> /// 添加普通偏转角度文字水印 /// </summary> public static void SetWatermark(string ...

  5. iTextSharp简单生成pdf和操作pdf添加水印

    遇到需要导出页面到pdf,并添加pdf水印的业务.稍微研究了下,借阅网友的前车之鉴,经过使用可行之后的代码贴出来,做个记录,也供需要的网友借阅. public class PDFSetWaterMar ...

  6. C# PDF添加水印

    需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/ using System;using System.Collecti ...

  7. java 为pdf添加水印图片

    首先需要引入两个Jar包分别是:iTextAsian.jar .itext-2.1.7.jar  可以去  http://download.csdn.net/detail/work201003/922 ...

  8. php pdf添加水印(中文水印,图片水印)

    1.下载软件包 链接:https://pan.baidu.com/s/1cah-mf-SCtfMhVyst_sG8w&shfl=sharepset 提取码:ld8z 2.下载pdf_water ...

  9. PDF上添加水印

    1.整合jar: itext-asian-5.2.0.jar,itextpdf-5.5.10.jar,iTextAsianCmaps.jar 下载:itext的整合jar包 2.使用方法: publi ...

随机推荐

  1. 条款1:将c++视作一个语言联邦

    c++是一个多重泛型编程语言,其所支持的泛型有: 面向过程编程(procedual) 面向对象编程(object-oriented) 面向函数编程(functional) 泛型编程(generic) ...

  2. position值详情

    1.absolute属性: 英文直译成汉语的解释有:绝对,独立的.显然在做为css中position属性的值时“绝对”这个意思更恰当一些.他的作用是:生成绝对定位的元素,相对于static 定位以外的 ...

  3. Puzzle Game HihoCoder - 1634

    题目链接:https://cn.vjudge.net/problem/HihoCoder-1634 题目意思:可以让矩阵里的某一个数变成p,或者不修改.求最大子矩阵最小,输出最小值. 思路:请看下图 ...

  4. Web目录结构

    /: Web应用的跟目录,该目录下所有文件在客户端都可以访问(JSP,HTML) /WEB-INF: 存放应用使用的各种资源.该目录及其子目录对客户端都是不可以访问的, 其中包括web.xml(部署表 ...

  5. Java中数据库连接池原理机制的详细讲解(转)

    连接池的基本工作原理 1.基本概念及原理 由上面的分析可以看出,问题的根源就在于对数据库连接资源的低效管理.我们知道,对于共享资源,有一个很著名的设计模式:资源池 (Resource Pool).该模 ...

  6. UVA11174 Stand in a Line

    题意 PDF 分析 \[ f(i)=f(c_1)f(c_2)\dots\times(s(i)-1)!/(s(c_1)!s(c_2)! \dots s(c_k)! )\\ f(root)=(s(root ...

  7. Python的Django框架中if标签的相关使用

    {% if today_is_weekend%} {% end if %} 系统会显示在这之间的内容 {% else %}标签是可选的 在python和django模板系统中,以下对象相当于布尔值的F ...

  8. hadoop之 Zookeeper 分布式应用程序协调服务

    (1) Zookeeper 在 Hadoop 集群中的作用 Zookeeper 是分布式管理协作框架,Zookeeper 集群用来保证 Hadoop 集群的高可用,(高可用的含义是:集群中就算有一部分 ...

  9. asp select count(*) 用 open还是excute

    dSql1="select count(*) from test_hist where uid="&cid  'dRs1.open dSql1,tConn,1,1  'dS ...

  10. ajax()函数传值中文乱码解决方法介绍

    jquery的ajax()函数传值中文乱码解决方法介绍,需要的朋友可以参考下 复制代码 代码如下: $.ajax({ dataType : ‘json',type : ‘POST',url : ‘ht ...