如何使用C#程序給PDF文件添加編輯域
PDF文檔通常是不能編輯的,但有些時(shí)候需要在PDF文檔中填寫日期或簽名之類,就需要在PDF有能編輯的文本域,本文介紹怎樣用C#來實(shí)現(xiàn)這一功能。
環(huán)境
工具:VS2015
語言:C#
操作PDF類庫(kù):iTextSharp 5.5.10
生成的PDF預(yù)覽的工具:Skim、福昕閱讀器、Acrobat Reader
代碼實(shí)現(xiàn)
獲取文檔的頁數(shù)
PdfReader reader = new PdfReader(@"C:\WorkSpace\1.pdf"); int count = reader.NumberOfPages;
創(chuàng)建文本域
TextField fieldDate = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(105, 100, 240, 125), "date"); fieldDate.BackgroundColor= BaseColor.WHITE;fieldDate.BorderWidth= 1; fieldDate.BorderColor= BaseColor.BLACK;fieldDate.BorderStyle= 4; fieldDate.FontSize = 11f;
iTextSharp.text.Rectangle(105, 100, 240, 125) 用來設(shè)置文本域的位置,四個(gè)參數(shù)分別為:llx、lly、urx、ury:
llx 為L(zhǎng)eft ,lly 為Bottom,urx 為Right,ury 為Top
其中:Width=Right - Left Heigth = Top - Bototom
創(chuàng)建文本
Chunk cname = new Chunk("Date:", FontFactory.GetFont("Futura", 16f,new BaseColor(170,64,0))); Phrase pname = new Phrase(cname); PdfContentByte over = stamp.GetOverContent(count); ColumnText.ShowTextAligned(over, Element.ALIGN_CENTER, pname, 400, 420, 0);
完整代碼
public static void AddTextField() { PdfReader reader = new PdfReader(@"C:\WorkSpace\1.pdf"); FileStream out1 = new FileStream(@"C:\WorkSpace\2.pdf", FileMode.Create, FileAccess.Write); PdfStamper stamp = new PdfStamper(reader, out1); //獲得pdf總頁數(shù) int count = reader.NumberOfPages; TextField fieldDate = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(105, 100, 240, 125), "date"); fieldDate.BackgroundColor = BaseColor.WHITE; fieldDate.BorderWidth = 1; fieldDate.BorderColor = BaseColor.BLACK; fieldDate.BorderStyle = 4; fieldDate.FontSize = 11f; TextField fieldSign = new TextField(stamp.Writer, new iTextSharp.text.Rectangle(430, 100, 530, 125), "sign"); fieldSign.BackgroundColor = BaseColor.WHITE; fieldSign.BorderWidth = 1; fieldSign.BorderColor = BaseColor.BLACK; fieldSign.BorderStyle = 4; fieldSign.FontSize = 11f; Chunk cname = new Chunk("Date:", FontFactory.GetFont("Futura", 16f,new BaseColor(170,64,0))); Chunk ctitle = new Chunk("User Sign:", FontFactory.GetFont("Futura", 16f, new BaseColor(0, 128, 128))); Phrase pname = new Phrase(cname); Phrase ptitle = new Phrase(ctitle); //PdfContentBye類,用來設(shè)置圖像和文本的絕對(duì)位置 PdfContentByte over = stamp.GetOverContent(count); ColumnText.ShowTextAligned(over, Element.ALIGN_CENTER, pname, 400, 420, 0); ColumnText.ShowTextAligned(over, Element.ALIGN_CENTER, ptitle, 400, 350, 0); stamp.AddAnnotation(fieldDate.GetTextField(), count); stamp.AddAnnotation(fieldSign.GetTextField(), count); stamp.FormFlattening = true; stamp.Close(); }
上一篇:C# 匿名方法基礎(chǔ)回顧
欄 目:C#教程
本文標(biāo)題:如何使用C#程序給PDF文件添加編輯域
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/6033.html
您可能感興趣的文章
- 01-10Extjs4如何處理后臺(tái)json數(shù)據(jù)中日期和時(shí)間
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10WinForm實(shí)現(xiàn)程序一段時(shí)間不運(yùn)行自動(dòng)關(guān)閉的方法
- 01-10C#實(shí)現(xiàn)將程序鎖定到Win7任務(wù)欄的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10asp.net中XML如何做增刪改查操作
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#一個(gè)簡(jiǎn)單的定時(shí)小程序?qū)崿F(xiàn)代碼
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫(kù)的方法


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻播放器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 04-02jquery與jsp,用jquery
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10delphi制作wav文件的方法