C#向pdf 添加水印
调用直接这样用:
//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 添加水印的更多相关文章
- 如何在PDF中添加水印,PDF添加水印技巧
PDF文件现在的使用很是普遍,不管是工作中还是学习中都会使用到PDF文件,制作一个PDF文件就很辛苦的,我们要是想把PDF文件中添加水印防止抄袭的时候应该要怎么做呢,其实吧PDF文件添加水印还挺简单的 ...
- PDF如何添加水印,PDF添加水印工具的使用方法
PDF文件在编辑修改的时候是需要借助工具才可以编辑,PDF文件不像普通的文件可以直接打开编辑,PDF编辑工具是PDF文件进行编辑的重要工具,就以添加水印为例,能够在PDF中添加水印的工具有哪些呢?要怎 ...
- PHP使用FPDF pdf添加水印中文乱码问题 pdf合并版本问题
---恢复内容开始--- require_once('../fpdf/fpdf.php');require_once('../fpdi/fpdi.php'); 使用此插件 pdf 合并 并添加水印 期 ...
- C#使用iTextSharp给PDF添加水印
代码: /// <summary> /// 添加普通偏转角度文字水印 /// </summary> public static void SetWatermark(string ...
- iTextSharp简单生成pdf和操作pdf添加水印
遇到需要导出页面到pdf,并添加pdf水印的业务.稍微研究了下,借阅网友的前车之鉴,经过使用可行之后的代码贴出来,做个记录,也供需要的网友借阅. public class PDFSetWaterMar ...
- C# PDF添加水印
需要iTextSharp.dll, 下载地址http://sourceforge.net/projects/itextsharp/ using System;using System.Collecti ...
- java 为pdf添加水印图片
首先需要引入两个Jar包分别是:iTextAsian.jar .itext-2.1.7.jar 可以去 http://download.csdn.net/detail/work201003/922 ...
- php pdf添加水印(中文水印,图片水印)
1.下载软件包 链接:https://pan.baidu.com/s/1cah-mf-SCtfMhVyst_sG8w&shfl=sharepset 提取码:ld8z 2.下载pdf_water ...
- PDF上添加水印
1.整合jar: itext-asian-5.2.0.jar,itextpdf-5.5.10.jar,iTextAsianCmaps.jar 下载:itext的整合jar包 2.使用方法: publi ...
随机推荐
- 移动端 HTML5 <video> 视频播放优化实践
遇到的挑战 移动端HTML5使用原生<video>标签播放视频,要做到两个基本原则,速度快和体验佳,先来分析一下这两个问题. 下载速度 以一个8s短视频为例,wifi环境下提供的高清视频达 ...
- Android界面View及ViewGroup学习 《转载》
View及ViewGroup类关系 Android View和ViewGroup从组成架构上看,似乎ViewGroup在View之上,View需要继承ViewGroup,但实际上不是这样的. View ...
- 在 Ubuntu 上搭建 Hadoop 分布式集群 Eclipse 开发环境
一直在忙Android FrameWork,终于闲了一点,利用空余时间研究了一下Hadoop,并且在自己和同事的电脑上搭建了分布式集群,现在更新一下blog,分享自己的成果. 一 .环境 1.操作系统 ...
- Java程序员进阶路线-高级java程序员养成
1. 引言 搞Java的弟兄们肯定都想要达到更高的境界,用更少的代码解决更多的问题,用更清晰的结构为可能的传承和维护做准备.想想当初自己摸着石头过河,也看过不少人介绍的学习路线,十多年走过来多少还是有 ...
- POJ1287 Networking
解题思路:Kruskal模板题,重复输入的情况,本题是无向图. 见代码: #include<cstdio> #include<algorithm> #include<cs ...
- 高级C/C++编译技术之读书笔记(四)之定位库文件
最近有幸阅读了<高级C/C++编译技术>深受启发,该书深入浅出地讲解了构建过程(编译.链接)中的各种细节,从多个角度展示了程序与库文件或代码的集成方法,提出了面向代码复用和系统集成的软件架 ...
- JQuery 提示用户名密码不为空
$(document).ready(function(){ //HTML()替换 HTML函数 //append()追加 :input = :t ...
- 《DSP using MATLAB》示例Example7.17
代码: M = 40; alpha = (M-1)/2; l = 0:M-1; wl = (2*pi/M)*l; T1 = 0.109021; T2 = 0.59417456; Hrs = [zero ...
- 转载 基于NicheStack协议栈的TCP/IP实现
一.摘要 Altera软件NIOS II高版本(7.2版本以上,本例程中使用的是9.0版本)中实现TCP/IP所用的协议栈为NicheStack,常用的例程有2个,web_server和simple_ ...
- 【3】SpringMVC的Controller
1SpringMvc的Controller是线程安全的吗? (1)由于是单例,tomcat的多线程环境访问,属性必须是不可变的,如果可变,会产生脏数据,线程不安全 2Spring的事务管理 (1)ao ...