/// <summary> /// Merges the PDF docs. /// </summary> /// <param name="pdfSources">The PDF sources.</param> /// <returns></returns> public virtual PDFDocument MergePdfDocs(List <Stream> pdfSources) { switch (pdfSources.Count) { case 0: return(new PDFDocument()); case 1: return(new PDFDocument(pdfSources[0])); default: return(PDFFile.MergeFiles(pdfSources.ToArray())); } }
//开始分卷 private void Btn_Start_Click(object sender, EventArgs e) { datas.Clear(); aj2.Clear(); if (String.IsNullOrEmpty(ArchiveChoicePath.Text) || String.IsNullOrEmpty(PartsChoicePath.Text) || String.IsNullOrEmpty(ExChoice.Text) || String.IsNullOrEmpty(ArchiveCatePath.Text)) { MessageBox.Show("请选择路径"); } else { Enble(false); FindEmptyDir(PartsChoicePath.Text); foreach (var item in nullDir) { nullDirListbox.Items.Add(item); } var pdf_path = PartsChoicePath.Text; IWorkbook wk = null; IWorkbook ajml = null; string extension = Path.GetExtension(ArchiveChoicePath.Text); string ajml_extension = Path.GetExtension(ArchiveCatePath.Text); try { //创建文件流 FileStream fs = File.OpenRead(ArchiveChoicePath.Text); FileStream ajml_fs = File.OpenRead(ArchiveCatePath.Text); if (extension.Equals(".xls")) { wk = new HSSFWorkbook(fs); } if (extension.Equals(".xlsx")) { wk = new XSSFWorkbook(fs); } if (ajml_extension.Equals(".xls")) { ajml = new HSSFWorkbook(ajml_fs); } if (ajml_extension.Equals(".xlsx")) { ajml = new XSSFWorkbook(ajml_fs); } //关闭文件流 fs.Close(); ajml_fs.Close(); // 读取案卷表 ISheet ajml_sheet = ajml.GetSheetAt(0); int ajrow = ajml_sheet.LastRowNum; int index = 0; try { for (int i = 1; i <= ajrow; i++) { index = i; var row = ajml_sheet.GetRow(i); var dh = row.GetCell(0).ToString(); //文件分数 int wj_num = int.Parse(row.GetCell(6).ToString()); //案卷页数 int wj_page = int.Parse(row.GetCell(10).ToString()); aj2.Add(dh, wj_page + "," + wj_num); } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } //读取文件表 ISheet sheet = wk.GetSheetAt(0); int nrow = sheet.LastRowNum; for (int i = 1; i <= nrow; i++) { try { //读取当前行数据 var row = sheet.GetRow(i); var page = 0; var ine = i; //案卷级档号 string aj_dh = row.GetCell(0).ToString(); //文件级档号 string wj_dh = row.GetCell(1).ToString(); var cp = row.GetCell(5).ToString(); page = Convert.ToInt32(cp); var next_page = page; var j = i + 1; if (j <= nrow) { var nextRow = sheet.GetRow(j); if (nextRow != null) { var str_end_page = nextRow.GetCell(5).ToString(); if (str_end_page.Length > 1) { next_page = Convert.ToInt32(str_end_page); } } } try { datas.Add(aj_dh + ',' + wj_dh, page + "," + next_page); } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } } catch (Exception ex) { //MessageBox.Show(ex.ToString() + i); } } //创建文件夹 foreach (var pageinfo in datas) { List <string> tempfilelist = new List <string>(); tempfilelist.Clear(); var dh = pageinfo.Key; var page = pageinfo.Value; var dharr = dh.Split(new char[1] { ',' }); var pagearr = page.Split(new char[1] { ',' }); var aj_dh = dharr[0]; var wj_dh = dharr[1]; var s_page = int.Parse(pagearr[0]); var e_page = int.Parse(pagearr[1]); var out_path = ExChoices + "\\" + wj_dh; if (!Directory.Exists(out_path)) { Directory.CreateDirectory(out_path); } try { string _o = aj2[aj_dh]; var infoarr = _o.Split(new char[1] { ',' }); var p1 = int.Parse(infoarr[0]); var num = int.Parse(infoarr[1]); if (num == 1 || s_page > e_page) { e_page = p1 + 1; } } catch (Exception ex) { //MessageBox.Show(ex.ToString()); } int count = 0; if (s_page < e_page) { for (int i = s_page; i < e_page; i++) { var fileinfo = new FileInfo(pdf_path + "\\" + aj_dh + "-" + i.ToString().PadLeft(3, '0') + ".pdf"); if (fileinfo.Exists) { tempfilelist.Add(fileinfo.FullName); count++; } } } else { var fileinfo = new FileInfo(pdf_path + "\\" + aj_dh + "-" + s_page.ToString().PadLeft(3, '0') + ".pdf"); if (fileinfo.Exists) { tempfilelist.Add(fileinfo.FullName); count++; } } if (count == 1) { var fileinfo = new FileInfo(pdf_path + "\\" + aj_dh + "-" + s_page.ToString().PadLeft(3, '0') + ".pdf"); File.Copy(fileinfo.FullName, out_path + "\\" + fileinfo.Name); Loglist.Add(fileinfo.Name + ".pdf分件成功"); } if (count > 1) { PDFFile.MergeFiles(out_path + @"\" + wj_dh + ".pdf", tempfilelist.ToArray()); Loglist.Add(wj_dh + ".pdf分件成功"); } } foreach (var item in Loglist) { ListLog.Items.Add(item); } DialogResult dialogResult = MessageBox.Show("是否删除空文件夹", "确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question); if (dialogResult == DialogResult.OK) { KillEmptyDirectory(ExChoices); MessageBox.Show("分件结束,空文件夹已清空"); Enble(true); } else { FindEmptyDirs(ExChoices); foreach (var item in outNullDir) { listBox1.Items.Add(item); } MessageBox.Show("分件结束"); Enble(true); } Btn_Start.Text = "开始分件"; } catch (Exception ex) { Console.WriteLine(ex); } } }