Jelajahi Sumber

页面编辑-完善部分逻辑

ZhouJieSheng 2 tahun lalu
induk
melakukan
2ffd11dc46

+ 12 - 0
PDF Office/Helper/CommonHelper.cs

@@ -445,6 +445,18 @@ namespace PDF_Office.Helper
             return sizeWithUnit;
         }
 
+        /// <summary>
+        /// 将mm单位转换成pdf文件尺寸
+        /// </summary>
+        /// <param name="size"></param>
+        /// <returns></returns>
+        public static double GetPageSizeFomrUnit(double size)
+        {
+            double pagesize = 0;
+            pagesize = size / 25.4 * 72.0;
+            return pagesize;
+        }
+
         /// <summary>
         /// 返回设备DPI
         /// </summary>

+ 14 - 1
PDF Office/Model/PageEdit/PageEditItem.cs

@@ -43,7 +43,20 @@ namespace PDF_Office.Model.PageEdit
         /// <summary>
         /// Size类型的页面尺寸 用于插入功能
         /// </summary>
-        public Size Size { get; set; }
+        private Size size;
+
+        public Size Size
+        {
+            get { return size; }
+            set { size = value; 
+                //if(Size.Width>0&&Size.Height>0)
+                //{
+                //    var heigit = size.Height * 208 / size.Width;
+                //    ItemSize = new Size(208,heigit);
+                //}
+            }
+        }
+
 
         private BitmapSource image;
         /// <summary>

+ 166 - 45
PDF Office/ViewModels/Dialog/PageEditDialogs/InsertDialogViewModel.cs

@@ -1,4 +1,5 @@
-using PDF_Office.Model;
+using PDF_Office.Helper;
+using PDF_Office.Model;
 using PDF_Office.Model.PageEdit;
 using Prism.Commands;
 using Prism.Mvvm;
@@ -15,7 +16,7 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 {
     public class InsertDialogViewModel : BindableBase, IDialogAware
     {
-        public string Title =>"";
+        public string Title => "";
 
         /// <summary>
         /// 数据模型
@@ -24,6 +25,8 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 
         public event Action<IDialogResult> RequestClose;
 
+        private Size size = new Size();
+
         private string currentPageSize;
 
         /// <summary>
@@ -49,7 +52,7 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
             set
             {
                 SetProperty(ref itemSelectedIndex, value);
-                if(Model!=null)
+                if (Model != null)
                 {
                     Model.filepath = Pages[itemSelectedIndex].FilePath;
                 }
@@ -96,6 +99,75 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
             }
         }
 
+        private int pageSizeSelectedIndex = 0;
+
+        /// <summary>
+        /// 页面尺寸选中项
+        /// </summary>
+        public int PageSizeSelectedIndex
+        {
+            get { return pageSizeSelectedIndex; }
+            set
+            {
+                SetProperty(ref pageSizeSelectedIndex, value);
+            }
+        }
+
+        private bool isCurrentSelected = true;
+
+        public bool IsCurrentSelected
+        {
+            get { return isCurrentSelected; }
+            set { SetProperty(ref isCurrentSelected, value); }
+        }
+
+        private bool isStandSelcted;
+
+        public bool IsStandSelected
+        {
+            get { return isStandSelcted; }
+            set
+            {
+                SetProperty(ref isStandSelcted, value);
+            }
+        }
+
+        private bool isCustomSelected;
+
+        public bool IsCustomSelected
+        {
+            get { return isCustomSelected; }
+            set
+            {
+                SetProperty(ref isCustomSelected, value);
+            }
+        }
+
+        private bool isVerticalSelected = true;
+
+        public bool IsVerticalSelected
+        {
+            get { return isVerticalSelected; }
+            set
+            {
+                SetProperty(ref isVerticalSelected, value);
+            }
+        }
+
+        private bool isHorizontalSelected;
+
+        public bool IsHorizontalSelected
+        {
+            get { return isHorizontalSelected; }
+            set
+            {
+                SetProperty(ref isHorizontalSelected, value);
+            }
+        }
+
+
+
+
 
         /// <summary>
         /// 自定义页面的路径集合
@@ -107,30 +179,43 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
         /// </summary>
         public List<string> Units { get; set; }
 
+        public List<string> PageSize { get; set; }
 
         public DelegateCommand CancelCommand { get; set; }
 
         public DelegateCommand InsertCommnad { get; set; }
 
-        /// <summary>
-        /// 页面方向选择的事件
-        /// </summary>
-        public DelegateCommand<string> OrientationCheckedCommand { get; set; }
-
         public InsertDialogViewModel()
         {
             InitPageSource();
             InitUnits();
+            InitPage();
 
             CancelCommand = new DelegateCommand(cancel);
             InsertCommnad = new DelegateCommand(insert);
-            OrientationCheckedCommand = new DelegateCommand<string>(OrientationChecked);
+        }
+
+        private void InitPage()
+        {
+            PageSize = new List<string>();
+            PageSize.Add("Letter(8.5x11 inches)");
+            PageSize.Add("Legal (210 x 297mm)");
+            PageSize.Add("A3 (297 x 420mm)");
+            PageSize.Add("A4 (210 x 297mm)");
+            PageSize.Add("A5 (148 x 210mm)");
+            PageSize.Add("B4 (250 x 353mm)");
+            PageSize.Add("B5 (176 x 250mm)");
+            PageSize.Add("Executive (7.25x10.5 inches)");
+            PageSize.Add("US4x6 (4x6 inches)");
+            PageSize.Add("US4x8 (4x8 inches)");
+            PageSize.Add("US5x7 (5x7 inches)");
+            PageSize.Add("Comm10 (4.125x9.5 inches)");
         }
 
         /// <summary>
         /// 初始化页面大小单位集合
         /// </summary>
-        private  void InitUnits()
+        private void InitUnits()
         {
             Units = new List<string>();
             Units.Add("mm");
@@ -144,41 +229,12 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
         private void InitPageSource()
         {
             Pages = new ObservableCollection<CustomPageItem>();
-            Pages.Add(new CustomPageItem() {Name="空白页",FilePath = ""});
-            Pages.Add(new CustomPageItem() { Name="横线",FilePath= System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg")});
+            Pages.Add(new CustomPageItem() { Name = "空白页", FilePath = "" });
+            Pages.Add(new CustomPageItem() { Name = "横线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\HorizontalLine.jpg") });
             Pages.Add(new CustomPageItem() { Name = "五线谱", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\Staff.jpg") });
             Pages.Add(new CustomPageItem() { Name = "格子线", FilePath = System.IO.Path.Combine(Environment.CurrentDirectory, @"Resources\PageEdit\GridLine.jpg") });
         }
 
-        /// <summary>
-        /// 页面方向选中事件
-        /// </summary>
-        /// <param name="orientation"></param>
-        private void OrientationChecked(string orientation)
-        {
-            switch (orientation)
-            {
-                //如果宽高不符合条件就对调宽高
-                case "Vertical":
-                    if(Model.height<=Model.width)
-                    {
-                        var temp = Model.height;
-                        Model.height = Model.width;
-                        Model.width = Model.height;
-                    }
-                    break;
-                case "Horizontal":
-                    if (Model.height> Model.width)
-                    {
-                        var temp = Model.height;
-                        Model.height = Model.width;
-                        Model.width = Model.height;
-                    }
-                    break;
-                default:
-                    break;
-            }
-        }
 
         private void cancel()
         {
@@ -187,7 +243,72 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 
         private void insert()
         {
+            //最后统一处理页面尺寸
+            if(IsCurrentSelected)
+            {
+                Model.width = (int)size.Width;
+                Model.height = (int)size.Height;
+            }
+            else if(IsStandSelected)
+            {
+                //TODO:
+            }
+            else
+            {
+                int width = (int)size.Width;
+                int height = (int)size.Height;
+                int.TryParse(customWidth,out width);
+                int.TryParse(customHeight,out height);
+                if(width <= 0)
+                {
+                    width = (int)size.Width;
+                }
+                if(height<=0)
+                {
+                    height = (int)size.Height;
+                }
+                switch (unitsSelectedIndex)
+                {
+                    case 1:
+                        width = width / 10;
+                        height = height / 10;
+                        break;
+                    case 2:
+                        width = (int)(width / 25.4);
+                        height = (int)(height / 25.4);
+                        break;
+                    case 0:
+                    default:
+                        break;
+                }
+                Model.width = width;
+                Model.height = height;
+            }
+
+            //方向处理
+            if (isVerticalSelected)
+            {
+                if (Model.height <= Model.width)
+                {
+                    var temp = Model.height;
+                    Model.height = Model.width;
+                    Model.width = Model.height;
+                }
+            }
+            else
+            {
+                if (Model.height > Model.width)
+                {
+                    var temp = Model.height;
+                    Model.height = Model.width;
+                    Model.width = temp;
+                }
+            }
+
             DialogParameters valuePairs = new DialogParameters();
+            //将mm转换成像素单位
+            Model.width = (int)CommonHelper.GetPageSizeFomrUnit(Model.width);
+            Model.height = (int)CommonHelper.GetPageSizeFomrUnit(Model.height);
             valuePairs.Add(ParameterNames.DataModel, Model);
             RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
         }
@@ -201,15 +322,15 @@ namespace PDF_Office.ViewModels.Dialog.PageEditDialogs
 
         public void OnDialogClosed()
         {
-        
+
         }
 
         public void OnDialogOpened(IDialogParameters parameters)
         {
-            if(parameters!=null)
+            if (parameters != null)
             {
-                var size = parameters.GetValue<Size>("CurrentPageSize");
-                CurrentPageSize = $"({size.Width.ToString("F2")}mm*{size.Height.ToString("F2")}mm)";
+                size = parameters.GetValue<Size>("CurrentPageSize");
+                CurrentPageSize = $"({size.Width.ToString("F0")}mm*{size.Height.ToString("F0")}mm)";
                 CustomWidth = size.Width.ToString("F2");
                 CustomHeight = size.Height.ToString("F2");
                 Model.width = (int)size.Width;

+ 0 - 7
PDF Office/ViewModels/Dialog/PageEditDialogs/SplitDialogViewModel.cs

@@ -77,13 +77,6 @@ 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);

+ 33 - 10
PDF Office/ViewModels/PageEdit/PageEditContentViewModel.cs

@@ -594,7 +594,10 @@ namespace PDF_Office.ViewModels.PageEdit
             else
             {
                 DialogParameters keyValuePairs = new DialogParameters();
-                keyValuePairs.Add("CurrentPageSize", PDFViewer.Document.GetPageSize(ListSelectedIndex));
+                Size size = new Size();
+                size.Width =CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Width);
+                size.Height = CommonHelper.GetUnitsFromPageSize(PDFViewer.Document.GetPageSize(listSelecedIndex).Height);
+                keyValuePairs.Add("CurrentPageSize", size);
                 //插入自定义页面
                 dialogs.ShowDialog(DialogNames.InsertDialog, keyValuePairs,e=> { 
                     if(e.Result == ButtonResult.OK&&e.Parameters!=null)
@@ -1719,17 +1722,37 @@ namespace PDF_Office.ViewModels.PageEdit
         /// <returns></returns>
         private bool InsertFromCustomPage(int insertIndex,double width,double height,string filePath)
         {
-            //文档层插入
-            var result = PDFViewer.Document.InsertPage(insertIndex, width, height, string.IsNullOrEmpty(filePath)?null:filePath);
-            if (!result)
+            try
             {
-                ShowToast();
-                return false;
+                bool result = false;
+                //文档层插入
+                if (!string.IsNullOrEmpty(filePath))
+                {
+                    BitmapFrame frame = BitmapFrame.Create(new Uri(filePath));
+                    byte[] imageData = new byte[frame.PixelWidth * frame.PixelHeight * 4];
+                    frame.CopyPixels(imageData, frame.PixelWidth * 4, 0);
+                    result = PDFViewer.Document.InsertPage(insertIndex, frame.Width, frame.Height, imageData, CPDFDocumentImageMode.CPDFDocumentImageModeScaleToFill);
+                }
+                else
+                {
+                    result = PDFViewer.Document.InsertPage(insertIndex, width, height, null);
+                }
+                if (!result)
+                {
+                    ShowToast();
+                    return false;
+                }
+                PageEditItem item = new PageEditItem();
+                if (width > height)
+                {
+                    item.isVertical = false;
+                }
+                PageEditItems.Insert(insertIndex, item);
+                ReloadAfterOption(true, true, Tuple.Create(insertIndex - 1, insertIndex + 1));
+                return true;
             }
-            PageEditItem item = new PageEditItem();
-            PageEditItems.Insert(insertIndex, item);
-            ReloadAfterOption(true, true, Tuple.Create(insertIndex - 1, insertIndex + 1));
-            return true;
+            catch
+            { return false; }
         }
 
         /// <summary>

+ 13 - 20
PDF Office/Views/Dialog/PageEditDialogs/InsertDialog.xaml

@@ -95,7 +95,7 @@
                             VerticalContentAlignment="Center"
                             Content="当前页"
                             GroupName="PageSize"
-                            IsChecked="True" />
+                            IsChecked="{Binding IsCurrentSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <Label
                             Height="32"
                             Margin="8,0"
@@ -109,26 +109,30 @@
                             Name="RbtnStandard"
                             VerticalAlignment="Center"
                             Content="标准"
-                            GroupName="PageSize" />
+                            GroupName="PageSize"
+                            IsChecked="{Binding IsStandSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <ComboBox
                             Width="228"
                             Height="32"
                             Margin="8,0"
-                            IsEnabled="{Binding ElementName=RbtnStandard, Path=IsChecked}" />
+                            IsEnabled="{Binding ElementName=RbtnStandard, Path=IsChecked}"
+                            ItemsSource="{Binding PageSize}"
+                            SelectedIndex="{Binding PageSizeSelectedIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                     </StackPanel>
                     <StackPanel Margin="0,0,0,12" Orientation="Horizontal">
                         <RadioButton
                             Name="RbtnCustom"
                             VerticalAlignment="Center"
                             Content="自定义"
-                            GroupName="PageSize" />
+                            GroupName="PageSize"
+                            IsChecked="{Binding IsCustomSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <TextBox
                             Width="80"
                             Height="32"
                             Margin="8,0"
                             VerticalContentAlignment="Center"
                             IsEnabled="{Binding ElementName=RbtnCustom, Path=IsChecked}"
-                            Text="{Binding CustomWidth, Mode=TwoWay}" />
+                            Text="{Binding CustomWidth, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <TextBlock VerticalAlignment="Center" Text="X" />
                         <TextBox
                             Width="80"
@@ -136,7 +140,7 @@
                             Margin="8,0"
                             VerticalContentAlignment="Center"
                             IsEnabled="{Binding ElementName=RbtnCustom, Path=IsChecked}"
-                            Text="{Binding CustomHeight, Mode=TwoWay}" />
+                            Text="{Binding CustomHeight, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <ComboBox
                             Width="80"
                             VerticalContentAlignment="Center"
@@ -154,23 +158,12 @@
                         <RadioButton
                             Margin="0,12,0,16"
                             Content="纵向页面"
-                            IsChecked="True">
-                            <i:Interaction.Triggers>
-                                <i:EventTrigger EventName="Checked">
-                                    <i:InvokeCommandAction Command="{Binding OrientationCheckedCommand}" CommandParameter="Vertical" />
-                                </i:EventTrigger>
-                            </i:Interaction.Triggers>
-                        </RadioButton>
+                            IsChecked="{Binding IsVerticalSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                         <RadioButton
                             Margin="24,12,0,16"
                             Content="横向页面"
-                            Tag="Vertical">
-                            <i:Interaction.Triggers>
-                                <i:EventTrigger EventName="Checked">
-                                    <i:InvokeCommandAction Command="{Binding OrientationCheckedCommand}" CommandParameter="Horizontal" />
-                                </i:EventTrigger>
-                            </i:Interaction.Triggers>
-                        </RadioButton>
+                            IsChecked="{Binding IsHorizontalSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                            Tag="Vertical" />
                     </StackPanel>
                 </StackPanel>
             </Grid>

+ 3 - 1
PDF Office/Views/Dialog/PageEditDialogs/SplitDialog.xaml

@@ -72,10 +72,12 @@
                             Width="110"
                             Height="32"
                             Margin="8,0"
+                            
                             IsAllPageVisible="Collapsed"
                             IsEnabled="{Binding ElementName=RbtnPagerange, Path=IsChecked}"
+                            MaxPageRange="{Binding PageCount}"
                             SelectedIndex="{Binding Model.PageSelectedIndex, Mode=TwoWay}"
-                            Text="{Binding CustomPageText, Mode=TwoWay}" />
+                            Text="{Binding CustomPageText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                     </StackPanel>
                 </StackPanel>
                 <StackPanel Grid.Row="1" Margin="16,0">

+ 1 - 1
PDF Office/Views/PageEdit/PageEditContent.xaml

@@ -134,7 +134,7 @@
                     Command="{Binding ShowPageSizeCommand}"
                     Header="隐藏页面大小"
                     Visibility="{Binding HasShowPageSize}" />
-                <MenuItem Header="打印页面" Command="{Binding PrintCommand}"/>
+                <MenuItem Command="{Binding PrintCommand}" Header="打印页面" />
             </ContextMenu>
         </ResourceDictionary>
     </UserControl.Resources>