c# - C#:使用 PDFsharp 创建 PDF 表单 (AcroForm)
问题描述
如何将 PDF 表单元素添加到 PDFsharpPdfPage
对象?
我知道 AcroForm 是可填写 PDF 元素的最佳格式,但 PDFsharp 库似乎不允许您创建 AcroForm 对象的实例。
我已经能够使用 PDFsharp 生成简单的文档,如下所示:
static void Main(string[] args) {
PdfDocument document = new PdfDocument();
document.Info.Title = "Created with PDFsharp";
// Create an empty page
PdfPage page = document.AddPage();
// Draw Text
XGraphics gfx = XGraphics.FromPdfPage(page);
XFont font = new XFont("Verdana", 20, XFontStyle.BoldItalic);
gfx.DrawString("Hello, World!", font, XBrushes.Black,
new XRect(0, 0, page.Width, page.Height), XStringFormats.Center);
// Save document
const string filename = "HelloWorld.pdf";
document.Save(filename);
}
但我无法弄清楚如何添加可填写的表单元素。我猜它可能会使用该page.Elements.Add(string key, PdfItem item)
方法,但是您如何制作 AcroForm PdfItem
?(因为像PdfTextField
这样的类似乎没有公共构造函数)
PDFsharp 论坛和文档对此没有帮助,而我在 Stack Overflow 上找到的最接近的答案是这个,它用错误的库来回答。
所以,简而言之:我如何将"Hello World"
上面的文本转换为文本字段?
是否可以在 PDFsharp 中执行此操作,或者我应该使用不同的 C# PDF 库?(我非常想坚持使用免费的——最好是开源的——库)
解决方案
PdfSharp 中的大多数类构造函数都是密封的,这使得创建新的 pdf 对象有点困难。但是,您可以使用它的类创建对象来添加低级 pdf 元素。
下面是创建文本字段的示例。
请参阅从第 432 页开始的关于关键元素定义的 pdf 技术规范 https://www.adobe.com/content/dam/acom/en/devnet/pdf/pdfs/PDF32000_2008.pdf
public static void AddTextBox()
{
using (PdfDocument pdf = new PdfDocument())
{
PdfPage page1 = pdf.AddPage();
double left = 50;
double right = 200;
double bottom = 750;
double top = 725;
PdfArray rect = new PdfArray(pdf);
rect.Elements.Add(new PdfReal(left));
rect.Elements.Add(new PdfReal(bottom));
rect.Elements.Add(new PdfReal(right));
rect.Elements.Add(new PdfReal(top));
pdf.Internals.AddObject(rect);
PdfDictionary form = new PdfDictionary(pdf);
form.Elements.Add("/Filter", new PdfName("/FlateDecode"));
form.Elements.Add("/Length", new PdfInteger(20));
form.Elements.Add("/Subtype", new PdfName("/Form"));
form.Elements.Add("/Type", new PdfName("/XObject"));
pdf.Internals.AddObject(form);
PdfDictionary appearanceStream = new PdfDictionary(pdf);
appearanceStream.Elements.Add("/N", form);
pdf.Internals.AddObject(appearanceStream);
PdfDictionary textfield = new PdfDictionary(pdf);
textfield.Elements.Add("/FT", new PdfName("/Tx"));
textfield.Elements.Add("/Subtype", new PdfName("/Widget"));
textfield.Elements.Add("/T", new PdfString("fldHelloWorld"));
textfield.Elements.Add("/V", new PdfString("Hello World!"));
textfield.Elements.Add("/Type", new PdfName("/Annot"));
textfield.Elements.Add("/AP", appearanceStream);
textfield.Elements.Add("/Rect", rect);
textfield.Elements.Add("/P", page1);
pdf.Internals.AddObject(textfield);
PdfArray annotsArray = new PdfArray(pdf);
annotsArray.Elements.Add(textfield);
pdf.Internals.AddObject(annotsArray);
page1.Elements.Add("/Annots", annotsArray);
// draw rectangle around text field
//XGraphics gfx = XGraphics.FromPdfPage(page1);
//gfx.DrawRectangle(new XPen(XColors.DarkOrange, 2), left, 40, right, bottom - top);
// Save document
const string filename = @"C:\Downloads\HelloWorld.pdf";
pdf.Save(filename);
pdf.Close();
Process.Start(filename);
}
}
推荐阅读
- swift - 如何在 Swift 中编写一个将枚举与枚举案例进行比较的函数
- node.js - 如何让我的 Discord Bot 删除服务器中的所有频道
- r - 使用表函数计算 R 中的样本比例
- firebase - Firebase 电话身份验证:一些用户没有收到 OTP
- javascript - (已解决)将 Ruby ROR 变量传递给 Javascript 小部件脚本的正确方法是什么?
- c# - 如何从 HTML 的下拉列表中获取选定的值到 C#
- spring-boot - JPARepository CPRQ 修改不保存完整对象
- python - Pandas 多索引
- android - Jetpack Compose 使用 Painter 在图像上绘图
- php - 在 URL 和模型关联中带有 id 的 Laravel 资源路由