当前位置:   article > 正文

一个PDF合并操作实现类,利用iTextSharp控件实现_itextsharp在新的pdf文件加入其他pdf

itextsharp在新的pdf文件加入其他pdf

http://blog.csdn.net/yinshuhai/article/details/3042702


using iTextSharp.text;

using iTextSharp.text.pdf;

/// <summary>

    /// pdf文件合并处理类

    /// </summary>

    class PDFMergeManager

    {

        private PdfWriter pw;

        private PdfReader reader;       

        private Document document;

        private PdfContentByte cb;

        private PdfImportedPage newPage;

        /// <summary>

        /// 通过输出文件来构建合并管理,合并到新增文件中,合并完成后调用FinishedMerge方法

        /// </summary>

        /// <param name="sOutFiles"></param>

        public PDFMergeManager(string sOutFiles)

        {

            document = new Document(PageSize.A3);         

            if (File.Exists(sOutFiles) && !MessageHelper.ShowQuestion("对应的文件已经存在,文件目录:/r/n "+sOutFiles+"/r/n是否覆盖该文件?"))

            {

                document = null;

                throw new IOException("用户取消操作");                

            }

            pw = PdfWriter.GetInstance(document, new FileStream(sOutFiles, FileMode.Create));

            document.Open();

            cb = pw.DirectContent;

        }       

        /// <summary>

        /// 通过文件流来合并文件,合并到当前的可写流中,合并完成后调用FinishedMerge方法

        /// </summary>

        /// <param name="sm"></param>

        public PDFMergeManager(Stream sm)

        {

            document = new Document();

            pw = PdfWriter.GetInstance(document, sm);            

            document.Open();

            cb = pw.DirectContent;

        }

        /// <summary>

        /// 合并文件

        /// </summary>

        /// <param name="sFiles">需要合并的文件路径名称</param>

        /// <returns></returns>

        public bool MergeFile(string sFiles)

        {

            reader = new PdfReader(sFiles);           

            {

                int iPageNum = reader.NumberOfPages;

                for (int j = 1; j <= iPageNum; j++)

                {

                    

                    newPage = pw.GetImportedPage(reader, j);

                    Rectangle r = reader.GetPageSize(j);

                    document.SetPageSize(r);

                    document.NewPage();

                    cb.AddTemplate(newPage, 0, 0);                    

                }

               

            }

            reader.Close();            

            return true;

        }

        /// <summary>

        /// 通过字节数据合并文件

        /// </summary>

        /// <param name="pdfIn">PDF字节数据</param>

        /// <returns></returns>

        public bool MergeFile(byte[] pdfIn)

        {

            reader = new PdfReader(pdfIn);

            {

                int iPageNum = reader.NumberOfPages;

                for (int j = 1; j <= iPageNum; j++)

                {

 

                    newPage = pw.GetImportedPage(reader, j);

                    Rectangle r = reader.GetPageSize(j);

                    document.SetPageSize(r);

                    document.NewPage();

                    cb.AddTemplate(newPage, 0, 0);

                }

 

            }

            reader.Close();

            return true;

        }

        /// <summary>

        /// 通过PDF文件流合并文件

        /// </summary>

        /// <param name="pdfStream">PDF文件流</param>

        /// <returns></returns>

        public bool MergeFile(Stream pdfStream)

        {

            reader = new PdfReader(pdfStream);

            {

                int iPageNum = reader.NumberOfPages;

                for (int j = 1; j <= iPageNum; j++)

                {

 

                    newPage = pw.GetImportedPage(reader, j);

                    Rectangle r = reader.GetPageSize(j);

                    document.SetPageSize(r);

                    document.NewPage();

                    cb.AddTemplate(newPage, 0, 0);

                }

 

            }

            reader.Close();

            return true;

        }

        /// <summary>

        /// 通过网络地址来合并文件

        /// </summary>

        /// <param name="pdfUrl">需要合并的PDF的网络路径</param>

        /// <returns></returns>

        public bool MergeFile(Uri pdfUrl)

        {

            reader = new PdfReader(pdfUrl);

            {

                int iPageNum = reader.NumberOfPages;

                for (int j = 1; j <= iPageNum; j++)

                {

 

                    newPage = pw.GetImportedPage(reader, j);

                    Rectangle r = reader.GetPageSize(j);

                    document.SetPageSize(r);

                    document.NewPage();

                    cb.AddTemplate(newPage, 0, 0);

                }

 

            }

            reader.Close();

            return true;

        }

        /// <summary>

        /// 完成合并

        /// </summary>

        public void FinishedMerge()

        {

            try

            {

                if (reader != null)

                {

                    reader.Close();

                }

                if (pw != null)

                {

                    pw.Flush();

                    pw.Close();

                }

                if (document.IsOpen())

                {

                    document.Close();

                }

            }

            catch

            {

 

            }

        }        

    }

 


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/1018991
推荐阅读
  

闽ICP备14008679号