123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.PDFPage.Edit;
- using ComPDFKit.Tool;
- using ComPDFKit.Tool.UndoManger;
- using ComPDFKit.Viewer.Helper;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Input;
- using ComPDFKit.Tool.DrawTool;
- using ComPDFKitViewer.Helper;
- using System.Linq;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using ComPDFKit.Tool.SettingParam;
- using System.Windows.Media;
- namespace ComPDFKit.Controls.Edit
- {
- public partial class PDFPathEditControl : UserControl, INotifyPropertyChanged
- {
- #region property
- public CPDFViewerTool ToolView { get; set; }
- public List<PathEditParam> EditEvents { get; set; } = new List<PathEditParam>();
- private Visibility _onlySingleVisible = Visibility.Collapsed;
- public Visibility OnlySingleVisible
- {
- get => _onlySingleVisible;
- set => UpdateProper(ref _onlySingleVisible, value);
- }
- #endregion
- public PDFPathEditControl()
- {
- DataContext = this;
- InitializeComponent();
- Loaded += PDPathEditControl_Loaded;
- Unloaded += PDFPathEditControl_Unloaded;
- }
- #region Load unload custom control
- private void PDPathEditControl_Loaded(object sender, RoutedEventArgs e)
- {
- RotateUI.RotationChanged -= RotateUI_RotationChanged;
- FlipUI.FlipChanged -= FlipUI_FlipChanged;
- RotateUI.RotationChanged += RotateUI_RotationChanged;
- FlipUI.FlipChanged += FlipUI_FlipChanged;
- ToolView.SelectedDataChanged -= ToolView_SelectedDataChanged;
- ToolView.SelectedDataChanged += ToolView_SelectedDataChanged;
- StrokeColorUI.ColorChanged -= StrokeColorUI_ColorChanged;
- StrokeColorUI.ColorChanged += StrokeColorUI_ColorChanged;
- FillColorUI.ColorChanged -= FillColorUI_ColorChanged;
- FillColorUI.ColorChanged += FillColorUI_ColorChanged;
- }
- private void StrokeColorUI_ColorChanged(object sender, EventArgs e)
- {
- SolidColorBrush newBrush = StrokeColorUI.Brush as SolidColorBrush;
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- bool result = pathAreas[0].SetStrokeColor(new byte[] { newBrush.Color.R, newBrush.Color.G, newBrush.Color.B});
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- if (pathArea.SetStrokeColor(new byte[] { newBrush.Color.R, newBrush.Color.G, newBrush.Color.B }))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- }
- editPage.EndEdit();
- if (EditEvents.Count > 0 && newBrush != null)
- {
- byte[] Color = new byte[3];
- Color[0] = newBrush.Color.R;
- Color[1] = newBrush.Color.G;
- Color[2] = newBrush.Color.B;
- EditEvents.FirstOrDefault().StrokeColor = Color;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- }
- private void FillColorUI_ColorChanged(object sender, EventArgs e)
- {
- SolidColorBrush newBrush = FillColorUI.Brush as SolidColorBrush;
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- bool result = pathAreas[0].SetFillColor(new byte[] { newBrush.Color.R, newBrush.Color.G, newBrush.Color.B });
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- if (pathArea.SetFillColor(new byte[] { newBrush.Color.R, newBrush.Color.G, newBrush.Color.B }))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- }
- editPage.EndEdit();
- if (EditEvents.Count > 0 && newBrush != null)
- {
- byte[] Color = new byte[3];
- Color[0] = newBrush.Color.R;
- Color[1] = newBrush.Color.G;
- Color[2] = newBrush.Color.B;
- EditEvents.FirstOrDefault().FillColor = Color;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- }
- private void PDFPathEditControl_Unloaded(object sender, RoutedEventArgs e)
- {
- RotateUI.RotationChanged -= RotateUI_RotationChanged;
- FlipUI.FlipChanged -= FlipUI_FlipChanged;
- }
- #endregion
- #region Property changed
- private void PathCut()
- {
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- SelectedRect selectedRect = ToolView.GetSelectedRectForEditAreaObject(pathAreas[0]);
- if (selectedRect == null)
- return;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- double currentZoom = ToolView.GetCPDFViewer().CurrentRenderFrame.ZoomFactor;
- Rect rect = selectedRect.GetRect();
- Rect maxRect = selectedRect.GetMaxRect();
- Rect pdfRect = new Rect((rect.X - maxRect.X) / currentZoom, (rect.Y - maxRect.Y) / currentZoom, rect.Width / currentZoom, rect.Height / currentZoom);
- pdfRect = DpiHelper.StandardRectToPDFRect(pdfRect);
- CRect newCRect = new CRect((float)pdfRect.Left, (float)pdfRect.Bottom, (float)pdfRect.Right, (float)pdfRect.Top);
- if (pathAreas[0].CutWithRect(newCRect))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- editPage.EndEdit();
- }
- private void ToolView_SelectedDataChanged(object sender, SelectedAnnotData e)
- {
- if (ToolView.GetIsCropMode())
- {
- PathCut();
- }
- }
- #endregion
- #region Init PDFViewer
- public void InitWithPDFViewer(CPDFViewerTool newPDFView)
- {
- ToolView = newPDFView;
- }
- #endregion
- #region public method
- //public void SetRotationText(float rotation)
- //{
- // RotationTxb.Text = rotation.ToString(CultureInfo.CurrentCulture);
- //}
- #endregion
- #region Path Edit
- private void SetPathTransparency(double transparency)
- {
- PathOpacitySlider.Value = transparency / 255D;
- OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Round(PathOpacitySlider.Value * 100)));
- }
- public void SetPDFPathEditData(List<PathEditParam> newEvents)
- {
- EditEvents = newEvents.Where(newEvent => newEvent.EditIndex >= 0 && newEvent.EditType == CPDFEditType.EditPath).ToList();
- if (EditEvents.Count == 0)
- return;
- PathEditParam defaultEvent = EditEvents.FirstOrDefault();
- SetPathTransparency(defaultEvent.Transparency);
- RotationTxb.Text = defaultEvent.Rotate.ToString();
- StrokeColorUI.SetCheckedForColor(Color.FromRgb(
- defaultEvent.StrokeColor[0],
- defaultEvent.StrokeColor[1],
- defaultEvent.StrokeColor[2]));
- FillColorUI.SetCheckedForColor(Color.FromRgb(
- defaultEvent.FillColor[0],
- defaultEvent.FillColor[1],
- defaultEvent.FillColor[2]));
- if (EditEvents.Count == 1)
- {
- OnlySingleVisible = Visibility.Visible;
- }
- else
- {
- OnlySingleVisible = Visibility.Collapsed;
- }
- }
- #endregion
- #region Property changed
- private void FlipUI_FlipChanged(object sender, bool e)
- {
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- bool result;
- if (e)
- {
- result = pathAreas[0].VerticalMirror();
- }
- else
- {
- result = pathAreas[0].HorizontalMirror();
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- bool result;
- if (e)
- {
- result = pathArea.VerticalMirror();
- }
- else
- {
- result = pathArea.HorizontalMirror();
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- editPage.EndEdit();
- }
- private void RotateUI_RotationChanged(object sender, double e)
- {
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- if (pathAreas[0].Rotate((int)e))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- if (pathArea.Rotate((int)e))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- editPage.EndEdit();
- RotationTxb.Text = pathAreas.FirstOrDefault().GetRotation().ToString();
- }
- private void Slider_DragStarted(object sender, DragStartedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "false";
- }
- }
- private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "true";
- }
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- if (pathAreas[0].SetTransparency((byte)(PathOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- if (pathArea.SetTransparency((byte)(PathOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- editPage.EndEdit();
- }
- private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- Slider slider = sender as Slider;
- if (OpacityTextBox != null)
- {
- OpacityTextBox.Text = string.Format("{0}%", (int)(PathOpacitySlider.Value * 100));
- }
- if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
- {
- return;
- }
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- if (pathAreas[0].SetTransparency((byte)(PathOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- if (pathArea.SetTransparency((byte)(PathOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- editPage.EndEdit();
- }
- private void PathClipBtn_Click(object sender, RoutedEventArgs e)
- {
- ToolView.SetCropMode(!ToolView.GetIsCropMode());
- }
- private void OpacityComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- ComboBoxItem selectItem = OpacityComboBox.SelectedItem as ComboBoxItem;
- if (selectItem != null && selectItem.Content != null)
- {
- if (double.TryParse(selectItem.Content.ToString().TrimEnd('%'), out double newOpacity))
- {
- OpacityTextBox.Text = selectItem.Content.ToString();
- PathOpacitySlider.Value = newOpacity / 100.0;
- }
- }
- }
- private void GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage)
- {
- pathAreas = new List<CPDFEditPathArea>();
- editPage = null;
- pdfPage = null;
- if (ToolView == null || EditEvents.Count == 0)
- {
- return;
- }
- try
- {
- foreach (var EditEvent in EditEvents)
- {
- CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
- CPDFDocument pdfDoc = pdfViewer.GetDocument();
- pdfPage = pdfDoc.PageAtIndex(EditEvent.PageIndex);
- editPage = pdfPage.GetEditPage();
- List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
- if (editAreas != null && editAreas.Count > EditEvent.EditIndex)
- {
- pathAreas.Add(editAreas[EditEvent.EditIndex] as CPDFEditPathArea);
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- private void SetAbsRotation(double absRotation)
- {
- GetPathArea(out List<CPDFEditPathArea> pathAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (pathAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(pathAreas[0].GetFrame());
- int rotation = (int)absRotation - pathAreas[0].GetRotation();
- if (pathAreas[0].Rotate(rotation))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, pathAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditPathArea pathArea in pathAreas)
- {
- int rotation = (int)absRotation - pathArea.GetRotation();
- if (pathArea.Rotate(rotation))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- groupHistory.Histories.Add(editHistory);
- }
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(groupHistory);
- ToolView.GetCPDFViewer()?.UpdateRenderFrame();
- }
- editPage.EndEdit();
- }
- private void RotationTxb_LostFocus(object sender, RoutedEventArgs e)
- {
- if (!double.TryParse(RotationTxb.Text, out double rotation))
- {
- return;
- }
- SetAbsRotation(rotation);
- }
- private void RotationTxb_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- RotationTxb_LostFocus(null, null);
- }
- }
- #endregion
- public event PropertyChangedEventHandler PropertyChanged;
- protected bool UpdateProper<T>(ref T properValue,
- T newValue,
- [CallerMemberName] string properName = "")
- {
- if (object.Equals(properValue, newValue))
- return false;
- properValue = newValue;
- OnPropertyChanged(properName);
- return true;
- }
- protected void OnPropertyChanged([CallerMemberName] string propertyName = "") =>
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
|