如何使用iTextSharp将PDF与可填写表单字段合并/合并?

时间:2011-06-13 03:51:38

标签: .net itextsharp

使用iTextSharp,如何将多个PDF合并为一个PDF而不会丢失每个PDF中的表单字段及其属性?

(我更喜欢使用来自数据库的流的示例,但文件系统也可以)

我发现this code可以正常工作,但它会使我的PDF变得扁平,所以我无法使用它。

更新

@Mark Storer - 这是我现在根据您的反馈使用的代码(见下文),但它在保存后给了我一个损坏的文档。我分别测试了每个代码部分,似乎在下面显示的MergePdfForms函数中失败了。我显然不想使用示例的renameFields部分,因为我需要字段名称保持“原样”。

Public Sub MergePdfForms(ByVal pdfFiles As ArrayList, ByVal outputPath As String)
    Dim ms As New IO.MemoryStream()
    Dim copier As New PdfCopyFields(ms)
    For Each pfile As String In pdfFiles
        Dim reader As New PdfReader(pfile)
        copier.AddDocument(reader)
    Next
    SaveMemoryStream(ms, outputPath)
    copier.Close()
End Sub

Public Sub SaveMemoryStream(ms As IO.MemoryStream, FileName As String)
    Dim outStream As IO.FileStream = IO.File.OpenWrite(FileName)
    ms.WriteTo(outStream)
    outStream.Flush()
    outStream.Close()
End Sub

3 个答案:

答案 0 :(得分:11)

PDF中的字段具有异常属性:同名的所有字段都是相同字段。他们共享一个价值。当表单引用同一个人并且您在表单中有一个很好的命名方案时,这很方便。当您想将单个表单的20个实例放入单个PDF时,这不是很方便。

至少可以说,这使得合并多种形式具有挑战性。最常见的选择(感谢iText)是在合并它们之前展平表单,此时你没有长时间的合并形式,问题就会消失。

另一个选项是在合并之前重命名字段。这可能使以后的数据提取变得困难,可能会破坏脚本,并且通常是PITA。这就是为什么扁平化更受欢迎的原因。

在iText中有一个名为PdfCopyFields的类,它会正确地将字段从一个文档复制到另一个文档......它还会合并具有相同名称的字段正确,这样它们就可以了真正共享一个值,Acrobat / Reader不需要对文件做一些额外的工作就可以在将它显示给用户之前就这样做。

但是,PdfCopyFields不会为您重命名字段。为此,您需要从相关AcroFields获取PdfReader对象,并在将文档与renameField(String, String)合并之前,在每个字段上调用PdfCopyFields。< / p>

所有这些都是基于“AcroForm”的PDF表单。如果您正在处理XFA表单(来自LiveCycle Designer的表单),则所有投注均已关闭。你必须使用XML,A Lot。

如果你必须将两者的形式结合起来,天堂会帮助你。

所以说你正在使用AcroForm字段,代码可能看起来像这样(原谅我的Java):

public void mergeForms(String outpath, String inPaths[]) {
  PdfCopyFields copier = new PdfCopyFields(new FileOutputStream(outpath) );
  for (String curInPath : inPaths) {
    PdfReader reader = new PdfReader(curInPath);
    renameFields(reader.getAcroFields());

    copier.addDocument(reader);
  }
  copier.close();
}

private static int counter = 0;
private void renameFields(AcroFields fields) {
  Set<String> fieldNames = fields.getFields().keySet();
  String prepend = String.format("_%d.", counter++);

  for(String fieldName : fieldNames) {
    fields.rename(fieldName, prepend + fieldName);
  }
}

理想情况下,renameFields还会创建一个名为prepend's-value的通用字段对象,并使文档中的所有其他字段成为子项。这将使Acrobat / Reader的生活更轻松,避免显然不必要的“保存更改?”从Acrobat关闭生成的PDF时请求。

是的,这就是为什么Acrobat有时会要求您在没有做任何事情时保存更改! Acrobat在幕后做了些什么。

答案 1 :(得分:2)

您也可以使用此代码....它将合并所有pdf文件而不会丢失字段值..

    Document document = new Document();
    try
        {         
           string destinationfile = desktopPath.Replace(@"d:\outputfile.pdf");
           PdfCopyFields copier = new PdfCopyFields(new FileStream(destinationfile,     FileMode.Create));
            PdfImportedPage page;

            //Loops for each file that has been listed
            foreach (string filename in fileList)
            {
                flag++;
                try
                {
                    //The current file path
                    string filePath = sourcefolder + filename;

                    PdfReader reader = new PdfReader(filePath);
                    copier.AddDocument(reader);

                }
                catch
                {

                }
            }
            copier.Close();
        }

答案 2 :(得分:0)