field_merge: Example code of Early Access iText(how to fill PDF form with iTextSharp)

本文介绍了一个使用 iTextSharp 库实现 PDF 文档中表单字段的合并及扁平化的 C# 示例程序。该程序接收命令行参数,包括输入 PDF 文件、输出 PDF 文件以及需要设置的表单字段及其值。

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

using System;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
public class field_merge
{
    
    
private static void  usage()
    {
        
        System.Console.Out.WriteLine(
"Field merging and flattening");
        System.Console.Out.WriteLine(
"usage: field_merge [-f] infile_pdf outfile_pdf [[name1=value1]dot.gif[namex=valuex]]");
        System.Console.Out.WriteLine(
"    -f - flatten the fields");
    }
    

    [STAThread]
    
public static void  Main(string[] args)
    {
        
if (args.Length < 2)
        {
            usage();
            
return ;
        }
        
int idx = 0;
        
if (args[0].Equals("-f"))
            
++idx;
        
if (args.Length - idx < 2)
        {
            usage();
            
return ;
        }
        
string in_pdf = args[idx];
        
string out_pdf = args[idx + 1];
        
try
        {
            PdfReader reader 
= new PdfReader(in_pdf);
            PdfStamper stamp 
= new PdfStamper(reader, new FileStream(out_pdf, FileMode.Create));
            AcroFields form 
= stamp.AcroFields;
            
for (int k = idx + 2; k < args.Length; ++k)
            {
                
string t = args[k];
                
int x = t.IndexOf((System.Char) '=');
                
if (x > 0)
                {
                    
string field = t.Substring(0, (x) - (0));
                    
string value_Renamed = t.Substring(x + 1);
                    form.SetField(field, value_Renamed);
                }
            }
            
if (idx > 0)
                stamp.FormFlattening
=(true);
            stamp.Close();
            System.Console.Out.WriteLine(
"Done.");
        }
        
catch (System.Exception e)
        {
            System.Console.Error.WriteLine(e.Message);
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值