且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PDF合并问题在iTextSharp的(后合并PDF文件不保留其值)

更新时间:2023-02-12 20:35:17

我想通了之后自己一些搜索...关注的是解决方案...

我创建的功能在PDF重命名字段,从而使合并域之后将被重新命名。

 私有静态诠释柜台= 0;
私人无效renameFields(PdfReader pdfReader)
        {
            尝试
            {
                字符串prePEND =的String.Format(_ {0},计数器++);
                的foreach(德的DictionaryEntry在pdfReader.AcroFields.Fields)
                {
                    pdfReader.AcroFields.RenameField(de.Key.ToString(),prePEND + de.Key.ToString());
                }
            }
            赶上(异常前)
            {
                扔恩;
            }
        }

此功能被称为MergeFiles作为后续功能...

  //创建一个特定文档阅读器
             PdfReader读卡器=新PdfReader(来源档案[F]);
             renameFields(读卡器);
          //检索的页的总数
             INT N = reader.NumberOfPages;

We are trying to merge three PDFs using ITextSharp. The problem is after merging we are able to get Data from the first PDF only, while the other two PDFs don't retain their values.

All these PDFs have the same structure (i.e. they use the same templates with different data), so my assumption is they are having same fields (AcroFields) which may be creating this problem while merging.

Here is the merge code :

public 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;
                    if (writer != null)
                    {
                        page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }

                PRAcroForm form = reader.AcroForm;
                if (form != null)
                {
                    if (writer != null)
                    {
                        writer.CopyAcroForm(reader);
                    }
                }

                f++;
            }
            // Step 5: Close the document
            if (document != null)
            {
                document.Close();
            }
        }
        catch (Exception)
        {
            //handle exception
        }
    }

This is called as follows :

    string[] sourcenames = { @"D:\1.pdf", @"D:\2.pdf", @"D:\3.pdf" };
    string destinationname = @"D:\pdf\mergeall\merge3.pdf";
    MergeFiles(destinationname, sourcenames);

I figured it out myself after little searching...Following is the solution...

I have created the function to rename the Fields in the PDF,so that after merging the fields will be renamed.

private static int counter = 0;
private void renameFields(PdfReader pdfReader)
        {
            try
            {
                string prepend = String.Format("_{0}", counter++);
                foreach (DictionaryEntry de in pdfReader.AcroFields.Fields)
                {
                    pdfReader.AcroFields.RenameField(de.Key.ToString(), prepend + de.Key.ToString());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

This function is called in "MergeFiles" function as follow...

          // Create a reader for a certain document
             PdfReader reader = new PdfReader(sourceFiles[f]);
             renameFields(reader);
          // Retrieve the total number of pages
             int n = reader.NumberOfPages;