C# 使用 itextsharp
C# 使用 itextsharp,版本 5.5.12.0
目录
- 一、添加水印
- 二、字节流下载到本地
- 三、将文档设置为只读
- 四、拆分PDF
- 五、合并PDF
一、添加水印
参数inContent可以是byte,也可以是string类型文本路径。
using System;using iTextSharp.text.pdf;using iTextSharp.text;using System.IO;/// <summary>/// 添加文本水印/// </summary>/// <param name="Content">文本字节流</param>/// <param name="shuiyin">想要添加的水印</param>/// <returns></returns>public static byte[] AddTextWatermark(byte[] Content, string shuiyin){using (PdfReader reader = new PdfReader(Content)){using (MemoryStream outputStream = new MemoryStream()){// 加完水印的文件PdfStamper pdfStamper = new PdfStamper(reader, outputStream);int pageNumber = reader.NumberOfPages + 1;PdfContentByte content;//创建字体BaseFont font = BaseFont.CreateFont(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts),"STKAITI.TTF"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);// 设置水印透明度PdfGState gs = new PdfGState();// 设置填充字体不透明度为0.4fgs.FillOpacity = (0.2f);int textH = 10;int textW = 100;iTextSharp.text.Rectangle pageRect;// 循环对每页插入水印for (int i = 1; i < pageNumber; i++){pageRect = reader.GetPageSizeWithRotation(i);// 水印在之前文本下content = pdfStamper.GetUnderContent(i);// 开始content.BeginText();// 设置水印字体参数及大小 (字体参数,字体编码格式,是否将字体信息嵌入到pdf中(一般不需要嵌入),字体大小)content.SetFontAndSize(font, 15);// 设置透明度content.SetGState(gs);// 设置水印对齐方式 水印内容 X坐标 Y坐标 旋转角度for (int height = textH; height < pageRect.Height * 2; height = height + textH * 20){for (int width = textW; width < pageRect.Width * 1.5 + textW; width = width + textW * 2){// rotation:倾斜角度content.ShowTextAligned(Element.ALIGN_LEFT, shuiyin, width - textW, height - textH, 45);}}// 设置水印颜色(灰色)content.SetColorFill(BaseColor.GRAY);//结束content.EndText();}pdfStamper.Close();return outputStream.ToArray();}}}
二、字节流下载到本地
public void Download(byte[] byteDatas){PdfReader reader = new PdfReader(byteDatas);int n = reader.NumberOfPages;Rectangle pagesize = reader.GetPageSize(1);Document document = new Document(pagesize);string dest = @"D:\itextsharp\cs.pdf";//文件下载地址FileStream stream = new FileStream(dest, FileMode.Create);PdfCopy copy = new PdfCopy(document, stream);document.Open();//写文件for (int i = 1; i <= n; i++){PdfImportedPage page = copy.GetImportedPage(reader, i);copy.AddPage(page);}document.Close();}
三、将文档设置为只读
public void Encryption(){string dest = @"D:\itextsharp\cs.pdf";FileStream fs = new FileStream(dest, FileMode.Open);//获取文件大小long size = fs.Length;byte[] array = new byte[size];//将文件读到byte数组中fs.Read(array, 0, array.Length);fs.Close();//pdf转成字节流PdfReader reader = new PdfReader(array);int n = reader.NumberOfPages;Rectangle pagesize = reader.GetPageSize(1);Document document = new Document(pagesize);FileStream stream = new FileStream(dest, FileMode.Create);PdfCopy copy = new PdfCopy(document, stream);byte[] mw = Encoding.Default.GetBytes("123456");//设置文档权限密码copy.SetEncryption(null, mw, PdfWriter.AllowCopy | PdfWriter.AllowPrinting, PdfWriter.STANDARD_ENCRYPTION_40);//加密必须放在文档打开之前document.Open();//写文件for (int i = 1; i <= n; i++){PdfImportedPage page = copy.GetImportedPage(reader, i);copy.AddPage(page);}document.Close();}
四、拆分PDF
下载PDF时按页拆分,可以按页循环下载
PdfReader reader = new PdfReader(byteDatas);
int n = reader.NumberOfPages;
Rectangle pagesize = reader.GetPageSize(1);
Document document = new Document(pagesize);for (int i = 1; i <= n; i++){string dest = PathTool.CombinConfig("DownloadFile\\cs" + i + ".pdf"); //拆分后的PDF路径FileStream stream = new FileStream(dest, FileMode.Create);PdfCopy copy = new PdfCopy(document, stream);document.Open();PdfImportedPage page = copy.GetImportedPage(reader, i);copy.AddPage(page);}
document.Close();
五、合并PDF
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.Collections.Generic;
using System.IO;public static void MergePdfFiles(List<string> fileList, string outMergeFile){string dest = @"D:\itextsharp\cs.pdf";//合并后PDF路径PdfReader reader;List<PdfReader> readerList = new List<PdfReader>();Document document = new Document();PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(dest, FileMode.Create));document.Open();PdfContentByte cb = writer.DirectContent;PdfImportedPage newPage;//循环要合并的PDFfor (int i = 0; i < fileList.Count; i++){reader = new PdfReader(fileList[i]);int iPageNum = reader.NumberOfPages;for (int j = 1; j <= iPageNum; j++){document.NewPage();newPage = writer.GetImportedPage(reader, j);cb.AddTemplate(newPage, 0, 0);}readerList.Add(reader);}document.Close();foreach (var rd in readerList)//清理占用{rd.Dispose();}}
C# 使用 itextsharp相关推荐
- 一些关于iText和iTextSharp的旧闻(some old news about iText and iTextSharp)
Google Calendar使用iText来输出PDF(iText used in Google Calendar) The calendars are generated as PDF files ...
- ITextSharp使用说明
ITextSharp是一个生成Pdf文件的开源项目,最近在项目中有使用到这个项目,对使用中的经验作一个小结. ITextSharp中相关的概念: 一.Document 这个对象有三个构造函数: 隐藏行 ...
- 通过iTextSharp为PDF添加带有超链接的Bookmark
最近有这样一个需求,即为PDF加入带有超链接的Bookmark.PDF的开发有个特点,就是虽然相关的开发工具很多,但大都是收费的,PDFOne就是这么一个PDF开发组件,接口调用很简单,但是需要收费, ...
- 【译】在Asp.Net中操作PDF – iTextSharp -利用块,短语,段落添加文本
本篇文章是讲述使用iTextSharp这个开源组件的系列文章的第三篇,iTextSharp可以通过Asp.Net创建PDFs,就像HTML和ASP.Net为文本提供了多种容器一样,iTextSharp ...
- 在.NET中使用iTextSharp创建/读取PDF报告: Part I [翻译]
原文地址:Create/Read Advance PDF Report using iTextSharp in C# .NET: Part I By Debopam Pal, 27 Nov 20 ...
- c# 使用 itextsharp 实现生成Pdf报表
由于项目需要,所以学习Itextsharp 此项目需求是 某一角色提交申请,然后从后台查出数据生成pdf报表 打印出来用于查看 以下是代码: string sql = "select ...
- iTextSharp.text.Rectangle 使用方法说明
iTextSharp.text.Rectangle 使用过程中,由于这方面的中文资料较少,带来了很多不便,消耗了工作时间, iTextSharp.text.Rectangle 的构造函数如下: pub ...
- itextsharp php,C#_C#使用iTextSharp设置PDF所有页面背景图功能实例,本文实例讲述了C#使用iTextSharp - phpStudy...
C#使用iTextSharp设置PDF所有页面背景图功能实例 本文实例讲述了C#使用iTextSharp设置PDF所有页面背景图功能的方法.分享给大家供大家参考.具体如下: 在生成PDF 的时候,虽然 ...
- HTML转PDF(C#---itextsharp)(转自别人的文章)
HTML转PDF(C#---itextsharp) HTML转PDF(C#---itextsharp) 一. 需求:将HTML转PDF打印.Web项目中总是有这样的需求,很是让人苦恼. 二. 分析:如 ...
- ASP.Net MVC——使用 ITextSharp 完美解决HTML转PDF(中文也可以)
前言: 最近在做老师交代的一个在线写实验报告的小项目中,有这么个需求:把学生提交的实验报告(HTML形式)直接转成PDF,方便下载和打印. 以前都是直接用rdlc报表实现的,可这次牵扯到图片,并且更为 ...
最新文章
- webGL简单例子(klayge)
- golang变量定义细节及beego环境搭建细节记录
- 小白创建网站的曲折之路
- 使用Beautifulsoup去除特定标签
- Spring MVC学习总结(14)——SpringMVC测试框架之mockMVC详解
- php diff 文本比较,php文本操作方法集合比较
- 有关不平衡学习与SMOTE算法
- 如何下载历史版本和最新版本的iar
- 德国《世界报》:在上海车牌与小汽车一样贵
- 生命与负熵---宇宙的心弦
- linux fastQC 操作命令,Linux shell合并fastq测序数据/批量fastqc小脚本|merge|multiqc
- Quadro P5200 - 最强大的移动工作站显卡 专门为了惠普 VR Z 背包电脑而发布
- 什么叫超融合基础架构?
- 什么是雪崩什么是击穿?
- 运算放大器的交流放大分析
- STM32F7 使用 FAL 配置片上FLASH
- JavaScript 循环控制语句
- 什么是内聚 内聚是什么 内聚的种类有哪些
- win10局域网硬盘文件访问
- 构造函数的作用,特点, 种类
热门文章
- HTML炫酷粒子源代码
- EasyUI仓库管理系统
- 基于Unity3D实现的HitUFO鼠标打飞碟游戏
- HTML添加背景音乐代码
- 小学计算机键盘的初步认识教案,小学三年级信息技术--认识键盘教学设计(宋艳)[小编整理]...
- IBM server guide download
- ssq历史红蓝冷热号回归测试各个期与若干分析周期的中奖率(红号出现3个以上再输出)
- knowladge_网站开发_Sandboxie沙盒
- 附件二:攻防演练保密协议.docx
- 部署Exchange 2010