123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571 |
- using ComPDFKit.Measure;
- using ComPDFKit.PDFAnnotation;
- using Compdfkit_Tools.PDFControl;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Input;
- using ComPDFKit.Tool.SettingParam;
- namespace Compdfkit_Tools.Measure
- {
- /// <summary>
- /// MeasureSettingPanel.xaml 的交互逻辑
- /// </summary>
- public partial class MeasureSettingPanel : UserControl
- {
- public event EventHandler CancelEvent;
- public event EventHandler DoneEvent;
- public PDFViewControl PdfViewControl { get; set; }
- public bool ReturnToInfoPanel { get; set; }
- //public List<AnnotHandlerEventArgs> UpdateArgsList { get; set; } = new List<AnnotHandlerEventArgs>();
- public MeasureSettingPanel()
- {
- InitializeComponent();
- }
- private void ButtonCancel_Click(object sender, RoutedEventArgs e)
- {
- //if (UpdateArgsList != null && UpdateArgsList.Count > 0)
- //{
- // ReturnToInfoPanel = false;
- //}
- //UpdateArgsList?.Clear();
- CancelEvent?.Invoke(this, e);
- ReturnToInfoPanel = false;
- }
- public void ShowAreaAndLength(Visibility visibility)
- {
- if (visibility == Visibility.Visible)
- {
- SettingPanel.Height = 350;
- }
- else
- {
- SettingPanel.Height = 290;
- }
- AreaAndLength.Visibility = visibility;
- }
- private void ButtonDone_Click(object sender, RoutedEventArgs e)
- {
- if (string.IsNullOrEmpty(RulerBaseText.Text) || string.IsNullOrEmpty(RulerTranslateText.Text))
- {
- MessageBox.Show("Scale is not greater than zero");
- return;
- }
- if (double.TryParse(RulerBaseText.Text, out double ruleBase))
- {
- if (ruleBase <= 0)
- {
- MessageBox.Show("Scale is not greater than zero");
- return;
- }
- }
- if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
- {
- if (ruletranBase <= 0)
- {
- MessageBox.Show("Scale is not greater than zero");
- return;
- }
- }
- //if (UpdateArgsList != null && UpdateArgsList.Count == 0)
- //{
- // SaveMeasureSetting();
- //}
- //if (UpdateArgsList != null && UpdateArgsList.Count > 0)
- //{
- // List<AnnotArgsType> allowTypeList = new List<AnnotArgsType>()
- // {
- // AnnotArgsType.LineMeasure,
- // AnnotArgsType.PolyLineMeasure,
- // AnnotArgsType.PolygonMeasure
- // };
- // foreach (AnnotHandlerEventArgs args in UpdateArgsList)
- // {
- // if (allowTypeList.Contains(args.EventType))
- // {
- // CPDFAnnotation pdfAnnot = args.GetPDFAnnot();
- // switch (pdfAnnot.Type)
- // {
- // case C_ANNOTATION_TYPE.C_ANNOTATION_LINE:
- // {
- // CPDFLineAnnotation lineAnnot = (CPDFLineAnnotation)pdfAnnot;
- // if (lineAnnot.IsMersured())
- // {
- // CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
- // CPDFMeasureInfo measureInfo = lineMeasure.MeasureInfo;
- // if (PrecisionBox.SelectedValue != null)
- // {
- // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
- // if (double.TryParse(checkItem.Content.ToString(), out double precision))
- // {
- // measureInfo.Precision = GetMeasureSavePrecision(precision);
- // }
- // }
- // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
- // {
- // measureInfo.RulerBase = (float)ruleBasedata;
- // }
- // if (RulerBaseUnitCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
- // }
- // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
- // {
- // measureInfo.RulerTranslate = (float)ruletranBasedata;
- // }
- // if (RulerTranslateCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
- // }
- // lineMeasure.SetMeasureInfo(measureInfo);
- // lineMeasure.SetMeasureScale(
- // measureInfo.RulerBase,
- // measureInfo.RulerBaseUnit,
- // measureInfo.RulerTranslate,
- // measureInfo.RulerTranslateUnit);
- // lineMeasure.UpdateAnnotMeasure();
- // lineAnnot.UpdateAp();
- // args.Draw();
- // if (PdfViewControl != null && PdfViewControl.PDFView != null)
- // {
- // CPDFViewer viewer = PdfViewControl.PDFView;
- // if (viewer != null && viewer.UndoManager != null)
- // {
- // viewer.UndoManager.CanSave = true;
- // }
- // }
- // }
- // }
- // break;
- // case C_ANNOTATION_TYPE.C_ANNOTATION_POLYLINE:
- // {
- // CPDFPolylineAnnotation polylineAnnot = (CPDFPolylineAnnotation)pdfAnnot;
- // if (polylineAnnot.IsMersured())
- // {
- // CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
- // CPDFMeasureInfo measureInfo = polylineMeasure.MeasureInfo;
- // if (PrecisionBox.SelectedValue != null)
- // {
- // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
- // if (double.TryParse(checkItem.Content.ToString(), out double precision))
- // {
- // measureInfo.Precision = GetMeasureSavePrecision(precision);
- // }
- // }
- // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
- // {
- // measureInfo.RulerBase = (float)ruleBasedata;
- // }
- // if (RulerBaseUnitCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
- // }
- // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
- // {
- // measureInfo.RulerTranslate = (float)ruletranBasedata;
- // }
- // if (RulerTranslateCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
- // }
- // polylineMeasure.SetMeasureInfo(measureInfo);
- // polylineMeasure.SetMeasureScale(
- // measureInfo.RulerBase,
- // measureInfo.RulerBaseUnit,
- // measureInfo.RulerTranslate,
- // measureInfo.RulerTranslateUnit);
- // polylineMeasure.UpdateAnnotMeasure();
- // polylineAnnot.UpdateAp();
- // args.Draw();
- // if (PdfViewControl != null && PdfViewControl.PDFView != null)
- // {
- // CPDFViewer viewer = PdfViewControl.PDFView;
- // if (viewer != null && viewer.UndoManager != null)
- // {
- // viewer.UndoManager.CanSave = true;
- // }
- // }
- // }
- // }
- // break;
- // case C_ANNOTATION_TYPE.C_ANNOTATION_POLYGON:
- // {
- // CPDFPolygonAnnotation areaAnnot = (CPDFPolygonAnnotation)pdfAnnot;
- // if (areaAnnot.IsMersured())
- // {
- // CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
- // CPDFMeasureInfo measureInfo = areaMeasure.MeasureInfo;
- // if (PrecisionBox.SelectedValue != null)
- // {
- // ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
- // if (double.TryParse(checkItem.Content.ToString(), out double precision))
- // {
- // measureInfo.Precision = GetMeasureSavePrecision(precision);
- // }
- // }
- // if (double.TryParse(RulerBaseText.Text, out double ruleBasedata))
- // {
- // measureInfo.RulerBase = (float)ruleBasedata;
- // }
- // if (RulerBaseUnitCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerBaseUnitcheckItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerBaseUnit = RulerBaseUnitcheckItem.Content.ToString();
- // }
- // if (double.TryParse(RulerTranslateText.Text, out double ruletranBasedata))
- // {
- // measureInfo.RulerTranslate = (float)ruletranBasedata;
- // }
- // if (RulerTranslateCombo.SelectedItem != null)
- // {
- // ComboBoxItem RulerTranslatecheckItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
- // measureInfo.RulerTranslateUnit = RulerTranslatecheckItem.Content.ToString();
- // }
- // measureInfo.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
- // if ((bool)AreaCheckBox.IsChecked)
- // {
- // measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
- // }
- // if ((bool)LengthCheckBox.IsChecked)
- // {
- // measureInfo.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
- // }
- // areaMeasure.SetMeasureInfo(measureInfo);
- // areaMeasure.SetMeasureScale(
- // measureInfo.RulerBase,
- // measureInfo.RulerBaseUnit,
- // measureInfo.RulerTranslate,
- // measureInfo.RulerTranslateUnit);
- // areaMeasure.UpdateAnnotMeasure();
- // areaAnnot.UpdateAp();
- // args.Draw();
- // if(PdfViewControl!=null && PdfViewControl.PDFView!=null)
- // {
- // CPDFViewer viewer = PdfViewControl.PDFView;
- // if(viewer!=null && viewer.UndoManager!=null)
- // {
- // viewer.UndoManager.CanSave = true;
- // }
- // }
- // }
- // }
- // break;
- // default:
- // break;
- // }
- // }
- // }
- // ReturnToInfoPanel = false;
- //}
- //UpdateArgsList?.Clear();
- DoneEvent?.Invoke(this, e);
- ReturnToInfoPanel = false;
- }
- private int GetMeasureSavePrecision(double Precision)
- {
- if (Precision == 1)
- {
- return CPDFMeasure.PRECISION_VALUE_ZERO;
- }
- if (Precision == 0.1)
- {
- return CPDFMeasure.PRECISION_VALUE_ONE;
- }
- if (Precision == 0.01)
- {
- return CPDFMeasure.PRECISION_VALUE_TWO;
- }
- if (Precision == 0.001)
- {
- return CPDFMeasure.PRECISION_VALUE_THREE;
- }
- if (Precision == 0.0001)
- {
- return CPDFMeasure.PRECISION_VALUE_FOUR;
- }
- return 0;
- }
- private void TextBox_PreviewKeyDown(object sender, KeyEventArgs e)
- {
- List<Key> allowKeys = new List<Key>()
- {
- Key.Delete, Key.Back, Key.Enter, Key.NumPad0, Key.NumPad1, Key.NumPad2, Key.NumPad3,
- Key.NumPad4, Key.NumPad5, Key.NumPad6, Key.NumPad7, Key.NumPad8, Key.NumPad9, Key.D0,
- Key.D1, Key.D2, Key.D3, Key.D4, Key.D5, Key.D6, Key.D7, Key.D8, Key.D9, Key.Left, Key.Right,
- Key.OemPeriod,Key.Decimal
- };
- if (allowKeys.Contains(e.Key) == false)
- {
- e.Handled = true;
- }
- }
- private void TextBox_CanExecute(object sender, CanExecuteRoutedEventArgs e)
- {
- try
- {
- if (e.Command == ApplicationCommands.Paste && Clipboard.ContainsText())
- {
- string checkText = Clipboard.GetText();
- if (int.TryParse(checkText, out int value))
- {
- e.CanExecute = true;
- }
- e.Handled = true;
- }
- }
- catch (Exception ex)
- {
- }
- }
- public void BindMeasureSetting(CPDFMeasureInfo measureInfo)
- {
- if(measureInfo == null)
- {
- return;
- }
- RulerBaseText.Text = measureInfo.RulerBase.ToString();
- RulerTranslateText.Text = measureInfo.RulerTranslate.ToString();
- RulerTranslateCombo.SelectedIndex = -1;
- RulerBaseUnitCombo.SelectedIndex = -1;
- PrecisionBox.SelectedIndex = -1;
- if (measureInfo.RulerBaseUnit == "in")
- {
- RulerBaseUnitCombo.SelectedIndex = 0;
- }
- if (measureInfo.RulerBaseUnit == "cm")
- {
- RulerBaseUnitCombo.SelectedIndex = 1;
- }
- if (measureInfo.RulerBaseUnit == "mm")
- {
- RulerBaseUnitCombo.SelectedIndex = 2;
- }
- for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
- {
- ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
- if (checkItem != null && checkItem.Tag.ToString() == measureInfo.RulerTranslateUnit.ToString())
- {
- RulerTranslateCombo.SelectedIndex = i;
- }
- }
- string precision = GetMeasureShowPrecision(measureInfo.Precision).ToString();
- for (int i = 0; i < PrecisionBox.Items.Count; i++)
- {
- ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
- if (checkItem != null && checkItem.Content.ToString() == precision)
- {
- PrecisionBox.SelectedIndex = i;
- }
- }
- }
- //public void BindMeasureSetting(MeasureEventArgs measureEventArgs)
- //{
- // if (measureEventArgs != null)
- // {
- // RulerBaseText.Text = measureEventArgs.RulerBase.ToString();
- // RulerTranslateText.Text = measureEventArgs.RulerTranslate.ToString();
- // RulerTranslateCombo.SelectedIndex = -1;
- // RulerBaseUnitCombo.SelectedIndex = -1;
- // PrecisionBox.SelectedIndex = -1;
- // if (measureEventArgs.RulerBaseUnit == "in")
- // {
- // RulerBaseUnitCombo.SelectedIndex = 0;
- // }
- // if (measureEventArgs.RulerBaseUnit == "cm")
- // {
- // RulerBaseUnitCombo.SelectedIndex = 1;
- // }
- // if (measureEventArgs.RulerBaseUnit == "mm")
- // {
- // RulerBaseUnitCombo.SelectedIndex = 2;
- // }
- // for (int i = 0; i < RulerTranslateCombo.Items.Count; i++)
- // {
- // ComboBoxItem checkItem = RulerTranslateCombo.Items[i] as ComboBoxItem;
- // if (checkItem != null && checkItem.Content.ToString() == measureEventArgs.RulerTranslateUnit.ToString())
- // {
- // RulerTranslateCombo.SelectedIndex = i;
- // }
- // }
- // for (int i = 0; i < PrecisionBox.Items.Count; i++)
- // {
- // ComboBoxItem checkItem = PrecisionBox.Items[i] as ComboBoxItem;
- // if (checkItem != null && checkItem.Content.ToString() == ((decimal)measureEventArgs.Precision).ToString())
- // {
- // PrecisionBox.SelectedIndex = i;
- // }
- // }
- // }
- //}
- public void SaveMeasureSetting(CPDFAnnotation annot)
- {
- MeasureSetting measureSetting = new MeasureSetting();
- if (double.TryParse(RulerBaseText.Text, out double ruleBase))
- {
- measureSetting.RulerBase = ruleBase;
- }
- if (RulerBaseUnitCombo.SelectedItem != null)
- {
- ComboBoxItem checkItem = RulerBaseUnitCombo.SelectedItem as ComboBoxItem;
- measureSetting.RulerBaseUnit = checkItem.Tag.ToString();
- }
- if (double.TryParse(RulerTranslateText.Text, out double ruletranBase))
- {
- measureSetting.RulerTranslate = ruletranBase;
- }
- if (RulerTranslateCombo.SelectedItem != null)
- {
- ComboBoxItem checkItem = RulerTranslateCombo.SelectedItem as ComboBoxItem;
- measureSetting.RulerTranslateUnit = checkItem.Tag.ToString();
- }
- if (PrecisionBox.SelectedValue != null)
- {
- ComboBoxItem checkItem = PrecisionBox.SelectedValue as ComboBoxItem;
- if (double.TryParse(checkItem.Content.ToString(), out double precision))
- {
- measureSetting.Precision = precision;
- }
- }
- measureSetting.IsShowArea = (bool)AreaCheckBox?.IsChecked;
- measureSetting.IsShowLength = (bool)LengthCheckBox?.IsChecked;
- UpdateAnnotWithSetting(annot, measureSetting);
- }
- private void UpdateAnnotWithSetting(CPDFAnnotation annot, MeasureSetting measureSetting)
- {
- if(annot is CPDFLineAnnotation lineAnnot)
- {
- if (lineAnnot.IsMersured())
- {
- CPDFDistanceMeasure lineMeasure = lineAnnot.GetDistanceMeasure();
- CPDFMeasureInfo info = lineMeasure.MeasureInfo;
-
- info.RulerBaseUnit = measureSetting.RulerBaseUnit;
- info.RulerBase = (float)measureSetting.RulerBase;
- info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- info.RulerTranslate = (float)measureSetting.RulerTranslate;
-
- info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
- lineAnnot.GetDistanceMeasure().SetMeasureInfo(info);
- lineAnnot.GetDistanceMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
-
- lineAnnot.GetDistanceMeasure().UpdateAnnotMeasure();
- lineAnnot.UpdateAp();
- PdfViewControl.UpdateAnnotFrame();
- }
- }
- else if(annot is CPDFPolylineAnnotation polylineAnnot)
- {
- if (polylineAnnot.IsMersured())
- {
- CPDFPerimeterMeasure polylineMeasure = polylineAnnot.GetPerimeterMeasure();
- CPDFMeasureInfo info = polylineMeasure.MeasureInfo;
- info.RulerBaseUnit = measureSetting.RulerBaseUnit;
- info.RulerBase = (float)measureSetting.RulerBase;
- info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- info.RulerTranslate = (float)measureSetting.RulerTranslate;
- info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
- polylineAnnot.GetPerimeterMeasure().SetMeasureInfo(info);
- polylineAnnot.GetPerimeterMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
- polylineAnnot.GetPerimeterMeasure().UpdateAnnotMeasure();
- polylineAnnot.UpdateAp();
- PdfViewControl.UpdateAnnotFrame();
- }
- }
- else if(annot is CPDFPolygonAnnotation areaAnnot)
- {
- if (areaAnnot.IsMersured())
- {
- CPDFAreaMeasure areaMeasure = areaAnnot.GetAreaMeasure();
- CPDFMeasureInfo info = areaMeasure.MeasureInfo;
- info.RulerBaseUnit = measureSetting.RulerBaseUnit;
- info.RulerBase = (float)measureSetting.RulerBase;
- info.RulerTranslateUnit = measureSetting.RulerTranslateUnit;
- info.RulerTranslate = (float)measureSetting.RulerTranslate;
- info.Precision = GetMeasureSavePrecision(measureSetting.Precision);
- info.CaptionType = CPDFCaptionType.CPDF_CAPTION_NONE;
- if (measureSetting.IsShowArea)
- {
- info.CaptionType |= CPDFCaptionType.CPDF_CAPTION_AREA;
- }
- if (measureSetting.IsShowLength)
- {
- info.CaptionType |= CPDFCaptionType.CPDF_CAPTION_LENGTH;
- }
- areaAnnot.GetAreaMeasure().SetMeasureInfo(info);
- areaAnnot.GetAreaMeasure().SetMeasureScale(info.RulerBase, info.RulerBaseUnit, info.RulerTranslate, info.RulerTranslateUnit);
- areaAnnot.GetAreaMeasure().UpdateAnnotMeasure();
- areaAnnot.UpdateAp();
- PdfViewControl.UpdateAnnotFrame();
- }
- }
- }
-
- private double GetMeasureShowPrecision(int precision)
- {
- if (precision == CPDFMeasure.PRECISION_VALUE_ZERO)
- {
- return 1;
- }
- if (CPDFMeasure.PRECISION_VALUE_ONE == precision)
- {
- return 0.1;
- }
- if (CPDFMeasure.PRECISION_VALUE_TWO == precision)
- {
- return 0.01;
- }
- if (CPDFMeasure.PRECISION_VALUE_THREE == precision)
- {
- return 0.001;
- }
- if (CPDFMeasure.PRECISION_VALUE_FOUR == precision)
- {
- return 0.0001;
- }
- return 0;
- }
- public void ChangedCheckBoxIsChecked(bool Area, bool Lenght)
- {
- AreaCheckBox.IsChecked = Area;
- LengthCheckBox.IsChecked = Lenght;
- }
- }
- }
|