CPDFTempStampUI.xaml.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using ComPDFKit.Controls.PDFControl;
  6. using ComPDFKitViewer;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Media;
  10. using System.Windows.Media.Imaging;
  11. using ComPDFKit.Tool.Help;
  12. using ComPDFKit.Tool.UndoManger;
  13. using ComPDFKitViewer.Helper;
  14. namespace ComPDFKit.Controls.PDFControlUI
  15. {
  16. /// <summary>
  17. /// Interaction logic for CPDFImageUI.xaml
  18. /// </summary>
  19. public partial class CPDFTempStampUI : UserControl
  20. {
  21. private CPDFStampAnnotation stampAnnot;
  22. private PDFViewControl viewControl;
  23. public CPDFTempStampUI()
  24. {
  25. InitializeComponent();
  26. }
  27. public void SetPresentAnnotAttrib(StampParam param,CPDFStampAnnotation annot,CPDFDocument pdfDoc,PDFViewControl view)
  28. {
  29. stampAnnot = null;
  30. NoteTextBox.Text = param?.Content;
  31. stampAnnot=annot;
  32. viewControl = view;
  33. WriteableBitmap writeableBitmap = GetAnnotImage();
  34. CPDFAnnotationPreviewerControl.DrawStampPreview(writeableBitmap);
  35. }
  36. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  37. {
  38. if(stampAnnot != null && stampAnnot.IsValid())
  39. {
  40. if (viewControl != null && viewControl.PDFViewTool != null)
  41. {
  42. StampAnnotHistory history = new StampAnnotHistory();
  43. history.PDFDoc = viewControl.GetCPDFViewer().GetDocument();
  44. history.Action = HistoryAction.Update;
  45. history.PreviousParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, stampAnnot.Page.PageIndex, stampAnnot);
  46. stampAnnot.SetContent(NoteTextBox.Text);
  47. stampAnnot.UpdateAp();
  48. viewControl.UpdateAnnotFrame();
  49. history.CurrentParam = ParamConverter.CPDFDataConverterToAnnotParam(history.PDFDoc, stampAnnot.Page.PageIndex, stampAnnot);
  50. viewControl.GetCPDFViewer().UndoManager.AddHistory(history);
  51. }
  52. }
  53. }
  54. private WriteableBitmap GetAnnotImage()
  55. {
  56. if(stampAnnot!=null && stampAnnot.IsValid())
  57. {
  58. CRect rawRect=stampAnnot.GetRect();
  59. int width = (int)(rawRect.width() / 72D * 96D);
  60. int height = (int)(rawRect.height() / 72D * 96D);
  61. Rect rotateRect = new Rect(0, 0, width, height);
  62. Matrix rotateMatrix = new Matrix();
  63. rotateMatrix.RotateAt(-90 * stampAnnot.Page.Rotation, width / 2, height / 2);
  64. rotateRect.Transform(rotateMatrix);
  65. byte[] ImageArray = new byte[(int)rotateRect.Width * (int)rotateRect.Height * 4];
  66. stampAnnot.RenderAnnot((int)rotateRect.Width, (int)rotateRect.Height, ImageArray);
  67. WriteableBitmap writeBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
  68. writeBitmap.WritePixels(new Int32Rect(0, 0, width, height), ImageArray, width * 4, 0);
  69. return writeBitmap;
  70. }
  71. return null;
  72. }
  73. }
  74. }