123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 |
- using ComPDFKit.Import;
- using ComPDFKit.PDFAnnotation;
- using ComPDFKit.PDFAnnotation.Form;
- using ComPDFKit.PDFDocument;
- using ComPDFKit.PDFPage;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Reflection;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Controls.Primitives;
- using System.Windows.Ink;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Media.Media3D;
- namespace Compdfkit_Tools.PDFControl
- {
- /// <summary>
- /// CPDFSignControl.xaml 的交互逻辑
- /// </summary>
- public partial class FillDigitalSignatureControl : UserControl
- {
- private Dictionary<string, Border> TabDict { get; set; }
- private SignatureConfig tempSignatureConfig = new SignatureConfig();
- private string _filePath = string.Empty;
- public string FilePath
- {
- get => _filePath;
- set
- {
- _filePath = value;
- }
- }
- public FillDigitalSignatureControl()
- {
- InitializeComponent();
- TabDict = new Dictionary<string, Border>
- {
- ["Keyboard"] = KeyboardBorder,
- ["Trackpad"] = TrackpadBorder,
- ["Image"] = ImageBorder,
- ["None"] = NoneBorder
- };
- SetCheckedTab("Keyboard");
- CreateTempSignature();
- }
- private void CreateTempSignature()
- {
- CPDFDocument tempDocument = CPDFDocument.CreateDocument();
- tempDocument.InsertPage(0, 200, 200, null);
- CPDFPage page = tempDocument.PageAtIndex(0);
- CPDFSignatureWidget signatureWidget = page.CreateWidget(C_WIDGET_TYPE.WIDGET_SIGNATUREFIELDS) as CPDFSignatureWidget;
- signatureWidget.SetRect(new CRect(0, 100, 100, 0));
- signatureWidget.UpdataApWithSignature(tempSignatureConfig);
- signatureWidget.UpdateFormAp();
- //if(signatureWidget.UpdateApWithImage("C:\\Users\\dkan\\Pictures\\Screenshots\\hao.jpg", "", 0))
- //{
- // signatureWidget.UpdateAp();
- //}
- byte[] signatureBitmapBytes = GetTempSignatureImage(signatureWidget, out int width, out int height);
- tempDocument.WriteToFilePath("E:\\testfile1.pdf");
- signatureWidget.ReleaseAnnot();
-
- if (signatureBitmapBytes.Length > 0)
- {
- PixelFormat fmt = PixelFormats.Bgra32;
- BitmapSource bps = BitmapSource.Create(width, height, 96, 96, fmt, null, signatureBitmapBytes, (width * fmt.BitsPerPixel + 7) / 8);
- imageControl.Source = bps;
- }
- else
- {
- imageControl.Source = null;
- }
- }
- public static byte[] GetTempSignatureImage(CPDFSignatureWidget signatureWidget, out int width, out int height)
- {
- CRect rect = signatureWidget.GetRect();
- var flags = BindingFlags.NonPublic | BindingFlags.Static;
- var dpiProperty = typeof(SystemParameters).GetProperty("Dpi", flags);
- int dpi = (int)dpiProperty.GetValue(null, null);
- width = (int)(rect.width() * dpi / 72D * 2);
- height = (int)(rect.height() * dpi / 72D * 2);
- byte[] imageData = new byte[width * height * 4];
- signatureWidget.RenderAnnot(width, height, imageData, CPDFAppearanceType.Normal);
- return imageData;
- }
- private void TextAlignBtn_Click(object sender, RoutedEventArgs e)
- {
- ToggleButton checkBtn = sender as ToggleButton;
- if (checkBtn == null)
- {
- return;
- }
- checkBtn.IsChecked = true;
- if (checkBtn != TextAlignLeftBtn)
- {
- TextAlignLeftBtn.IsChecked = false;
- }
- if (checkBtn != TextAlignRightBtn)
- {
- TextAlignRightBtn.IsChecked = false;
- }
- }
- private void Border_MouseDown(object sender, MouseButtonEventArgs e)
- {
- Border clickBorder = sender as Border;
- if (clickBorder == null || clickBorder.Tag == null)
- {
- return;
- }
- SetCheckedTab(clickBorder.Tag.ToString());
- ImagePickPanel.Visibility = Visibility.Hidden;
- if (clickBorder == TrackpadBorder)
- {
- CanvaDrawPopup.Visibility = Visibility.Visible;
- }
- if (clickBorder == ImageBorder)
- {
- ImagePickPanel.Visibility = Visibility.Visible;
- }
- }
- private void SetCheckedTab(string tab)
- {
- if (TabDict != null && TabDict.ContainsKey(tab))
- {
- foreach (string key in TabDict.Keys)
- {
- Border checkBorder = TabDict[key];
- if (checkBorder == null)
- {
- continue;
- }
- checkBorder.BorderThickness = new Thickness(0);
- if (key == tab)
- {
- checkBorder.BorderThickness = new Thickness(0, 0, 0, 2);
- }
- }
- }
- }
- private void CanvasPopupClose_Click(object sender, RoutedEventArgs e)
- {
- CanvaDrawPopup.Visibility = Visibility.Collapsed;
- }
- private void CanvasClearBtn_Click(object sender, RoutedEventArgs e)
- {
- DrawInkCanvas.Strokes.Clear();
- }
- public void GetDrawInk()
- {
- if (DrawInkCanvas != null && DrawInkCanvas.Strokes != null && DrawInkCanvas.Strokes.Count > 0)
- {
- Rect bound = DrawInkCanvas.Strokes.GetBounds();
- DrawingVisual drawVisual = new DrawingVisual();
- DrawingContext drawContext = drawVisual.RenderOpen();
- foreach (Stroke drawStroke in DrawInkCanvas.Strokes)
- {
- Pen drawPen = new Pen(new SolidColorBrush(drawStroke.DrawingAttributes.Color), drawStroke.DrawingAttributes.Width);
- PathGeometry drawPath = new PathGeometry();
- PathFigureCollection Figures = new PathFigureCollection();
- PathFigure AddFigure = new PathFigure();
- Figures.Add(AddFigure);
- drawPath.Figures = Figures;
- if (drawStroke.StylusPoints.Count > 1)
- {
- StylusPoint startPoint = drawStroke.StylusPoints[0];
- AddFigure.StartPoint = new Point(startPoint.X - bound.X, startPoint.Y - bound.Y);
- for (int i = 1; i < drawStroke.StylusPoints.Count; i++)
- {
- StylusPoint drawPoint = drawStroke.StylusPoints[i];
- Point offsetPoint = new Point(drawPoint.X - bound.X, drawPoint.Y - bound.Y);
- LineSegment drawSegment = new LineSegment();
- drawSegment.Point = offsetPoint;
- AddFigure.Segments.Add(drawSegment);
- }
- }
- if (AddFigure.Segments.Count > 0)
- {
- drawContext.DrawGeometry(null, drawPen, drawPath);
- }
- }
- drawContext.Close();
- RenderTargetBitmap renderBitmap = new RenderTargetBitmap((int)bound.Width, (int)bound.Height, 96, 96, PixelFormats.Pbgra32);
- renderBitmap.Render(drawVisual);
- }
- }
- private void ReasonCheckBox_Click(object sender, RoutedEventArgs e)
- {
- CheckBox checkItem = sender as CheckBox;
- if (checkItem == null)
- {
- return;
- }
- ReasonPanel.Visibility = checkItem.IsChecked == true ? Visibility.Visible : Visibility.Collapsed;
- }
- }
- }
|