我有一些PDF,我想使用C#自动填写。我知道iTextSharp,但我不确定商业用途的许可问题,我宁愿找一个不同的解决方案。
基本上,我想打开一个PDF,指定字段(或给一个文本框的坐标),并能够插入文本(&可能是小图像?)。然后我需要合并并保存pdf。
如果我不需要购买/担心许可证,有什么建议可以实现这一点吗?
发布于 2012-10-20 05:03:26
不确定iTextSharp的优点是什么,但我找到了一个使用PDFSharp的解决方案,到目前为止,它工作得很好!由于文档似乎对PDFSharp有点轻描淡写,我也发现this thread非常有用……
如果这对任何人都有帮助,下面是我的示例程序:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using PdfSharp.Pdf.AcroForms;
namespace PDFSharpTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void goButton_Click(object sender, EventArgs e)
{
//TestPDF(); //uncomment this to find out whether acroform will work correctly
//open file
PdfDocument pdf = PdfReader.Open(@"YOURFILEPATHandNAME", PdfDocumentOpenMode.Modify);
//fix some odd setting where filled fields don't always show SetupPDF(pdf);
//find and fill fields
PdfTextField txtEmployerName = (PdfTextField)(pdf.AcroForm.Fields["txtEmployerName"]);
txtEmployerName.Value = new PdfString("My Name");
PdfTextField txtEmployeeTitle = (PdfTextField)(pdf.AcroForm.Fields["txtEmployeeTitle"]);
txtEmployeeTitle.Value = new PdfString("Workin'");
PdfCheckBoxField chxAttached = (PdfCheckBoxField)(pdf.AcroForm.Fields["chxAttached"]);
chxAttached.Checked = true;
//save file
pdf.Save(@"NEWFILEPATHandNAMEHERE");
}
private void SetupPDF(PdfDocument pdf)
{
if (pdf.AcroForm.Elements.ContainsKey("/NeedAppearances") == false)
{
pdf.AcroForm.Elements.Add("/NeedAppearances", new PdfSharp.Pdf.PdfBoolean(true));
}
else
{
pdf.AcroForm.Elements["/NeedAppearances"] = new PdfSharp.Pdf.PdfBoolean(true);
}
}
private void PDFTest()
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
PdfDocument _document = null;
try { _document = PdfReader.Open(ofd.FileName, PdfDocumentOpenMode.Modify); }
catch (Exception ex)
{
MessageBox.Show(ex.Message, "FATAL"); //do any cleanup and return
return;
}
if (_document != null)
{
if (_document.AcroForm != null)
{
MessageBox.Show("Acroform is object", "SUCCEEDED");
//pass acroform to some function for processing
_document.Save(@"C:\temp\newcopy.pdf");
}
else
{
MessageBox.Show("Acroform is null", "FAILED");
}
}
else
{
MessageBox.Show("Unknown error opening document", "FAILED");
}
}
}
}
}发布于 2012-10-20 01:55:23
这是一个免费的开源解决方案:SharpPDF
但老实说,iTextSharp可能是你能找到的最好的东西。
发布于 2012-10-20 04:16:54
您可以尝试使用Docotic.Pdf库来完成您的任务。这个库不是免费的,但可能没有什么需要担心的(许可方面的)。
以下是在线提供的可能对您有帮助的示例:
来自Forms and Annotation组的Other samples在您的案例中也可能被证明是有用的。
免责声明:我为该库的供应商工作。
https://stackoverflow.com/questions/12979674
复制相似问题