合并PDF文件。

| 是否可以使用itextsharp在asp.net C#中将可填充pdf文件与常规pdf文件合并。任何帮助将不胜感激。     
已邀请:
        花了我很长时间才弄清楚这一点,但这是一个有效的示例:
public static void MergeFiles(string destinationFile, string[] sourceFiles)
    {
        try 
        {
            int f = 0;
            String outFile = destinationFile;
            Document document = null;
            PdfCopy  writer = null;
            while (f < sourceFiles.Length) {
                // Create a reader for a certain document
                PdfReader reader = new PdfReader(sourceFiles[f]);

                // Retrieve the total number of pages
                int n = reader.NumberOfPages;
                //Trace.WriteLine(\"There are \" + n + \" pages in \" + sourceFiles[f]);
                if (f == 0) 
                {
                    // Step 1: Creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    // Step 2: Create a writer that listens to the document
                    writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
                    // Step 3: Open the document
                    document.Open();
                }
                // Step 4: Add content
                PdfImportedPage page;
                for (int i = 0; i < n; ) 
                {
                    ++i;
                    page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null)
                    writer.CopyAcroForm(reader);
                f++;
            }
            // Step 5: Close the document
            document.Close();
        }
        catch(Exception) 
        {
            //handle exception
        }
    }
希望这可以帮助! 资源: http://www.ujihara.jp/iTextdotNET/examples/concat_pdf.java     
        是的 PdfCopyFields听起来差不多。这样,即使您的“常规pdf文件”具有任何种类的注释(链接,flash等等),它们仍然可以正常工作。 或者,您可以使用PdfStamper打开表单,然后从““常规” PDF中添加PdfImportedPages。这将保留有关原始PDF的所有内容(元数据,结构,文档级脚本等),并从“常规pdf”中吸取页面(没有页面内容的内容除外)。 可悲的是,这里没有完美的答案,但是至少有两个可行的选择。     
        您可以使用PdfCopyFields合并任何复杂的可填充pdf表单。它确实支持所有样式,表单字段和pdf功能。只需尝试以下代码即可。
PdfReader readerfile1 = new PdfReader(\"File1\");
PdfReader readerfile2 = new PdfReader(\"File2\");


    PdfCopyFields copyfile =
    new PdfCopyFields(new FileStream(\"OutputFilewithFullPath\", FileMode.Create));
    copyfile.AddDocument(readerfile1);
    copyfile.AddDocument(readerfile2);
    copyfile.Close();
这将产生一个您指定的新文件,它将是一个完整的可填写表格。     
        我意识到这是一个旧线程,但是我遇到了类似的问题,这是一个更干净的解决方案
public string GetMergedPdfPath(List<KeyValuePair<AccountHolder, string>> filePaths, string outputFilename)
{

string directory = ApplicationDeployment.IsNetworkDeployed ? ApplicationDeployment.CurrentDeployment.DataDirectory : NoticeConstants.Paths.TestDirectory;
string outputFilepath = directory + \"\\\\\" + outputFilename;

Document outputDocument = null;
PdfCopy outputCopier = null;

try
{
   using (outputDocument = new Document())
   {
       using (FileStream fs = new FileStream(outputFilepath, FileMode.Create))
       {
            using (outputCopier = new PdfCopy(outputDocument, fs))
            {
                outputDocument.Open();

                for (int i = 0; i < filePaths.Count; i++)
                {
                    if (string.IsNullOrEmpty(filePaths[i].Value))
                    {
                        continue;
                    }

                    using (PdfReader reader = new PdfReader(filePaths[i].Value))
                    {
                        NoticeUtil.GetPdfPages(reader, ref outputCopier);
                    }
                }

                outputDocument.Close();
            }
        }
    }

      return outputFilepath;
  }
  catch (Exception ex)
  {
      Log.Error(ex.Message, ex);

      return string.Empty;
  }

}

    private static List<PdfImportedPage> GetPdfPages(PdfReader reader, ref PdfCopy outputCopier)
    {
        List<PdfImportedPage> pages = new List<PdfImportedPage>();

        try
        {
            for (int p = 1; p <= reader.NumberOfPages; p++)
            {
                outputCopier.AddPage(outputCopier.GetImportedPage(reader, p));
            }
        }
        catch (Exception ex)
        {
            Log.Error(ex.Message, ex);
        }

        return pages;
    }
    

要回复问题请先登录注册