vba 使用 Adob​​e Acrobat Pro 从 Excel 填写 PDF 表单数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8994926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 15:02:05  来源:igfitidea点击:

Fill PDF form data from Excel with Adobe Acrobat Pro

c#excel-vbaadobevbaexcel

提问by EKet

Is there any to fill PDF form fields using an Excel Data source? (Think 1000 pdf's)

是否可以使用 Excel 数据源填写 PDF 表单字段?(想想 1000 个 pdf)

For each row in Excel create a new blank pdf based from template "myForm.pdf" and fill in the matching values from the columns in Excel to the fields in "myForm.pdf" then save it as "myForm(i).pdf"

对于 Excel 中的每一行,根据模板“myForm.pdf”创建一个新的空白 pdf,并将 Excel 中列的匹配值填充到“myForm.pdf”中的字段中,然后将其保存为“myForm(i).pdf”

Finally Merge all the pdfs into a single PDF document.

最后将所有 pdf 合并为一个 PDF 文档。

VBA or Javascript for Adobe is fine as long as the concept holds true. Some manual parts are ok if it's not too much.

只要概念成立,适用于 Adob​​e 的 VBA 或 Javascript 就可以。如果不是太多,一些手动零件是可以的。

Not too many tutorials out there apparently so I really appreciate any expertise here.

显然没有太多教程,所以我非常感谢这里的任何专业知识。

Thanks!

谢谢!

回答by Siddharth Rout

As promised :)

按照承诺 :)

TRIED AND TESTED

久经考验

// Add reference to iTextSharp.dll. Freely available on the web. Free to use for non commercial applications.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Original Pdf
            string PDFFile = "C:\MyOriginalPDF.pdf";

            // New Pdf
            string newPDFFile = "C:\NewPDFFILE.pdf";

            PdfReader pdfReader = new PdfReader(PDFFile);
            PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newPDFFile, FileMode.Create));

            AcroFields pdfFFields = pdfStamper.AcroFields;

            // Fill PDF Form Fields
            pdfFFields.SetField("FieldName1", "Value1");
            pdfFFields.SetField("FieldName2", "Value2");
            //
            // And So on

            // Use this to remove editting options by setting it to false
            // To keep editing option leave it as TRUE
            pdfStamper.FormFlattening = true;

            // close the pdf
            pdfStamper.Close();
        }
    }
}

Edit: To interact with Excel From C#, I would recommended visiting the below mentioned link.

编辑:要与 C# 中的 Excel 交互,我建议访问下面提到的链接。

Topic: VB.NET and Excel

主题:VB.NET 和 Excel

Link: VB.NET and Excel

链接VB.NET 和 Excel

To convert the VB.Net Code to C# use the below link :)

要将 VB.Net 代码转换为 C#,请使用以下链接:)

Topic: Convert C# Code to VB.Net and vice-verse

主题:将 C# 代码转换为 VB.Net,反之亦然

Link: http://www.developerfusion.com/tools

链接http: //www.developerfusion.com/tools

Do let me know if you are still stuck and we will take it from there... :)

如果您仍然被卡住,请告诉我,我们将从那里开始...... :)

More Edits!!!

更多编辑!!!

Inspired by your post, I finally ended up writing an article on my blog. Now all you have to do is copy the entire code and convert it into C# and make relevant changes to suit your needs :)

受你的启发,我终于在我的博客上写了一篇文章。现在您所要做的就是复制整个代码并将其转换为 C# 并进行相关更改以满足您的需要:)

Topic: Fill/Retrieve data from PDF Form Fields using VB.Net From an Excel File

主题:使用 VB.Net 从 Excel 文件填充/检索 PDF 表单字段中的数据

Link: http://www.siddharthrout.com/index.php/2018/08/28/fillretrieve-data-from-pdf-form-fields-using-vb-net-from-an-excel-file/

链接http: //www.siddharthrout.com/index.php/2018/08/28/fillretrieve-data-from-pdf-form-fields-using-vb-net-from-an-excel-file/