123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKitViewer.PdfViewer;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace ComPDFKitDemo.PageEditor
- {
- /// <summary>
- /// CropWindow.xaml 的交互逻辑
- /// </summary>
- public partial class CropWindow : Window
- {
- public CPDFViewer CurrentViewer { get; set; }
- private Rect CropRect { get; set; }
- private CPDFDocument Document { get; set; }
- private int CropPageUI { get; set; }
- public CropWindow()
- {
- InitializeComponent();
- }
- public void SetDoc(CPDFDocument doc)
- {
- Document = doc;
-
- CropUI.CurrentDoc = Document;
- PageCountText.Text = "of / "+ Document.PageCount;
- CropPageUI = CurrentViewer.CurrentIndex;
- CropUI.CropPageUI = CropPageUI;
- StartPageText.Text = (CropPageUI+1).ToString();
- EndPageText.Text = (CropPageUI + 1).ToString();
- CropUI.InvalidateVisual();
- }
- private int IgnorePrecision(double value)
- {
- int check=(int)value;
- if (check <= 2)
- {
- return 0;
- }
- return check;
- }
- private void CropUI_CropChanged(object sender, KeyValuePair<Rect, Rect> e)
- {
- CropRect = e.Key;
- LeftText.Text = Math.Max(0, IgnorePrecision(e.Key.Left- e.Value.Left)).ToString();
- TopText.Text = Math.Max(0, IgnorePrecision(e.Key.Top - e.Value.Top)).ToString();
- RightText.Text = Math.Max(0, IgnorePrecision(e.Value.Right - e.Key.Right)).ToString();
- BottomText.Text = Math.Max(0, IgnorePrecision(e.Value.Bottom - e.Key.Bottom)).ToString();
- }
- private void CancelButton_Click(object sender, RoutedEventArgs e)
- {
- Close();
- }
- private void SaveButton_Click(object sender, RoutedEventArgs e)
- {
- if(CropRect!=Rect.Empty&& Document!=null&& Document.PageCount>0)
- {
- int startPage = CropPageUI;
- int endPage = CropPageUI;
- if (RangeRadioBtn.IsChecked==true)
- {
- if(int.TryParse(StartPageText.Text,out startPage))
- {
- startPage -= 1;
- }
- if (int.TryParse(EndPageText.Text, out endPage))
- {
- endPage -= 1;
- }
- }
- else
- {
- startPage = 0;
- endPage = Document.PageCount - 1;
-
- }
- List<int> cropPageList=new List<int>();
- for (int i = startPage; i <= endPage && i < Document.PageCount; i++)
- {
- cropPageList.Add(i);
- }
- if(cropPageList.Count>0)
- {
- CurrentViewer?.CropPage((CPDFDisplayBox)CropTypeBox.SelectedIndex, CropRect, cropPageList);
- }
- }
- Close();
- }
- private void RadioButton_Checked(object sender, RoutedEventArgs e)
- {
- RadioButton checkRadioBtn=sender as RadioButton;
- if(checkRadioBtn!=null && checkRadioBtn.Tag!=null)
- {
- if(checkRadioBtn.Tag.ToString()== "AllRadioBtn")
- {
- StartPageText.IsEnabled = false;
- EndPageText.IsEnabled = false;
- return;
- }
- if (checkRadioBtn.Tag.ToString() == "RangeRadioBtn")
- {
- StartPageText.IsEnabled = true;
- EndPageText.IsEnabled = true;
- return;
- }
- }
- }
- private void RefreshButton_Click(object sender, RoutedEventArgs e)
- {
- int left, top, right, bottom;
- int.TryParse(LeftText.Text, out left);
- int.TryParse(TopText.Text, out top);
- int.TryParse(RightText.Text, out right);
- int.TryParse(BottomText.Text, out bottom);
- if (Document != null && Document.PageCount > CropPageUI && CropPageUI >= 0)
- {
- CPDFPage cropPage = Document.PageAtIndex(CropPageUI);
- Rect mediaRect = cropPage.GetBoundsForBox(CPDFDisplayBox.MediaBox);
- left = (int)(left + mediaRect.Left);
- top = (int)(top + mediaRect.Top);
- right = (int)(mediaRect.Right - right);
- bottom = (int)(mediaRect.Bottom - bottom);
- if (right > left && bottom > top)
- {
- Rect cropData = new Rect(left, top, right - left, bottom - top);
- if (mediaRect.Contains(cropData))
- {
- CropRect = cropData;
- CropUI.UpdateCropRect(cropData);
- }
- }
- }
- }
- }
- }
|