123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846 |
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using ComPDFKit.PDFPage.Edit;
- using ComPDFKit.Tool;
- using ComPDFKit.Tool.SettingParam;
- 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.Media;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Linq;
- namespace ComPDFKit.Controls.Edit
- {
- public partial class PDFTextEditControl : UserControl, INotifyPropertyChanged
- {
- #region Property
- public CPDFViewerTool ToolView { get; private set; }
- public List<TextEditParam> EditEvents { get; set; }
- public event PropertyChangedEventHandler PropertyChanged;
- private bool _isMultiSelected = true;
- public bool IsMultiSelected
- {
- get => _isMultiSelected;
- set
- {
- UpdateProper(ref _isMultiSelected, value);
- }
- }
- private bool _showBorder;
- public bool ShowBorder
- {
- get => _showBorder;
- set
- {
- UpdateProper(ref _showBorder, value);
- }
- }
- #endregion
- public PDFTextEditControl()
- {
- DataContext = this;
- InitializeComponent();
- Loaded += PDFTextEditControl_Loaded;
- }
- #region Init PDFView
- public void InitWithPDFViewer(CPDFViewerTool newPDFView)
- {
- ToolView = newPDFView;
- }
- #endregion
- #region UI
- public void SetPDFTextEditData(List<TextEditParam> newEvents)
- {
- EditEvents = newEvents.Where(newEvent => newEvent.EditIndex >= 0 && newEvent.EditType == CPDFEditType.EditText).ToList();
- TextEditParam defaultEvent = EditEvents.FirstOrDefault();
- if (EditEvents.Count > 0)
- {
- GetTextArea(out List<CPDFEditTextArea> textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- List<string> sysfontList = new List<string>();
- if (textArea != null)
- {
- sysfontList = textArea.FirstOrDefault().GetFontList();
- }
- if (sysfontList.Count == 0)
- {
- sysfontList.Add("Helvetica");
- sysfontList.Add("Courier New");
- sysfontList.Add("Times New Roman");
- }
- if (sysfontList.Contains(defaultEvent.FontName) == false && string.IsNullOrEmpty(defaultEvent.FontName) == false)
- {
- sysfontList.Add(defaultEvent.FontName);
- }
- TextStyleUI.SetFontNames(sysfontList);
- TextStyleUI.SelectFontName(defaultEvent.FontName);
- TextStyleUI.SetFontStyle(defaultEvent.IsBold, defaultEvent.IsItalic);
- TextStyleUI.SetFontSize(defaultEvent.FontSize);
- OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(defaultEvent.Transparency * 100 / 255D)));
- FontOpacitySlider.Value = ((int)(Math.Ceiling(defaultEvent.Transparency * 100 / 255D))) / 100D;
- TextAlignUI.SetFontAlign(defaultEvent.TextAlign);
- if (defaultEvent.FontColor != null && defaultEvent.FontColor.Length == 3)
- {
- FontColorUI.SetCheckedForColor(Color.FromRgb(
- defaultEvent.FontColor[0],
- defaultEvent.FontColor[1],
- defaultEvent.FontColor[2]));
- }
- }
- }
- //public void SetPDFTextMultiEditData(List<PDFEditEvent> editEvents)
- //{
- // EditEvent = null;
- // EditMultiEvents = null;
- // if(editEvents!=null && editEvents.Count>0)
- // {
- // PDFEditEvent editEvent= editEvents[0];
- // if (editEvent != null && editEvent.EditType == CPDFEditType.EditText)
- // {
- // if (editEvent.SystemFontNameList != null && editEvent.SystemFontNameList.Count == 0)
- // {
- // editEvent.SystemFontNameList.Add("Helvetica");
- // editEvent.SystemFontNameList.Add("Courier New");
- // editEvent.SystemFontNameList.Add("Times New Roman");
- // }
- // if (editEvent.SystemFontNameList.Contains(editEvent.FontName) == false && string.IsNullOrEmpty(editEvent.FontName) == false)
- // {
- // editEvent.SystemFontNameList.Add(editEvent.FontName);
- // }
- // TextStyleUI.SetFontNames(editEvent.SystemFontNameList);
- // TextStyleUI.SelectFontName(editEvent.FontName);
- // TextStyleUI.SetFontStyle(editEvent.IsBold, editEvent.IsItalic);
- // TextStyleUI.SetFontSize(editEvent.FontSize);
- // OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(editEvent.Transparency * 100 / 255D)));
- // FontOpacitySlider.Value = ((int)(Math.Ceiling(editEvent.Transparency * 100 / 255D))) / 100D;
- // TextAlignUI.SetFontAlign(editEvent.TextAlign);
- // FontColorUI.SetCheckedForColor(editEvent.FontColor);
- // }
- // }
- // EditMultiEvents=editEvents;
- //}
- private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "true";
- }
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], (byte)(FontOpacitySlider.Value * 255), isBold, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255));
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.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();
- if (EditEvents.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().Transparency = (byte)(FontOpacitySlider.Value * 255);
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- }
- private void SliderOpacity_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- Slider slider = sender as Slider;
- if (OpacityTextBox != null && FontOpacitySlider != null)
- {
- OpacityTextBox.Text = string.Format("{0}%", (int)(FontOpacitySlider.Value * 100));
- }
- if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
- {
- return;
- }
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], (byte)(FontOpacitySlider.Value * 255), isBold, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255));
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.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();
- if (EditEvents?.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().Transparency = (byte)(FontOpacitySlider.Value * 255);
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- }
- private void Slider_DragStarted(object sender, DragStartedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "false";
- }
- }
- #endregion
- #region Loaded
- private void PDFTextEditControl_Loaded(object sender, RoutedEventArgs e)
- {
- TextStyleUI.TextFontChanged -= TextStyleUI_TextFontChanged;
- TextStyleUI.TextBoldChanged -= TextStyleUI_TextBoldChanged;
- TextStyleUI.TextItalicChanged -= TextStyleUI_TextItalicChanged;
- TextStyleUI.TextSizeChanged -= TextStyleUI_TextSizeChanged;
- TextAlignUI.TextAlignChanged -= TextAlignUI_TextAlignChanged;
- FontColorUI.ColorChanged -= FontColorUI_ColorChanged;
- TextStyleUI.TextFontChanged += TextStyleUI_TextFontChanged;
- TextStyleUI.TextBoldChanged += TextStyleUI_TextBoldChanged;
- TextStyleUI.TextItalicChanged += TextStyleUI_TextItalicChanged;
- TextStyleUI.TextSizeChanged += TextStyleUI_TextSizeChanged;
- TextAlignUI.TextAlignChanged += TextAlignUI_TextAlignChanged;
- FontColorUI.ColorChanged += FontColorUI_ColorChanged;
- IsMultiSelected = ToolView.GetIsMultiSelected();
- ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
- }
- #endregion
- #region Property changed
- private void TextStyleUI_TextSizeChanged(object sender, double e)
- {
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, (float)e, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontSize((float)e, true);
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontSize((float)e, true))
- {
- 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 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().FontSize = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- //if (EditMultiEvents != null)
- //{
- // foreach (PDFEditEvent editEvent in EditMultiEvents)
- // {
- // editEvent.FontSize = e;
- // }
- // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
- //}
- }
- private void FontColorUI_ColorChanged(object sender, EventArgs e)
- {
- SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, fontSize, newBrush.Color.R, newBrush.Color.G, newBrush.Color.B, transparency, isBold, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontColor(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, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontColor(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().FontColor = Color;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- }
- private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
- {
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (textAreas[0].SelectLineRects != null && textAreas[0].SelectLineRects.Count > 0)
- {
- result = textAreas[0].SetTextRangeAlign(e);
- }
- else
- {
- result = textAreas[0].SetTextAreaAlign(e);
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- bool result;
- if (textArea.SelectLineRects != null && textArea.SelectLineRects.Count > 0)
- {
- result = textArea.SetTextRangeAlign(e);
- }
- else
- {
- result = textArea.SetTextAreaAlign(e);
- }
- 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();
- if (EditEvents.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().TextAlign = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- //if (EditMultiEvents != null)
- //{
- // foreach (PDFEditEvent editEvent in EditMultiEvents)
- // {
- // editEvent.TextAlign = e;
- // }
- // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
- //}
- }
- private void TextStyleUI_TextItalicChanged(object sender, bool e)
- {
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, e);
- }
- else
- {
- result = textAreas[0].SetCharsFontItalic(e);
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontItalic(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();
- if (EditEvents.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().IsItalic = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- //if (EditMultiEvents != null)
- //{
- // foreach (PDFEditEvent editEvent in EditMultiEvents)
- // {
- // editEvent.IsItalic = e;
- // }
- // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
- //}
- }
- private void TextStyleUI_TextBoldChanged(object sender, bool e)
- {
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(fontName, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, e, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontBold(e);
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontBold(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();
- if (EditEvents.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().IsBold = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- //if (EditMultiEvents != null)
- //{
- // foreach (PDFEditEvent editEvent in EditMultiEvents)
- // {
- // editEvent.IsBold = e;
- // }
- // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
- //}
- }
- private void TextStyleUI_TextFontChanged(object sender, string e)
- {
- GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textAreas.Count == 0 || pdfPage == null || editPage == null)
- return;
- if (ToolView.CurrentEditAreaObject() != null)
- {
- bool result;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textAreas[0].GetFrame());
- if (string.IsNullOrEmpty(textAreas[0].SelectText))
- {
- string fontName = "Helvetica";
- float fontSize = 14;
- byte[] fontColor = { 0, 0, 0 };
- byte transparency = 255;
- bool isBold = false;
- bool isItalic = false;
- textAreas[0].GetTextStyle(ref fontName, ref fontSize, ref fontColor, ref transparency, ref isBold, ref isItalic);
- result = textAreas[0].SetCurTextStyle(e, fontSize, fontColor[0], fontColor[1], fontColor[2], transparency, isBold, isItalic);
- }
- else
- {
- result = textAreas[0].SetCharsFontName(e);
- }
- if (result)
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- editHistory.PageIndex = pdfPage.PageIndex;
- ToolView.GetCPDFViewer().UndoManager.AddHistory(editHistory);
- ToolView.UpdateRender(oldRect, textAreas[0]);
- }
- }
- else
- {
- GroupHistory groupHistory = new GroupHistory();
- foreach (CPDFEditTextArea textArea in textAreas)
- {
- if (textArea.SetCharsFontName(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();
- if (EditEvents.Count > 0 && textAreas.Count > 0)
- {
- EditEvents.FirstOrDefault().FontName = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvents.FirstOrDefault());
- }
- //if (EditMultiEvents != null)
- //{
- // foreach (PDFEditEvent editEvent in EditMultiEvents)
- // {
- // editEvent.FontName = e;
- // }
- // PDFEditEvent.UpdatePDFEditList(EditMultiEvents);
- //}
- }
- 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();
- FontOpacitySlider.Value = newOpacity / 100.0;
- }
- }
- }
- #endregion
- #region Text Edit
- private void GetTextArea(out List<CPDFEditTextArea> textAreas, out CPDFPage pdfPage, out CPDFEditPage editPage)
- {
- textAreas = new List<CPDFEditTextArea>();
- editPage = null;
- pdfPage = null;
- if (ToolView == null)
- {
- return;
- }
- if (EditEvents != null && EditEvents.Count>0 )
- {
- try
- {
- CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
- CPDFDocument pdfDoc = pdfViewer.GetDocument();
- pdfPage = pdfDoc.PageAtIndex(EditEvents.FirstOrDefault().PageIndex);
- editPage = pdfPage.GetEditPage();
- List<CPDFEditArea> editAreas = editPage.GetEditAreaList();
- foreach (TextEditParam editEvent in EditEvents)
- {
- if (editAreas != null && editAreas.Count > editEvent.EditIndex)
- {
- textAreas.Add(editAreas[editEvent.EditIndex] as CPDFEditTextArea);
- }
- }
- }
- catch (Exception ex)
- {
- }
- }
- else
- {
- CPDFViewer pdfViewer = ToolView.GetCPDFViewer();
- CPDFDocument pdfDoc = pdfViewer.GetDocument();
- pdfPage = pdfDoc.PageAtIndex(0);
- editPage = pdfPage.GetEditPage();
- editPage.BeginEdit(CPDFEditType.EditText);
- editPage.EndEdit();
- }
- }
- #endregion
- private void chkMulti_Click(object sender, RoutedEventArgs e)
- {
- ToolView.SetIsMultiSelected((e.Source as CheckBox).IsChecked.GetValueOrDefault());
- }
- private void chkEditPen_Click(object sender, RoutedEventArgs e)
- {
- if ((e.Source as CheckBox).IsChecked.GetValueOrDefault())
- {
- ToolView.SetEditPen(null);
- }
- else
- {
- ToolView.SetEditPen(new Pen()
- {
- Brush = new SolidColorBrush(Colors.Black),
- Thickness = 0
- });
- }
- ShowBorder = ToolView.GetEditPen() == null || ToolView.GetEditPen().Thickness != 0;
- ToolView.GetCPDFViewer().UpdateRenderFrame();
- }
- 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));
- }
- }
|