123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473 |
- 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 Compdfkit_Tools.PDFControl;
- using ComPDFKitViewer;
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Media;
- namespace Compdfkit_Tools.Edit
- {
- public partial class PDFTextEditControl : UserControl
- {
- public CPDFViewerTool ToolView { get; private set; }
- public TextEditParam EditEvent { get; set; }
- public PDFTextEditControl()
- {
- InitializeComponent();
- Loaded += PDFTextEditControl_Loaded;
- }
- public void InitWithPDFViewer(CPDFViewerTool newPDFView)
- {
- ToolView = newPDFView;
- }
- public void SetPDFTextEditData(TextEditParam newEvent)
- {
- if (newEvent.EditIndex<0)
- {
- EditEvent = null;
- }
- else
- {
- EditEvent = newEvent;
- }
- if (newEvent != null && newEvent.EditType == CPDFEditType.EditText)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- List<string> sysfontList = new List<string>();
- if (textArea != null)
- {
- sysfontList = textArea.GetFontList();
- }
- if (sysfontList.Count == 0)
- {
- sysfontList.Add("Helvetica");
- sysfontList.Add("Courier New");
- sysfontList.Add("Times New Roman");
- }
- if (sysfontList.Contains(newEvent.FontName) == false && string.IsNullOrEmpty(newEvent.FontName) == false)
- {
- sysfontList.Add(newEvent.FontName);
- }
- TextStyleUI.SetFontNames(sysfontList);
- TextStyleUI.SelectFontName(newEvent.FontName);
- TextStyleUI.SetFontStyle(newEvent.IsBold, newEvent.IsItalic);
- TextStyleUI.SetFontSize(newEvent.FontSize);
- OpacityTextBox.Text = string.Format("{0}%", (int)(Math.Ceiling(newEvent.Transparency * 100 / 255D)));
- FontOpacitySlider.Value = ((int)(Math.Ceiling(newEvent.Transparency * 100 / 255D))) / 100D;
- TextAlignUI.SetFontAlign(newEvent.TextAlign);
- if (newEvent.FontColor != null && newEvent.FontColor.Length == 3)
- {
- FontColorUI.SetCheckedForColor(Color.FromRgb(
- newEvent.FontColor[0],
- newEvent.FontColor[1],
- newEvent.FontColor[2]));
- }
- }
- EditEvent = newEvent;
- }
- private void Slider_DragCompleted(object sender, DragCompletedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "true";
- }
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontSize((float)slider.Value, false))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.FontSize = slider.Value;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void SliderOpacity_DragCompleted(object sender, DragCompletedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "true";
- }
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- 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 CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontTransparency((byte)(FontOpacitySlider.Value * 255)))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.Transparency = (byte)(FontOpacitySlider.Value * 255);
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void Slider_DragStarted(object sender, DragStartedEventArgs e)
- {
- Slider slider = sender as Slider;
- if (slider != null)
- {
- slider.Tag = "false";
- }
- }
- private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
- {
- Slider slider = sender as Slider;
- if (slider != null && slider.Tag != null && slider.Tag.ToString() == "false")
- {
- return;
- }
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontSize((float)slider.Value, false))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.FontSize = slider.Value;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- 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;
- }
- private void TextStyleUI_TextSizeChanged(object sender, double e)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontSize((float)e, false))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.FontSize = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void FontColorUI_ColorChanged(object sender, EventArgs e)
- {
- SolidColorBrush newBrush = FontColorUI.Brush as SolidColorBrush;
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null && newBrush != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontColor(newBrush.Color.R, newBrush.Color.G, newBrush.Color.B))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null && newBrush != null)
- {
- byte[] Color = new byte[3];
- Color[0] = newBrush.Color.R;
- Color[1] = newBrush.Color.G;
- Color[2] = newBrush.Color.B;
- EditEvent.FontColor = Color;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void TextAlignUI_TextAlignChanged(object sender, TextAlignType e)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- bool result = false;
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- 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;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.TextAlign = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void TextStyleUI_TextItalicChanged(object sender, bool e)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontItalic(e))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.IsItalic = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void TextStyleUI_TextBoldChanged(object sender, bool e)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontBold(e))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.IsBold = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- private void TextStyleUI_TextFontChanged(object sender, string e)
- {
- GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage);
- if (textArea != null)
- {
- Rect oldRect = DataConversionForWPF.CRectConversionForRect(textArea.GetFrame());
- if (textArea.SetCharsFontName(e))
- {
- PDFEditHistory editHistory = new PDFEditHistory();
- editHistory.EditPage = editPage;
- if (pdfPage != null)
- {
- editHistory.PageIndex = pdfPage.PageIndex;
- }
- ToolView.GetCPDFViewer()?.UndoManager.AddHistory(editHistory);
- ToolView.UpDateRender(oldRect, textArea);
- editPage.EndEdit();
- }
- }
- if (EditEvent != null && textArea == null)
- {
- EditEvent.FontName = e;
- DefaultSettingParam defaultSettingParam = ToolView.GetDefaultSettingParam();
- defaultSettingParam.SetPDFEditParamm(EditEvent);
- }
- }
- 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;
- }
- }
- }
- private void GetTextArea(out CPDFEditTextArea textArea, out CPDFPage pdfPage, out CPDFEditPage editPage)
- {
- textArea = null;
- editPage = null;
- pdfPage = null;
- if (ToolView == null)
- {
- return;
- }
- if (EditEvent != null)
- {
- try
- {
- 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)
- {
- textArea = 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();
- }
- }
- }
- }
|