如何使用C#程序给PDF文件添加编辑域

本文介绍如何使用C#和iTextSharp库在PDF文档中创建可编辑的文本域,包括日期和签名字段,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

环境

工具:VS2015

语言:C#

操作PDF类库:iTextSharp 5.5.10

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

代码实现

获取文档的页数

?
1
2
PdfReader reader = new PdfReader( @"C:\WorkSpace\1.pdf" );
int count = reader.NumberOfPages;

创建文本域

?
1
2
3
4
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
创建文本

?
1
2
3
4
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);

完整代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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();
}

 

微信公众号搜索 “ 脚本之家 ” ,选择关注

程序猿的那些事、送书等活动等着你

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值