合并PDF文件iTextSharp的

合并PDF文件iTextSharp的

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

问题描述

我看了看几个例子在线想出了这个code合并使用iTextSharp的PDF文件。但我得到一个错误:

{的文档没有页。}

它未能在网页= writer.GetImportedPage(阅读器,X);

下面的堆栈跟踪:

 在iTextSharp.text.pdf.PdfPages.WritePageTree()
 在iTextSharp.text.pdf.PdfWriter.Close()
 在iTextSharp.text.pdf.PdfCopy.Close()
 在iTextSharp.text.Document.Close()
 

它,当我调试它3页。什么是错在这里?

下面是我的code

 公共静态的MemoryStream MergePdfs(名单< MemoryStream的> pdfStreams)
       {
        //创建输出流
           MemoryStream的OutStream =新的MemoryStream();
           文献文件= NULL;

           尝试
           {
                //创建主阅读器
                PdfReader读卡器=新PdfReader(pdfStreams.ElementAt(0));
                //创建主文档
                文档=新的文件(Reader.GetPageSizeWithRotation(1));
                //创建主撰稿人
                PdfCopy作家=新PdfCopy(文档,OutStream);
                //写入打开文件
                Document.Open();
                //添加页面
                的addPages(Reader.NumberOfPages,读卡器,裁判作家);

                //对于后第一次每增加PDF将它们组合成主文档
                的foreach(的MemoryStream PdfStream在pdfStreams.Skip(1))
                {
                    PdfReader Reader2 =新PdfReader(PdfStream);
                    //添加内容
                    的addPages(Reader2.NumberOfPages,Reader2,裁判作家);
                }
             }
             最后
             {
                 //第五步:关闭文档
                 如果(文件!= NULL)
                     Document.Close();

                的foreach(在pdfStreams VAR STRM)
                {
                    尝试{如果(空= STRM!)Strm.Dispose(); } 抓住 { }
                }
             }

             返回OutStream;
         }

        私有静态无效的addPages(INT页,PdfReader读者,裁判PdfCopy作家)
        {
            PdfImportedPage页=无效;

            对于(INT X = 0,X<页面; X ++)
            {
                 页= writer.GetImportedPage(读者,X);
                 writer.AddPage(页);
            }

            如果(reader.AcroForm!= NULL)
                writer.CopyAcroForm(读卡器);
        }
 

解决方案

页码GetImportedPage是基于1的。因此:

 的for(int x = 1; X<网页+ 1; X ++)
{
    页= writer.GetImportedPage(读者,X);
    writer.AddPage(页);
}
 

I looked at a few examples online and came up with this code to merge pdfs using iTextSharp. But I am getting an error :.

{"The document has no pages."}

It fails at Page = writer.GetImportedPage(reader, X);

Here is stack trace :

 at iTextSharp.text.pdf.PdfPages.WritePageTree()
 at iTextSharp.text.pdf.PdfWriter.Close()
 at iTextSharp.text.pdf.PdfCopy.Close()
 at iTextSharp.text.Document.Close()

It has 3 pages when I debugged it. What is wrong here??

Here is my code

public static MemoryStream MergePdfs(List<MemoryStream> pdfStreams)
       {
        //Create output stream
           MemoryStream OutStream = new MemoryStream();
           Document Document = null;

           try
           {
                //Create Main reader
                PdfReader Reader = new PdfReader(pdfStreams.ElementAt(0));
                //Create Main Doc
                Document = new Document(Reader.GetPageSizeWithRotation(1));
                //Create main writer
                PdfCopy Writer = new PdfCopy(Document, OutStream);
                //Open document for writing
                Document.Open();
                //Add pages
                AddPages(Reader.NumberOfPages, Reader, ref Writer);

                //For each additional pdf after first combine them into main document
                foreach (MemoryStream PdfStream in pdfStreams.Skip(1))
                {
                    PdfReader Reader2 = new PdfReader(PdfStream);
                    // Add content
                    AddPages(Reader2.NumberOfPages, Reader2, ref Writer);
                }
             }
             finally
             {
                 // Step 5: Close the document
                 if (Document != null)
                     Document.Close();

                foreach (var Strm in pdfStreams)
                {
                    try { if (null != Strm) Strm.Dispose(); } catch { }
                }
             }

             return OutStream;
         }

        private static void AddPages(int Pages, PdfReader reader, ref PdfCopy writer)
        {
            PdfImportedPage Page = null;

            for (int X = 0; X < Pages; X++)
            {
                 Page = writer.GetImportedPage(reader, X);
                 writer.AddPage(Page);
            }

            if (reader.AcroForm != null)
                writer.CopyAcroForm(reader);
        }
解决方案

Page numbers for GetImportedPage are 1-based. Thus:

for (int X = 1; X < Pages + 1; X++)
{
    Page = writer.GetImportedPage(reader, X);
    writer.AddPage(Page);
}

这篇关于合并PDF文件iTextSharp的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 04:53