Browse Source

ComPDF(win) - 创建签名 - 字体自定义

liuaoran 11 months ago
parent
commit
cc7613b1e8

+ 5 - 7
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationUI/CPDFCreateSignatureDialog.xaml

@@ -377,19 +377,17 @@
                         FontSize="45"/>
                     <StackPanel Grid.Row="3" Orientation="Horizontal" Grid.ColumnSpan="2">
                         <TextBlock  Text="{Binding Converter={StaticResource PropertyPanelResourceConverter},ConverterParameter=Property_Font}" VerticalAlignment="Center"/>
-                        <ComboBox Width="232"
+                        <ComboBox Width="200" Name="FontNameCmb"
+                                  Height="25"
                         Style="{StaticResource ComboBoxStyle1}"
                         Margin="8,0,0,0"
-                        SelectionChanged="TextName_SelectionChanged"
-                        x:Name="TextName"
+                         SelectionChanged="FontNameCmb_SelectionChanged"
                         SelectedIndex="0"
-                        Loaded="TextName_Loaded"
                         HorizontalAlignment="Left"
                         VerticalAlignment="Center">
-                            <ComboBoxItem Content="Arial"/>
-                            <ComboBoxItem Content="Courier New" IsSelected="True"/>
-                            <ComboBoxItem Content="Times New Roman"/>
                         </ComboBox>
+                        <TextBlock  Text="样式" VerticalAlignment="Center" Margin="10,0,8,0"/>
+                        <ComboBox x:Name="StyleNameCmb" Style="{StaticResource ComboBoxStyle1}" Height="25" Width="80" Margin="10,0,0,0" SelectionChanged="StyleNameCmb_SelectionChanged"></ComboBox>
                     </StackPanel>
                     <StackPanel
                         Grid.Row="3"

+ 26 - 12
Demo/Examples/Compdfkit_Tools/Annotation/PDFAnnotationPanel/PDFAnnotationUI/CPDFCreateSignatureDialog.xaml.cs

@@ -15,6 +15,7 @@ using System.Globalization;
 using System.Windows.Ink;
 using Compdfkit_Tools.Common;
 using ComPDFKitViewer.Helper;
+using ComPDFKit.PDFDocument;
 
 namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
 {
@@ -28,6 +29,9 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
         private double StrokeWidth = 3;
         private double StrokeHigh = 3;
         private bool IsPageLoaded = false;
+
+        private string postScriptName;
+
         public CPDFCreateSignatureDialog()
         {
             InitializeComponent();
@@ -44,6 +48,9 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
 
             DrawinkCanvas.DefaultDrawingAttributes.Color = solidColorBrush.Color;
             InPutTextBox.Foreground = solidColorBrush;
+
+            FontNameCmb.Items.Clear();
+            FontNameCmb.ItemsSource = CPDFFont.GetFontNameDictionary().Keys;
         }
 
         private void Cancel_Click(object sender, RoutedEventArgs e)
@@ -84,7 +91,7 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             DrawinkCanvas.DefaultDrawingAttributes.Color = solidColorBrush.Color;
             InPutTextBox.Foreground = solidColorBrush;
 
-            if (CreateSignatureControl.SelectedIndex==1)
+            if (CreateSignatureControl.SelectedIndex == 1)
             {
                 InPutTextBox.Focus();
             }
@@ -174,6 +181,7 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
 
             }
         }
+
         private void SaveTextSignature()
         {
             if (string.IsNullOrEmpty(InPutTextBox.Text))
@@ -182,7 +190,7 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             }
             System.Windows.Media.Brush fontcolor = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#252629"));
             fontcolor = TextColorPickerControl.Brush;
-            Bitmap bmp = TextToBitmap(InPutTextBox.Text, TextName.SelectionBoxItem.ToString(), 50, System.Drawing.Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
+            Bitmap bmp = TextToBitmap(InPutTextBox.Text, postScriptName, 50, System.Drawing.Rectangle.Empty, fontcolor, System.Drawing.Color.Transparent);
             string guid = Guid.NewGuid().ToString();
             string path = System.IO.Path.Combine(SignaturePath, guid);
             bmp.Save(path, ImageFormat.Png);
@@ -230,7 +238,7 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             double inkThickness;
             inkThickness = StrokeWidth;
             WriteableBitmap writeStamp = RawPointListToBitmap(RawPointList, inkThickness, DrawinkCanvas.DefaultDrawingAttributes.Color);
-            if (writeStamp==null)
+            if (writeStamp == null)
             {
                 return;
             }
@@ -343,6 +351,7 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             }
             return null;
         }
+
         private Bitmap TextToBitmap(string text, string FontFamily, double size, System.Drawing.Rectangle rect, System.Windows.Media.Brush fontcolor, System.Drawing.Color backColor)
         {
             FormattedText formatText = new FormattedText(
@@ -377,13 +386,6 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             return bitmap;
         }
 
-        private void TextName_SelectionChanged(object sender, SelectionChangedEventArgs e)
-        {
-            if (TextName.SelectedIndex != -1 && IsPageLoaded)
-            {
-                InPutTextBox.FontFamily = new System.Windows.Media.FontFamily((TextName.SelectedItem as ComboBoxItem)?.Content.ToString());
-            }
-        }
 
         private void Grid_Drop(object sender, DragEventArgs e)
         {
@@ -543,9 +545,21 @@ namespace Compdfkit_Tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI
             }
         }
 
-        private void TextName_Loaded(object sender, RoutedEventArgs e)
+        private void FontNameCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
+        {
+            StyleNameCmb.ItemsSource = CPDFFont.GetFontNameDictionary()[FontNameCmb.SelectedValue.ToString()];  
+            StyleNameCmb.SelectedIndex = 0;
+            CPDFFont.GetPostScriptName(FontNameCmb.SelectedValue.ToString(), StyleNameCmb.SelectedValue.ToString(),ref postScriptName);
+            InPutTextBox.FontFamily = new System.Windows.Media.FontFamily(postScriptName);
+        }
+
+        private void StyleNameCmb_SelectionChanged(object sender, SelectionChangedEventArgs e)
         {
-            InPutTextBox.FontFamily = new System.Windows.Media.FontFamily((TextName.SelectedItem as ComboBoxItem)?.Content.ToString());
+            if (!string.IsNullOrEmpty(StyleNameCmb.SelectedValue?.ToString()))
+            {
+                CPDFFont.GetPostScriptName(FontNameCmb.SelectedValue.ToString(), StyleNameCmb.SelectedValue.ToString(), ref postScriptName);
+                InPutTextBox.FontFamily = new System.Windows.Media.FontFamily(postScriptName);
+            } 
         }
     }
 }

+ 10 - 0
Demo/Examples/PDFViewer/MainWindow.xaml.cs

@@ -24,6 +24,8 @@ using ComPDFKit.PDFDocument;
 using Compdfkit_Tools.Data;
 using Compdfkit_Tools.Common;
 using System.Reflection;
+using ComPDFKit.NativeMethod;
+using System.Threading;
 
 namespace PDFViewer
 {
@@ -64,6 +66,14 @@ namespace PDFViewer
             TabControl.SelectionChanged += TabControl_SelectionChanged;
             FirstLoadFile();
             CPDFAnnotationData.Author = Properties.Settings.Default.DocumentAuthor;
+
+            Thread thread = new Thread(DoWork); 
+            thread.Start(); 
+        }
+
+        static void DoWork()
+        {
+            CPDFFont.InitFont();
         }
 
         private void FirstLoadFile()