国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

如何使用C#程序給PDF文件添加編輯域

2019-10-29 21:16:03
字體:
來源:轉載
供稿:網友

PDF文檔通常是不能編輯的,但有些時候需要在PDF文檔中填寫日期或簽名之類,就需要在PDF有能編輯的文本域,本文介紹怎樣用C#來實現這一功能。

環境

工具:VS2015

語言:C#

操作PDF類庫:iTextSharp 5.5.10

生成的PDF預覽的工具:Skim、福昕閱讀器、Acrobat Reader

代碼實現

獲取文檔的頁數

PdfReader reader = new PdfReader(@"C:/WorkSpace/1.pdf");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;

iTextSharp.text.Rectangle(105, 100, 240, 125) 用來設置文本域的位置,四個參數分別為:llx、lly、urx、ury:

llx 為Left ,lly 為Bottom,urx 為Right,ury 為Top
其中:Width=Right - Left Heigth = Top - Bototom
創建文本

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總頁數   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類,用來設置圖像和文本的絕對位置   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#教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 龙门县| 青海省| 淮阳县| 灯塔市| 吴川市| 类乌齐县| 邮箱| 东海县| 牟定县| 萝北县| 泌阳县| 民勤县| 青铜峡市| 高碑店市| 晋江市| 界首市| 长兴县| 饶阳县| 肃南| 安国市| 中山市| 溧阳市| 荆门市| 永昌县| 土默特左旗| 大宁县| 奈曼旗| 高密市| 巫山县| 望都县| 宜川县| 大荔县| 柳江县| 湘潭县| 逊克县| 拉萨市| 绥宁县| 遂平县| 丹凤县| 特克斯县| 南丹县|