Forráskód Böngészése

页面编辑-补充拆分功能

ZhouJieSheng 2 éve
szülő
commit
d5ac63613c

+ 2 - 0
PDF Office/CustomControl/DialogContent.cs

@@ -18,6 +18,8 @@ namespace PDF_Office.CustomControl
     /// <summary>
     /// 不能使用usercontrol来实现该模块 会导致content里的控件无法命名
     /// 需要创建自定义控件 customcontrol类型
+    /// 
+    /// Content里暂时不能包含region 会注册不上,需要留意
     /// </summary>
     public class DialogContent : Control
     {

+ 30 - 16
PDF Office/CustomControl/WritableComboBox.xaml

@@ -1,22 +1,36 @@
-<UserControl x:Class="PDF_Office.CustomControl.WritableComboBox"
-                   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
-             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
-             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
-             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
-             xmlns:local="clr-namespace:PDF_Office.CustomControl"
-             mc:Ignorable="d" 
-             d:DesignHeight="32" d:DesignWidth="200">
+<UserControl
+    x:Class="PDF_Office.CustomControl.WritableComboBox"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:local="clr-namespace:PDF_Office.CustomControl"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    d:DesignHeight="32"
+    d:DesignWidth="200"
+    mc:Ignorable="d">
     <Grid>
 
 
-        <ComboBox x:Name="writableComboBox" Grid.Column="1"  Visibility="Visible" MinHeight="32"  MinWidth="58"
-                                      SelectionChanged="writableComboBox_SelectionChanged" SelectedIndex="0">
-            <ComboBoxItem >全部页面</ComboBoxItem>
-            <ComboBoxItem >奇数页</ComboBoxItem>
-            <ComboBoxItem >偶数页</ComboBoxItem>
-            <ComboBoxItem >自定义页面</ComboBoxItem>
+        <ComboBox
+            x:Name="writableComboBox"
+            Grid.Column="1"
+            MinWidth="58"
+            MinHeight="32"
+            VerticalContentAlignment="Center"
+            SelectionChanged="writableComboBox_SelectionChanged"
+            Visibility="Visible">
+            <ComboBoxItem Visibility="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=local:WritableComboBox}, Path=IsAllPageVisible}">全部页面</ComboBoxItem>
+            <ComboBoxItem>奇数页</ComboBoxItem>
+            <ComboBoxItem>偶数页</ComboBoxItem>
+            <ComboBoxItem>自定义页面</ComboBoxItem>
         </ComboBox>
-        <TextBox  x:Name="writableTextBox" VerticalContentAlignment="Center"  Visibility="Hidden" MinHeight="{Binding ElementName=EnterableComboBox, Path=MinHeight}" Grid.Column="0" HorizontalAlignment="Left" TextChanged="writableTextBox_TextChange">
-        </TextBox>
+        <TextBox
+            x:Name="writableTextBox"
+            Grid.Column="0"
+            MinHeight="{Binding ElementName=writableComboBox, Path=MinHeight}"
+            HorizontalAlignment="Left"
+            VerticalContentAlignment="Center"
+            TextChanged="writableTextBox_TextChange"
+            Visibility="Hidden" />
     </Grid>
 </UserControl>

+ 25 - 0
PDF Office/CustomControl/WritableComboBox.xaml.cs

@@ -26,6 +26,7 @@ namespace PDF_Office.CustomControl
         public WritableComboBox()
         {
             InitializeComponent();
+            writableComboBox.SelectedIndex = 0;
         }
         private void writableComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
@@ -66,6 +67,30 @@ namespace PDF_Office.CustomControl
         public static readonly DependencyProperty IsCurrentPageProperty =
             DependencyProperty.Register("IsCurrentPage", typeof(bool), typeof(WritableComboBox), new PropertyMetadata(false));
 
+
+
+        public Visibility IsAllPageVisible
+        {
+            get { return (Visibility)GetValue(IsAllPageVisibleProperty); }
+            set { SetValue(IsAllPageVisibleProperty, value); 
+            }
+        }
+
+        // Using a DependencyProperty as the backing store for IsAllPageVisible.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty IsAllPageVisibleProperty =
+            DependencyProperty.Register("IsAllPageVisible", typeof(Visibility), typeof(WritableComboBox), new PropertyMetadata(Visibility.Visible,(d,e)=> {
+                if((Visibility)e.NewValue!=Visibility.Visible)
+                {
+
+                    (d as WritableComboBox).SetIndexByVisiblity((Visibility)e.NewValue);
+                }
+            }));
+
+        private void SetIndexByVisiblity(Visibility visible)
+        {
+            writableComboBox.SelectedIndex = 1;
+        }
+
         public bool CurrentPage
         {
             get { return (bool)GetValue(CurrentPageProperty); }

+ 129 - 0
PDF Office/Model/Dialog/HomePageToolsDialogs/HomePageSplitDialogModel.cs

@@ -15,6 +15,10 @@ namespace PDF_Office.Model.HomePageToolsDialogs
         /// </summary>
         public SplitMode Mode = SplitMode.AveragePages;
         /// <summary>
+        /// 页面范围形式
+        /// </summary>
+        public PageRangeMode PageMode = PageRangeMode.OddPage;
+        /// <summary>
         /// 拆分模式份数或者页数
         /// </summary>
         public int GetModeCount = 1;
@@ -22,6 +26,115 @@ namespace PDF_Office.Model.HomePageToolsDialogs
         /// 页面信息
         /// </summary>
         public string PageRange = "1,3-4,10";
+
+        private int pageSelectedIndex = 1;
+
+        public int PageSelectedIndex
+        {
+            get { return pageSelectedIndex; }
+            set
+            {
+                SetProperty(ref pageSelectedIndex, value);
+                switch (value)
+                {
+                    case 1:
+                        PageMode = PageRangeMode.OddPage;
+                        break;
+                    case 2:
+                        PageMode = PageRangeMode.EvenPage;
+                        break;
+                    case 3:
+                        PageMode = PageRangeMode.CustomPage;
+                        break;
+                    default:
+                        break;
+                }
+            }
+        }
+
+
+
+        private double pageNum = 1;
+        /// <summary>
+        /// 平均页数
+        /// </summary>
+        public double PageNum
+        {
+            get { return pageNum; }
+            set
+            {
+                SetProperty(ref pageNum, value);
+                GetModeCount = (int)value;
+            }
+        }
+
+        private double filesCount = 1;
+        /// <summary>
+        /// 平均文件数
+        /// </summary>
+        public double FilesCount
+        {
+            get { return filesCount; }
+            set
+            {
+                SetProperty(ref filesCount, value);
+                GetModeCount = (int)value;
+            }
+        }
+
+        private bool isAveragePages = true;
+        /// <summary>
+        /// 平均按页拆分 是否选中
+        /// </summary>
+        public bool IsAveragePages
+        {
+            get { return isAveragePages; }
+            set
+            {
+                SetProperty(ref isAveragePages, value);
+                if(value)
+                {
+                    Mode = SplitMode.AveragePages;
+                }
+            }
+        }
+
+        private bool isAveragePDF = false;
+        /// <summary>
+        /// 平均按PDF拆分 是否选中
+        /// </summary>
+        public bool IsAveragePDF
+        {
+            get { return isAveragePDF; }
+            set
+            {
+                SetProperty(ref isAveragePDF, value);
+                if (value)
+                {
+                    Mode = SplitMode.AverageFiles;
+                }
+            }
+        }
+
+
+        private bool isCustomRange = false;
+        /// <summary>
+        /// 自定义页面范围,是否选中
+        /// </summary>
+
+        public bool IsCustomRange
+        {
+            get { return isCustomRange; }
+            set
+            {
+                SetProperty(ref isCustomRange, value);
+                if (value)
+                {
+                    Mode = SplitMode.CustomPageRange;
+                }
+            }
+        }
+
         /// <summary>
         /// 文件名标签
         /// </summary>
@@ -164,5 +277,21 @@ namespace PDF_Office.Model.HomePageToolsDialogs
             /// </summary>
             CustomPageRange
         }
+
+        public enum PageRangeMode
+        {
+            /// <summary>
+            /// 奇数页
+            /// </summary>
+            OddPage,
+            /// <summary>
+            /// 偶数页
+            /// </summary>
+            EvenPage,
+            /// <summary>
+            /// 自定义页面范围
+            /// </summary>
+            CustomPage
+        }
     }
 }

+ 17 - 18
PDF Office/ViewModels/Dialog/PageEditDialogs/SplitDialogViewModel.cs

@@ -1,4 +1,6 @@
 using ComPDFKitViewer.PdfViewer;
+using PDF_Office.CustomControl;
+using PDF_Office.Helper;
 using PDF_Office.Model;
 using PDF_Office.Model.HomePageToolsDialogs;
 using Prism.Commands;
@@ -29,29 +31,16 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
             }
         }
 
-        private double perPage;
+        private string customPageText;
         /// <summary>
-        /// 平均PerPage页拆分为一个PDF
+        /// 自定义的输入内容
         /// </summary>
-        public double PerPage
+        public string CustomPageText 
         {
-            get { return perPage; }
+            get { return customPageText; }
             set
             {
-                SetProperty(ref perPage, value);
-            }
-        }
-
-        private double perPDF;
-        /// <summary>
-        /// 平均拆分为PerPDF个
-        /// </summary>
-        public double PerPDF
-        {
-            get { return perPDF; }
-            set
-            {
-                SetProperty(ref perPDF, value);
+                SetProperty(ref customPageText, value);
             }
         }
 
@@ -87,7 +76,17 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 
         private void split()
         {
+            List<int> pages = new List<int>();
+            var result = CommonHelper.GetPagesInRange(ref pages,CustomPageText,(int)pageCount,new char[]{ '-'},new char[] { ','});
+            if(!result)
+            {
+                AlertsMessage alertsMessage = new AlertsMessage();
+                alertsMessage.ShowDialog("","请输入有效范围","OK");
+                return;
+            }
+            Model.PageRange = customPageText;
             DialogParameters valuePairs = new DialogParameters();
+            valuePairs.Add(ParameterNames.DataModel,Model);
             RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
         }
 

+ 181 - 1
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -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>

+ 19 - 7
PDF Office/Views/Dialog/PageEditDialogs/SplitDialog.xaml

@@ -35,11 +35,14 @@
                             VerticalAlignment="Center"
                             Content="平均每"
                             GroupName="SplitMode"
-                            IsChecked="True" />
+                            IsChecked="{Binding Model.IsAveragePages, Mode=TwoWay}" />
                         <cus:NumericUpDown
+                            Height="32"
                             Margin="8,0"
                             IsEnabled="{Binding ElementName=RbtnPage, Path=IsChecked}"
-                            Minimum="1" />
+                            Maximum="{Binding PageCount}"
+                            Minimum="1"
+                            Value="{Binding Model.PageNum, Mode=TwoWay}" />
                         <TextBlock VerticalAlignment="Center" Text="页拆分为一个PDF文件" />
                     </StackPanel>
                     <StackPanel Margin="0,12" Orientation="Horizontal">
@@ -47,11 +50,15 @@
                             Name="RbtnPdf"
                             VerticalAlignment="Center"
                             Content="平均拆分为"
-                            GroupName="SplitMode" />
+                            GroupName="SplitMode"
+                            IsChecked="{Binding Model.IsAveragePDF, Mode=TwoWay}" />
                         <cus:NumericUpDown
+                            Height="32"
                             Margin="8,0"
                             IsEnabled="{Binding ElementName=RbtnPdf, Path=IsChecked}"
-                            Minimum="1" />
+                            Maximum="{Binding PageCount}"
+                            Minimum="1"
+                            Value="{Binding Model.FilesCount, Mode=TwoWay}" />
                         <TextBlock VerticalAlignment="Center" Text="个PDF文件" />
                     </StackPanel>
                     <StackPanel Margin="0,0,0,12" Orientation="Horizontal">
@@ -59,11 +66,16 @@
                             x:Name="RbtnPagerange"
                             VerticalAlignment="Center"
                             Content="按页面范围拆分"
-                            GroupName="SplitMode" />
-                        <ComboBox
+                            GroupName="SplitMode"
+                            IsChecked="{Binding Model.IsCustomRange, Mode=TwoWay}" />
+                        <cus:WritableComboBox
                             Width="110"
+                            Height="32"
                             Margin="8,0"
-                            IsEnabled="{Binding ElementName=RbtnPagerange, Path=IsChecked}" />
+                            IsAllPageVisible="Collapsed"
+                            IsEnabled="{Binding ElementName=RbtnPagerange, Path=IsChecked}"
+                            SelectedIndex="{Binding Model.PageSelectedIndex, Mode=TwoWay}"
+                            Text="{Binding CustomPageText, Mode=TwoWay}" />
                     </StackPanel>
                 </StackPanel>
                 <StackPanel Grid.Row="1" Margin="16,0">