PDF合并问题在iTextSharp的(后合并PDF文件不保留其值) [英] Pdf Merge Issue in ItextSharp (After Merging Pdfs don't retain their Values)

查看:606
本文介绍了PDF合并问题在iTextSharp的(后合并PDF文件不保留其值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正试图合并使用iTextSharp的3 PDF文件。问题是,合并后,我们能够只从第一PDF获取数据,而其他两个PDF文件不保留它们的值。

所有这些PDF具有相同的结构(即它们使用不同的数据相同的模板),所以我的假设是它们具有相同的字段(AcroFields),其可以同时合并被创建了这个问题。

下面是合并code:

 公共无效MergeFiles(字符串destinationFile,字符串[]来源档案)
    {
        尝试
        {
            INT F = 0;
            串不过outFile = destinationFile;
            文献文件= NULL;
            PdfCopy作家=无效;
            而(F< sourceFiles.Length)
            {
                //创建一个阅读器,用于特定文档
                PdfReader读卡器=新PdfReader(来源档案[F]);
                //检索的页的总数
                INT N = reader.NumberOfPages;
                //Trace.WriteLine(\"There是+来源档案[F])+ N +页面;
                如果(F == 0)
                {
                    //步骤1:文档对象的创建
                    文档=新的文件(reader.GetPageSizeWithRotation(1));
                    //步骤2:创建侦听到文档中的作家
                    作家=新PdfCopy(文件,新的FileStream(不过outFile,FileMode.Create));
                    //第3步:打开文档
                    document.Open();
                }
                //第4步:添加内容
                PdfImportedPage页面;
                的for(int i = 0; I< N)
                {
                    ++我;
                    如果(作家!= NULL)
                    {
                        页= writer.GetImportedPage(读者,我);
                        writer.AddPage(页);
                    }
                }                PRAcroForm形式= reader.AcroForm;
                如果(表格!= NULL)
                {
                    如果(作家!= NULL)
                    {
                        writer.CopyAcroForm(读卡器);
                    }
                }                ˚F++;
            }
            //第5步:关闭文档
            如果(文件!= NULL)
            {
                document.Close();
            }
        }
        赶上(例外)
        {
            //处理异常
        }
    }

这就是所谓如下:

 的String [] = sourcenames {@D:\\次数1.pdf,@D:\\ 2.pdf,@D:\\ 3.PDF};
    字符串destinationname = @D:\\ PDF \\ mergeall \\ merge3.pdf
    MergeFiles(destinationname,sourcenames);


解决方案

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

我创建的功能在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;

这篇关于PDF合并问题在iTextSharp的(后合并PDF文件不保留其值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆