繁体   English   中英

ItextSharp中的Pdf合并问题(合并Pdf后不保留其值)

[英]Pdf Merge Issue in ItextSharp (After Merging Pdfs don't retain their Values)

我们正在尝试使用ITextSharp合并三个PDF。 问题是合并后,我们只能从第一个PDF获取数据,而其他两个PDF不会保留其值。

所有这些PDF具有相同的结构(即,它们使用具有不同数据的相同模板),因此我假设它们具有相同的字段(AcroFields),这可能在合并时造成此问题。

这是合并代码:

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
        }
    }

这称为:

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

我经过一番搜索才弄清楚了...以下是解决方案...

我创建了重命名PDF中的字段的功能,以便合并后的字段将被重命名。

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;
            }
        }

该函数在“ MergeFiles”函数中调用,如下所示...

          // 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM