如何使用C#程序給PDF文件添加編輯域
PDF文檔通常是不能編輯的,但有些時(shí)候需要在PDF文檔中填寫日期或簽名之類,就需要在PDF有能編輯的文本域,本文介紹怎樣用C#來實(shí)現(xiàn)這一功能。
環(huán)境
工具:VS2015
語言:C#
操作PDF類庫: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();
}
相關(guān)文章
基于WPF實(shí)現(xiàn)簡(jiǎn)單的文件夾比較工具
文件比較平常都是用Beyond?Compare,可以說離不開的神器,不過Beyond?Compare平常拿它主要是用來做代碼比較,用來做一些大批量的二進(jìn)制文件比較,其實(shí)有點(diǎn)不是很方便,所以本文來用WPF做一個(gè)簡(jiǎn)單的文件夾比較的小工具2023-05-05
c#中實(shí)現(xiàn)圖片灰度化技術(shù)詳解
這篇文章主要介紹了c#中實(shí)現(xiàn)圖片灰度化技術(shù)詳解,本文給出計(jì)算公式和實(shí)現(xiàn)代碼以及圖片例子,需要的朋友可以參考下2014-08-08
C#程序集的主版本號(hào)和次版本號(hào)的實(shí)現(xiàn)
C# 程序集的版本號(hào)和次版本號(hào)是程序集的一部分,用于標(biāo)識(shí)程序集的不同版,本本文主要介紹了C#程序集的主版本號(hào)和次版本號(hào)的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04
使用C#驗(yàn)證PDF數(shù)字簽名有效性的方法示例
數(shù)字簽名作為PDF文檔中的重要安全機(jī)制,不僅能夠驗(yàn)證文件的來源,還能確保文件內(nèi)容在傳輸過程中未被篡改,本文將詳細(xì)介紹如何使用免費(fèi).NET控件通過C#驗(yàn)證PDF簽名的有效性以及驗(yàn)證PDF文檔是否被修改,需要的朋友可以參考下2024-07-07
C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法
這篇文章主要介紹了C#將數(shù)字轉(zhuǎn)換成字節(jié)數(shù)組的方法,涉及C#字符串操作的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04
C#?wpf利用附加屬性實(shí)現(xiàn)任意控件拖動(dòng)
這篇文章主要為大家詳細(xì)介紹了C#?WPF如何利用附加屬性對(duì)幾種拖動(dòng)方式進(jìn)行封裝,實(shí)現(xiàn)復(fù)用性,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-11-11

