Browse Source

compdfkit(win) - FreeText属性面板和undo redo

weixiangjie 11 months ago
parent
commit
8a83cc41ad

+ 40 - 10
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationUI/CPDFFreeTextUI.xaml.cs

@@ -76,6 +76,11 @@ namespace Compdfkit_Tools.PDFControlUI
             {
                 if (textAnnot != null && textAnnot.IsValid())
                 {
+                    FreeTextAnnotHistory history = new FreeTextAnnotHistory();
+                    history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
+                    history.Action = HistoryAction.Update;
+                    history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                    
                    switch(CPDFFontControl.TextAlignment)
                     {
                         case TextAlignment.Left:
@@ -95,6 +100,9 @@ namespace Compdfkit_Tools.PDFControlUI
                     {
                         viewControl.UpdateAnnotFrame();
                     }
+                    
+                    history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                    viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
                 }
             }
             CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
@@ -110,15 +118,24 @@ namespace Compdfkit_Tools.PDFControlUI
             {
                 if (textAnnot != null && textAnnot.IsValid())
                 {
+                    FreeTextAnnotHistory history = new FreeTextAnnotHistory();
+                    history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
+                    history.Action = HistoryAction.Update;
+                    history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                    
                     CTextAttribute textAttr = textAnnot.FreeTextDa;
-                    string postScriptName = string.Empty; 
-                    textAttr.FontName = CPDFFontControl.PostScriptName;
+                    string psName=String.Empty;
+                    CPDFFont.GetPostScriptName(CPDFFontControl.FontFamilyValue, CPDFFontControl.FontStyleValue, ref psName);
+                    textAttr.FontName = psName;
                     textAnnot.SetFreetextDa(textAttr);
                     textAnnot.UpdateAp();
                     if (viewControl != null && viewControl.PDFViewTool != null)
                     {
                         viewControl.UpdateAnnotFrame();
                     }
+                    
+                    history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                    viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
                 }
             }
             CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
@@ -132,17 +149,26 @@ namespace Compdfkit_Tools.PDFControlUI
             }
             else
             {
+                FreeTextAnnotHistory history = new FreeTextAnnotHistory();
+                history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
+                history.Action = HistoryAction.Update;
+                history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                
                 CTextAttribute textAttr = textAnnot.FreeTextDa;
                 bool isBold = IsBold(textAttr.FontName);
                 bool isItalic = IsItalic(textAttr.FontName);
-                FontType fontType = GetFontType(CPDFFontControl.FontFamilyValue);
-                textAttr.FontName = ObtainFontName(fontType, isBold, isItalic);
+                string psName=String.Empty;
+                CPDFFont.GetPostScriptName(CPDFFontControl.FontFamilyValue, CPDFFontControl.FontStyleValue, ref psName);
+                textAttr.FontName = psName;
                 textAnnot.SetFreetextDa(textAttr);
                 textAnnot.UpdateAp();
                 if (viewControl != null && viewControl.PDFViewTool != null)
                 {
                     viewControl.UpdateAnnotFrame() ;
                 }
+                
+                history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, textAnnot.Page.PageIndex, textAnnot);
+                viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
             }
             CPDFAnnotationPreviewerControl.DrawFreeTextPreview(GetFreeTextData());
         }
@@ -181,14 +207,18 @@ namespace Compdfkit_Tools.PDFControlUI
             else
             {
                 Color fontColor = ((SolidColorBrush)ColorPickerControl.Brush).Color;
-                CTextAttribute textAttr = textAnnot.FreeTextDa;
-                textAttr.FontColor = new byte[3]
+                CTextAttribute textAttr = new CTextAttribute
                 {
-                    fontColor.R,
-                    fontColor.G, 
-                    fontColor.B,
+                    FontName = textAnnot.FreeTextDa.FontName,
+                    FontSize = textAnnot.FreeTextDa.FontSize,
+                    FontColor = new byte[3]
+                    {
+                        fontColor.R,
+                        fontColor.G, 
+                        fontColor.B,
+                    }
                 };
-                
+
                 if (viewControl != null && !textAnnot.FreeTextDa.FontColor.SequenceEqual(textAttr.FontColor))
                 {
                     FreeTextAnnotHistory history = new FreeTextAnnotHistory();

+ 0 - 1
Demo/Examples/Compdfkit_Tools/Common/PropertyControl/PDFFont/CPDFFontControl.xaml.cs

@@ -42,7 +42,6 @@ namespace Compdfkit_Tools.Common
             get => CPDFFontUI.FamilyName;
             set
             {
-                CPDFFontUI.lockFamilyName = true;
                 CPDFFontUI.FamilyName = value;
             }
         }

+ 19 - 5
Demo/Examples/Compdfkit_Tools/Common/PropertyControl/PDFFont/CPDFFontUI.xaml.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using System.ComponentModel;
+using System.Linq;
 using System.Runtime.CompilerServices;
 using System.Windows;
 using System.Windows.Controls;
@@ -11,8 +12,8 @@ namespace Compdfkit_Tools.Common
 {
     public partial class CPDFFontUI : UserControl, INotifyPropertyChanged
     {
-        internal bool lockFamilyName = false;
         internal bool lockStyleName = false;
+        internal bool isFirstLoad = true;
 
         private string regular = LanguageHelper.PropertyPanelManager.GetString("Font_Regular");
         private string bold = LanguageHelper.PropertyPanelManager.GetString("Font_Bold");
@@ -32,7 +33,7 @@ namespace Compdfkit_Tools.Common
             set
             { 
                 _familyName = value;
-                if (lockFamilyName && FontFamilyComboBox.Items.Contains(_familyName))
+                if (FontFamilyComboBox.Items.Contains(_familyName))
                 {
                     FontFamilyComboBox.SelectedItem = _familyName;
                 }
@@ -153,18 +154,31 @@ namespace Compdfkit_Tools.Common
 
         private void FontFamilyComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
+            if (isFirstLoad)
+            {
+                var fontNames = CPDFFont.GetFontNameDictionary().Keys.ToList();
+                var defaultIndex = fontNames.FindIndex(x => x == "Helvetica");
+                if (defaultIndex == -1)
+                {
+                    defaultIndex = fontNames.FindIndex(x => x == "Arial");
+                }
+                FontFamilyComboBox.SelectedIndex = defaultIndex;
+                FontStyleComboBox.SelectedIndex = 0;
+                isFirstLoad = false;
+                return;
+            }
+            
             var styleNames = CPDFFont.GetFontNameDictionary()[FontFamilyComboBox.SelectedValue.ToString()];
             FontStyleComboBox.ItemsSource = styleNames;
-            if (!lockFamilyName)
+            if (FamilyName != FontFamilyComboBox.SelectedValue.ToString())
             {
                 FamilyName = FontFamilyComboBox.SelectedValue.ToString();
                 if (styleNames.Count != 0)
                 {
                     FontStyleComboBox.SelectedIndex = 0;
                 }
-                FontStyleChanged?.Invoke(this, EventArgs.Empty);
+                FontFamilyChanged?.Invoke(this, EventArgs.Empty);
             }
-            lockFamilyName = false;
         }
 
         private void FontStyleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)