//Pre-Condition: // string destinationFile, the file which will be saved for output after merger // string[] sourceFiles, a string array of the location of the files to be merged // string[] pageName, a string array of the bookmark names for each pdf file to be merged //Post-Condition: // the seperate pdf files are merged with bookmarks added then saved to the destinationFile public static void MergeFiles(string destinationFile, string[] sourceFiles, string[] pageName) { int TotalPages = 0;//总页码 try { int f = 0; PdfReader reader = new PdfReader(sourceFiles[f]); int n = reader.NumberOfPages; TotalPages += n; Document document = new Document(reader.getPageSizeWithRotation(1)); PdfWriter writer = PdfWriter.getInstance(document, new FileStream(destinationFile, FileMode.Create)); //每列显示一页 //显示大纲 //页面大小适合窗口 writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn | PdfWriter.PageModeUseOutlines | PdfWriter.FitWindow); document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; int pdfPageName = 0; bool pdfNewPageFlag = true; while (f < sourceFiles.Length) { int i = 0; while (i < n)//n为当前导入文件的页次 { i++; //取得页面方向 document.setPageSize(reader.getPageSizeWithRotation(i)); //新建一页 document.newPage(); if (pdfNewPageFlag == true) { //建立转向标记 PdfAction a = PdfAction.gotoLocalPage(TotalPages, new PdfDestination(PdfDestination.FITH), writer); //建立在纲 PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, pageName[f]); pdfNewPageFlag = false; pdfPageName++; } page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).Height); } else { cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } } f++; if (f < sourceFiles.Length) { reader = new PdfReader(sourceFiles[f]); n = reader.NumberOfPages; TotalPages += n;//累计页码 pdfNewPageFlag = true; } } document.Close(); } catch { //Console.Error.WriteLine(e.Message); //Console.Error.WriteLine(e.StackTrace); } }
//合并文件 #region public static MemoryStream MergeFiles(ArrayList sourceFiles, ArrayList pageName) { int f = 0; //生成数据流初始化 MemoryStream ms = new MemoryStream(); PdfReader reader = new PdfReader(sourceFiles[f].ToString()); int n = reader.NumberOfPages; Document document = new Document(reader.getPageSizeWithRotation(1)); //打开 Document 生成 PDF PdfWriter writer = PdfWriter.getInstance(document, ms); //每列显示一页,显示大纲,页面大小适合窗口 writer.setViewerPreferences(PdfWriter.PageLayoutOneColumn | PdfWriter.PageModeUseOutlines | PdfWriter.FitWindow); try { #region document.Open(); PdfContentByte cb = writer.DirectContent; PdfImportedPage page; int rotation; int curPage = 1;//页码 while (f < sourceFiles.Count) { ////建立转向标记 //PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); //PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, pageName[f].ToString()); string bookmark = pageName[f].ToString(); if (f == 0) { //建立转向标记 PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); } else { int index = sourceFiles[f - 1].ToString().LastIndexOf('\\'); string lastfileName = sourceFiles[f - 1].ToString().Substring(index + 1, 10); string fileName = sourceFiles[f].ToString().Substring(index + 1, 10); if (fileName.Substring(0, 2) == "AA") { if (lastfileName != fileName) { //建立转向标记 PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); } } else { //建立转向标记 PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); } } int i = 0; while (i < n)//n为当前导入文件的页数 { i++; //取得页面方向 document.setPageSize(reader.getPageSizeWithRotation(i)); //新建一页 document.newPage(); #region 已注释 //if (i == 1) //{ // //建立转向标记 // PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); // PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, pageName[f].ToString()); //} //string bookmark = pageName[f].ToString(); //if (f == 0) //{ // //建立转向标记 // PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); // PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); //} //else //{ // int index = sourceFiles[f - 1].ToString().LastIndexOf('\\'); // string lastfileName = sourceFiles[f - 1].ToString().Substring(index + 1, 10); // string fileName = sourceFiles[f].ToString().Substring(index + 1, 10); // if (fileName.Substring(0, 2) == "AA") // { // if (lastfileName != fileName) // { // //建立转向标记 // PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); // PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); // } // } // else // { // //建立转向标记 // PdfAction a = PdfAction.gotoLocalPage(curPage, new PdfDestination(PdfDestination.FITH), writer); // PdfOutline outLine = new PdfOutline(writer.DirectContent.RootOutline, a, bookmark); // } //} #endregion page = writer.getImportedPage(reader, i); rotation = reader.getPageRotation(i); if (rotation == 90 || rotation == 270) { cb.addTemplate(page, 0, -1f, 1f, 0, 0, reader.getPageSizeWithRotation(i).Height); } else { cb.addTemplate(page, 1f, 0, 0, 1f, 0, 0); } curPage++; } f++; //下一份文件 if (f < sourceFiles.Count) { string test = sourceFiles[f].ToString(); if (System.IO.File.Exists(sourceFiles[f].ToString())) { try { reader = new PdfReader(sourceFiles[f].ToString()); n = reader.NumberOfPages; } catch { } } } } #endregion } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { document.Close(); } return(ms); }