gpt4 book ai didi

c# - 如何使用 itextsharp 读取 marge fillable pdf 数据

转载 作者:太空宇宙 更新时间:2023-11-03 14:55:08 25 4
gpt4 key购买 nike

我有两个可填写的 pdf 文件,并编写了将这些 pdf 合并为一个 pdf 的代码。下面是我的代码。

public void PDFSplit()
{
List<string> files=new List<string>();
files.Add(Server.MapPath("~/Template/sample_pdf.pdf"));
files.Add(Server.MapPath("~/Template/temp/sample_pdf.pdf"));
//call method
Merge(files, Server.MapPath("~/Template/sample_pdf_123.pdf"));
}

//Merge pdf
public void Merge(List<String> InFiles, String OutFile)
{
using (FileStream stream = new FileStream(OutFile, FileMode.Create))
using (iTextSharp.text.Document doc = new iTextSharp.text.Document())
using (PdfCopy pdf = new PdfCopy(doc, stream))
{
doc.Open();

PdfReader reader = null;
PdfImportedPage page = null;

InFiles.ForEach(file =>
{
reader = new PdfReader(file);

for (int i = 0; i < reader.NumberOfPages; i++)
{
page = pdf.GetImportedPage(reader, i + 1);
pdf.AddPage(page);
}

pdf.FreeReader(reader);
reader.Close();
});
}
}

代码工作正常,但问题是当我尝试读取新生成的合并文件时,它没有显示使用 AcroFields 的字段。

//To read pdf data
PdfReader reader = null;

reader = new PdfReader(Server.MapPath("~/Template/sample_pdf_123.pdf"));

AcroFields pdfFormFields = reader.AcroFields;

最佳答案

您无法判断错误 PDF 文件,因为您使用的是旧版本的 iText。请升级到iText 7 for .NET并阅读 iText 7 jump-start tutorial ,更具体地说 chapter 6它说:

合并表单

是这样的:

PdfDocument destPdfDocument = new PdfDocument(new PdfWriter(dest));
PdfDocument[] sources = new PdfDocument[] {
new PdfDocument(new PdfReader(SRC1)),
new PdfDocument(new PdfReader(SRC2)) };
PdfPageFormCopier formCopier = new PdfPageFormCopier();
foreach (PdfDocument sourcePdfDocument in sources) {
sourcePdfDocument.CopyPagesTo(1,
sourcePdfDocument.GetNumberOfPages(), destPdfDocument, formCopier);
sourcePdfDocument.Close();
}
destPdfDocument.Close();

关于c# - 如何使用 itextsharp 读取 marge fillable pdf 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49819174/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com