|
@@ -25,6 +25,8 @@ using PDF_Office.Helper;
|
|
|
using Microsoft.Win32;
|
|
|
using ComPDFKit.PDFDocument;
|
|
|
using PDF_Office.CustomControl;
|
|
|
+using PDF_Office.Model.HomePageToolsDialogs;
|
|
|
+using System.IO;
|
|
|
|
|
|
namespace PDF_Office.ViewModels.PageEdit
|
|
|
{
|
|
@@ -378,7 +380,13 @@ namespace PDF_Office.ViewModels.PageEdit
|
|
|
{
|
|
|
DialogParameters valuePairs = new DialogParameters();
|
|
|
valuePairs.Add(ParameterNames.PDFViewer, PDFViewer);
|
|
|
- dialogs.ShowDialog(DialogNames.SplitDialog, valuePairs, null);
|
|
|
+ dialogs.ShowDialog(DialogNames.SplitDialog, valuePairs,e=> {
|
|
|
+ if(e.Result==ButtonResult.OK&&e.Parameters!=null)
|
|
|
+ {
|
|
|
+ var model = e.Parameters.GetValue<HomePageSplitDialogModel>(ParameterNames.DataModel);
|
|
|
+ DoSplitPages(model);
|
|
|
+ }
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
@@ -946,6 +954,178 @@ namespace PDF_Office.ViewModels.PageEdit
|
|
|
IsLoading = Visibility.Collapsed;
|
|
|
}
|
|
|
|
|
|
+ private void DoSplitPages(HomePageSplitDialogModel model)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ var pdfViewer = PDFViewer;
|
|
|
+
|
|
|
+ System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
|
|
|
+ //dialog.Description = App.MainPageLoader.GetString("Main_OpenFolderNoteWarning"); ;
|
|
|
+ if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(dialog.SelectedPath))
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ return;
|
|
|
+
|
|
|
+ var data = model;
|
|
|
+ int PageCount = pdfViewer.Document.PageCount;
|
|
|
+ string selectedfile = "";
|
|
|
+ if (data.Mode == HomePageSplitDialogModel.SplitMode.AveragePages)
|
|
|
+ {
|
|
|
+ int perPages = Convert.ToInt32(data.GetModeCount);
|
|
|
+ int SplitCount = (int)Math.Ceiling((double)PageCount / (double)perPages);
|
|
|
+ //TODO:命名规则是否要调整
|
|
|
+ selectedfile = data.FileName + " " + "1.pdf";
|
|
|
+ for (int i = 0; i < SplitCount; i++)
|
|
|
+ {
|
|
|
+ int startNum = i * perPages + 1;
|
|
|
+ int endNum = Math.Min(PageCount, (i + 1) * perPages);
|
|
|
+ string range = startNum.ToString() + "-" + endNum.ToString();
|
|
|
+ CPDFDocument savedoc = CPDFDocument.CreateDocument();
|
|
|
+ bool result = savedoc.ImportPages(pdfViewer.Document, range);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ savedoc.Release();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ string filename = data.FileName + " " + (i + 1) + ".pdf";
|
|
|
+ string path = System.IO.Path.Combine(dialog.SelectedPath, filename);
|
|
|
+ path = CommonHelper.CreateFilePath(path);
|
|
|
+ result = savedoc.WriteToFilePath(path);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ savedoc.Release();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ savedoc.Release();
|
|
|
+ selectedfile = path;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (data.Mode == HomePageSplitDialogModel.SplitMode.AverageFiles)
|
|
|
+ {
|
|
|
+ int docCount = Convert.ToInt32(data.GetModeCount);
|
|
|
+ int avePages = (int)Math.Floor((double)PageCount / (double)docCount);
|
|
|
+ int remain = PageCount % docCount;
|
|
|
+ int startNum = 1;
|
|
|
+ //TODO:命名规则是否要调整
|
|
|
+ selectedfile = data.FileName + " " + "1.pdf";
|
|
|
+ for (int i = 0; i < docCount; i++)
|
|
|
+ {
|
|
|
+ int endNum = startNum + (avePages - 1);
|
|
|
+ if (i < remain) endNum++;
|
|
|
+ endNum = Math.Min(endNum, PageCount);
|
|
|
+ string range = startNum + "-" + endNum;
|
|
|
+ startNum = endNum + 1;
|
|
|
+ string fileName = data.FileName + " " + (i + 1) + ".pdf";
|
|
|
+ string path = System.IO.Path.Combine(dialog.SelectedPath, fileName);
|
|
|
+ path = CommonHelper.CreateFilePath(path);
|
|
|
+ CPDFDocument savedoc = CPDFDocument.CreateDocument();
|
|
|
+ bool result = savedoc.ImportPages(pdfViewer.Document, range);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ savedoc.Release();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ result = savedoc.WriteToFilePath(path);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ savedoc.Release();
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ savedoc.Release();
|
|
|
+ selectedfile = path;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else//自定义页码范围
|
|
|
+ {
|
|
|
+ string pageRange="";
|
|
|
+ if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.OddPage)
|
|
|
+ {
|
|
|
+ int[] page = new int[(PageCount + 1) / 2];
|
|
|
+ for (int i = 0; i < page.Length; i++)
|
|
|
+ {
|
|
|
+ page[i] = i * 2 + 1;
|
|
|
+ }
|
|
|
+ pageRange = string.Join(",", page);
|
|
|
+ }
|
|
|
+ else if (data.PageMode == HomePageSplitDialogModel.PageRangeMode.EvenPage)
|
|
|
+ {
|
|
|
+ int[] page = new int[PageCount / 2];
|
|
|
+ for (int i = 0; i < page.Length; i++)
|
|
|
+ {
|
|
|
+ page[i] = i * 2 + 2;
|
|
|
+ }
|
|
|
+ pageRange = string.Join(",", page);
|
|
|
+ }
|
|
|
+ List<int> pageList1 = new List<int>();
|
|
|
+ //如果是自定义输入页面范围的话,直接解析
|
|
|
+ CommonHelper.GetPagesInRange(ref pageList1, pageRange, PageCount, new char[] { ',' }, new char[] { '-' });
|
|
|
+ //part1
|
|
|
+ selectedfile = "";
|
|
|
+ CPDFDocument saveDoc1 = CPDFDocument.CreateDocument();
|
|
|
+ string filepath = data.FileName + " " + "1.pdf";
|
|
|
+ string path1 = Path.Combine(dialog.SelectedPath, filepath);
|
|
|
+ path1 = CommonHelper.CreateFilePath(path1);
|
|
|
+ bool result = saveDoc1.ImportPages(pdfViewer.Document, pageRange);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ saveDoc1.Release();
|
|
|
+ ShowTip = Visibility.Visible;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ result = saveDoc1.WriteToFilePath(path1);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ saveDoc1.Release();
|
|
|
+ ShowTip = Visibility.Visible;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ saveDoc1.Release();
|
|
|
+ //Part2
|
|
|
+ List<int> pageList2 = new List<int>();
|
|
|
+ for (int i = 1; i <= pdfViewer.Document.PageCount; i++)
|
|
|
+ {
|
|
|
+ if (!pageList1.Contains(i - 1))//pagelist1 存放的是index
|
|
|
+ pageList2.Add(i);
|
|
|
+ }
|
|
|
+ string pageRange2 = string.Join(",", pageList2);
|
|
|
+ CPDFDocument saveDoc2 = CPDFDocument.CreateDocument();
|
|
|
+ string filepath2 = data.FileName + " " + "2.pdf";
|
|
|
+ string path2 = Path.Combine(dialog.SelectedPath, filepath2);
|
|
|
+ path2 = CommonHelper.CreateFilePath(path2);
|
|
|
+ result = saveDoc2.ImportPages(pdfViewer.Document, pageRange2);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ saveDoc2.Release();
|
|
|
+ ShowTip = Visibility.Visible;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ result = saveDoc2.WriteToFilePath(path2);
|
|
|
+ if (!result)
|
|
|
+ {
|
|
|
+ saveDoc2.Release();
|
|
|
+ ShowTip = Visibility.Visible;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ selectedfile = path1;
|
|
|
+ saveDoc2.Release();
|
|
|
+ }
|
|
|
+ //显示文件夹,并选中一个文件
|
|
|
+ Process.Start("explorer", "/select,\"" + selectedfile + "\"");
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 将文件插入到指定位置
|
|
|
/// </summary>
|