调用直接这样用:
//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. UICollectionView-网格视图

    1. UICollectionView 网格视图,除了提供与UITableView同样的功能显示一组顺序显示的数据,还支持多列的布局. 2. UICollectionView 使用 > 设置Co ...

  2. kibana查询语法

    单项term查询 例: 搜 Dahlen, Malone 字段field查询 field:value   例:city:Keyport, age:26 通配符 ? 匹配单个字符      例: H?b ...

  3. react 项目及视频

    项目     视频

  4. TCP中的服务端与客户端的实现

    TCP中首先要在服务端开启监听,这样才可以从客户端链接 using System; using System.Collections.Generic; using System.Linq; using ...

  5. c++ 基础知识 0001 const 知识1

    1. C++ const用法 尽可能使用const 2. C++ const 允许指定一个语义约束,编译器会强制实施这个约束,允许程序员告诉编译器某值是保持不变的.如果在编程中确实有某个值保持不变,就 ...

  6. LINUX 命令—netstat [简单实用]

    1.--当我们在检查程序是否启动或者网络状况的时候 会查看本机活跃的端口,就需要这个命令: |--"netstat – Print network connections, routing ...

  7. notebook查找文件

  8. linux操作系统安全防护

    Linux系统攻防对抗实践 一.实践内容 在Metasploit渗透攻击框架软件中寻找一个针对Linux系统服务的渗透攻击模块,在网络安全攻防实验环境中部署有漏洞的环境(如渗透利用第三方网络服务,需要 ...

  9. 在DIV里面让图片居中

    .content.itemDiv{ display: table-cell;width: 171px;height: 133px;text-align: center; vertical-align: ...

  10. 第12篇 PSR-1规范

    这个规范也不多,七点如下: 1. Overview Files MUST use only <?php and <?= tags. Files MUST use only UTF-8 wi ...