mingmingruyue20160420

C#PDF根据指定范围进行拆分

2016-10-18 11:01  Moon-明月  阅读(848)  评论(0编辑  收藏  举报
/// <summary>
        /// Pdf拆分方法
        /// </summary>
        /// <param name="inputFileName">源文件夹下所有PDF文件</param>
        /// <param name="outputPath">拆分之后的pdf保存【路径】(名字为生成)</param>
        public bool PdfSplite(string inputFileName, string outputPath)
        {
            int iindex = inputFileName.LastIndexOf("\\");
            string strPdfName = inputFileName.Substring(iindex+1);
            try
            {
                
                int rotation = 0;
                PdfReader pdfReader = new PdfReader(inputFileName);
                string fileName = inputFileName.Substring(0, inputFileName.Length - 4);
                int totalPages = pdfReader.NumberOfPages;
                if ((totalPages<0))
                {
                    MessageBox.Show("此pdf为空!");
                    PDFSort.ErrorLog.WriteError(inputFileName+"此pdf为空页!");
                   return false;
                }
                #region 新抽取的pdf命名带有_页数
                //2016-09-27 新抽取的pdf命名去掉了_页数
                //else if (totalPages <10&&totalPages>0)
                //{
                //    //小于10页
                //    File.Copy(inputFileName, string.Format("{0}\\{1}_{2}.pdf", outputPath, Path.GetFileNameWithoutExtension(inputFileName), "0"+totalPages.ToString()), true);
                //}
                //else if (totalPages <= 12 && totalPages >=10)
                //{
                //    //小于12页
                //    File.Copy(inputFileName, string.Format("{0}\\{1}_{2}.pdf", outputPath, Path.GetFileNameWithoutExtension(inputFileName), totalPages), true);
                //} 
                #endregion
                else
                {
                    //var newPdf = string.Format("{0}\\{1}_12.pdf", outputPath, Path.GetFileNameWithoutExtension(inputFileName));
                    var newPdf = string.Format("{0}\\{1}.pdf", outputPath, Path.GetFileNameWithoutExtension(inputFileName));
                    FileStream fs = new FileStream(newPdf, FileMode.Create);
                    Document newDocument = new Document();
                    PdfWriter pdfWriter = PdfWriter.GetInstance(newDocument, fs);
                    pdfWriter.CloseStream = true;
                    newDocument.Open();
                    PdfContentByte pdfContentByte = pdfWriter.DirectContent;
                    for (int i = 0; i < totalPages; i++)
                    {
                        if (i < 6 || i >= totalPages - 6)
                        {
                            newDocument.SetPageSize(pdfReader.GetPageSizeWithRotation(i + 1));
                            newDocument.NewPage();
                            PdfImportedPage importedPage = pdfWriter.GetImportedPage(pdfReader, i + 1);
                            rotation = pdfReader.GetPageRotation(i + 1);
                            pdfContentByte.AddTemplate(importedPage, 0, 0);
                            if (rotation == 90 || rotation == 270)
                            {
                                pdfContentByte.AddTemplate(importedPage, 0, -1f, 1f, 0, 0, pdfReader.GetPageSizeWithRotation(i + 1).Height);
                            }
                            else
                            {
                                pdfContentByte.AddTemplate(importedPage, 1f, 0, 0, 1f, 0, 0);
                            }
                        }
                    }
                    fs.Flush();
                    newDocument.Close();

                    return true;
                }
            }
            catch (Exception ex)
            {
                ErrorLog.WriteError(ex.ToString());
                return false;
            }
        }

 

分类:

技术点:

相关文章: