【问题标题】:Pdf Merge Issue in ItextSharp (After Merging Pdfs don't retain their Values)ItextSharp 中的 Pdf 合并问题(合并 Pdf 后不保留其值)
【发布时间】:2012-08-25 22:56:58
【问题描述】:

我们正在尝试使用 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);

【问题讨论】:

    标签: c# asp.net pdf merge itextsharp


    【解决方案1】:

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

    我已经创建了重命名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;
    

    【讨论】:

    • 这也解决了我的合并问题。虽然不确定 try/catch 的意义。并且变量 f 可以用作计数器而不是静态变量(renameFields 的参数)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-22
    • 2012-11-21
    • 2013-03-09
    • 2023-03-22
    • 2011-01-15
    • 2023-03-03
    • 2012-06-17
    相关资源
    最近更新 更多