我自己听取了 khkremer 的建议,并在 C# 中构建了一个小型 .net exe,以基于 itextsharp 库从 xfdf 文件创建 PDF。它仍然需要使用 passthru 来调用,但它又好又小。我的版本还可以选择在 pdf 中插入隐藏字段,因为我的项目需要该功能。
您将需要下载并添加对 iTextSharp.text.pdf 程序集的引用。
希望能帮助到你!
using System;
using System.IO;
using iTextSharp.text.pdf;
namespace PDFBrain
{
class Program
{
// args:
// 0 => template
// 1 => xfdf
// 2 => outputfile
// 3 => flatten output file?
static void Main(string[] args)
{
if (args == null)
{
Console.Out.WriteLine("No arguments were provided. Exiting.");
return;
}
if (args[0] == "create")
{
if (args.Length != 5)
{
Console.Out.WriteLine("Wrong number of arguments were provided. Exiting.");
return;
}
CreatePDF(args[1], args[2], args[3], args[4]);
}
if (args[0] == "hidden")
{
if (args.Length != 3)
{
Console.Out.WriteLine("Wrong number of arguments were provided. Exiting.");
return;
}
InsertHiddenIdField(args[1], args[2]);
}
}
public static void CreatePDF(string templ, string xfdf, string output, string flatten)
{
PdfReader template = new PdfReader(templ);
XfdfReader xfdfReader = new XfdfReader(xfdf);
PdfStamper stamper = new PdfStamper(template, new FileStream(output, FileMode.Create));
stamper.AcroFields.SetFields(xfdfReader);
stamper.FormFlattening = flatten == "true" ? true : false;
stamper.Writer.CloseStream = false;
stamper.Close();
}
public static void InsertHiddenIdField(string templ, string output)
{
PdfReader template = new PdfReader(templ);
PdfStamper stamper = new PdfStamper(template, new FileStream(output, FileMode.Create));
TextField clientID = new TextField(stamper.Writer, new iTextSharp.text.Rectangle(10,750,60,770), "hdnClientID");
clientID.Visibility = BaseField.HIDDEN;
stamper.AddAnnotation(clientID.GetTextField(),1);
stamper.Close();
}
}
}