如何使用C#程序給PDF文件添加編輯域
PDF文檔通常是不能編輯的,但有些時(shí)候需要在PDF文檔中填寫日期或簽名之類,就需要在PDF有能編輯的文本域,本文介紹怎樣用C#來(lái)實(shí)現(xiàn)這一功能。
環(huán)境
工具:VS2015
語(yǔ)言:C#
操作PDF類庫(kù):iTextSharp 5.5.10
生成的PDF預(yù)覽的工具:Skim、福昕閱讀器、Acrobat Reader
代碼實(shí)現(xiàn)
獲取文檔的頁(yè)數(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) 用來(lái)設(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總頁(yè)數(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類,用來(lái)設(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(); }
相關(guān)文章
C#使用Socket實(shí)現(xiàn)心跳的方法示例
這篇文章主要介紹了C#使用Socket實(shí)現(xiàn)心跳的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫(kù)建模的基本教程
這篇文章主要介紹了C#操作LINQ to SQL組件進(jìn)行數(shù)據(jù)庫(kù)建模的基本教程,LINQ to SQL被集成在.NET框架之中,需要的朋友可以參考下2016-03-03C#實(shí)現(xiàn)的sqlserver操作類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的sqlserver操作類,結(jié)合具體實(shí)例形式分析了C#針對(duì)sqlserver數(shù)據(jù)庫(kù)進(jìn)行連接、查詢、更新、關(guān)閉等相關(guān)操作技巧,需要的朋友可以參考下2017-06-06C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問題
這篇文章主要介紹了C# TabControl控件中TabPage選項(xiàng)卡切換時(shí)的觸發(fā)事件問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04