Csharp/C#教程:如何使用C#程序给PDF文件添加编辑域分享

PDF文档通常是不能编辑的,但有些时候需要在PDF文档中填写日期或签名之类,就需要在PDF有能编辑的文本域,本文介绍怎样用C#来实现这一功能。

环境

工具:VS2015

语言:C#

操作PDF类库:iTextSharp5.5.10

生成的PDF预览的工具:Skim、福昕阅读器、AcrobatReader

代码实现

获取文档的页数

PdfReaderreader=newPdfReader(@"C:WorkSpace1.pdf"); intcount=reader.NumberOfPages;

创建文本域

TextFieldfieldDate=newTextField(stamp.Writer,newiTextSharp.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-LeftHeigth=Top-Bototom
创建文本

Chunkcname=newChunk("Date:",FontFactory.GetFont("Futura",16f,newBaseColor(170,64,0))); Phrasepname=newPhrase(cname); PdfContentByteover=stamp.GetOverContent(count); ColumnText.ShowTextAligned(over,Element.ALIGN_CENTER,pname,400,420,0);

完整代码

publicstaticvoidAddTextField() { PdfReaderreader=newPdfReader(@"C:WorkSpace1.pdf"); FileStreamout1=newFileStream(@"C:WorkSpace2.pdf",FileMode.Create,FileAccess.Write); PdfStamperstamp=newPdfStamper(reader,out1); //获得pdf总页数 intcount=reader.NumberOfPages; TextFieldfieldDate=newTextField(stamp.Writer,newiTextSharp.text.Rectangle(105,100,240,125),"date"); fieldDate.BackgroundColor=BaseColor.WHITE; fieldDate.BorderWidth=1; fieldDate.BorderColor=BaseColor.BLACK; fieldDate.BorderStyle=4; fieldDate.FontSize=11f; TextFieldfieldSign=newTextField(stamp.Writer,newiTextSharp.text.Rectangle(430,100,530,125),"sign"); fieldSign.BackgroundColor=BaseColor.WHITE; fieldSign.BorderWidth=1; fieldSign.BorderColor=BaseColor.BLACK; fieldSign.BorderStyle=4; fieldSign.FontSize=11f; Chunkcname=newChunk("Date:",FontFactory.GetFont("Futura",16f,newBaseColor(170,64,0))); Chunkctitle=newChunk("UserSign:",FontFactory.GetFont("Futura",16f,newBaseColor(0,128,128))); Phrasepname=newPhrase(cname); Phraseptitle=newPhrase(ctitle); //PdfContentBye类,用来设置图像和文本的绝对位置 PdfContentByteover=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#在PDF文件添加图片印章C#的Process类调用第三方插件实现PDF文件转SWF文件C#如何给PDF文件添加水印C#实现pdf导出.Net导出pdf文件C#WinForm打开PDF文件并在窗体中显示C#实现PDF文件添加图片背景用C#来解析PDF文件C#实现TIF图像转PDF文件的方法C#获取指定PDF文件页数的方法C#生成PDF文件流

标签: 添加编辑 程序

C#通过NPOI操作Excel的实例代码

C#向Word文档中添加内容控件的方法示例

上述就是C#学习教程:如何使用C#程序给PDF文件添加编辑域分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/907637.html

(0)
上一篇 2021年10月24日
下一篇 2021年10月24日

精彩推荐