CPDFTempStampUI.xaml.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.Tool;
  5. using Compdfkit_Tools.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. namespace Compdfkit_Tools.PDFControlUI
  12. {
  13. /// <summary>
  14. /// Interaction logic for CPDFImageUI.xaml
  15. /// </summary>
  16. public partial class CPDFTempStampUI : UserControl
  17. {
  18. private CPDFStampAnnotation stampAnnot;
  19. private PDFViewControl viewControl;
  20. public CPDFTempStampUI()
  21. {
  22. InitializeComponent();
  23. }
  24. public void SetPresentAnnotAttrib(StampParam param,CPDFStampAnnotation annot,CPDFDocument pdfDoc,PDFViewControl view)
  25. {
  26. stampAnnot = null;
  27. NoteTextBox.Text = param.Content;
  28. stampAnnot=annot;
  29. viewControl = view;
  30. WriteableBitmap writeableBitmap = GetAnnotImage();
  31. CPDFAnnotationPreviewerControl.DrawStampPreview(writeableBitmap);
  32. }
  33. private void NoteTextBox_TextChanged(object sender, TextChangedEventArgs e)
  34. {
  35. if(stampAnnot != null && stampAnnot.IsValid())
  36. {
  37. stampAnnot.SetContent(NoteTextBox.Text);
  38. stampAnnot.UpdateAp();
  39. if (viewControl != null && viewControl.PDFViewTool != null)
  40. {
  41. CPDFViewer pdfViewer = viewControl.PDFViewTool.GetCPDFViewer();
  42. if (pdfViewer != null)
  43. {
  44. pdfViewer.UpDataAnnotFrame();
  45. }
  46. }
  47. }
  48. }
  49. private WriteableBitmap GetAnnotImage()
  50. {
  51. if(stampAnnot!=null && stampAnnot.IsValid())
  52. {
  53. CRect rawRect=stampAnnot.GetRect();
  54. int width = (int)(rawRect.width() / 72D * 96D);
  55. int height = (int)(rawRect.height() / 72D * 96D);
  56. Rect rotateRect = new Rect(0, 0, width, height);
  57. Matrix rotateMatrix = new Matrix();
  58. rotateMatrix.RotateAt(-90 * stampAnnot.Page.Rotation, width / 2, height / 2);
  59. rotateRect.Transform(rotateMatrix);
  60. byte[] ImageArray = new byte[(int)rotateRect.Width * (int)rotateRect.Height * 4];
  61. stampAnnot.RenderAnnot((int)rotateRect.Width, (int)rotateRect.Height, ImageArray);
  62. WriteableBitmap writeBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
  63. writeBitmap.WritePixels(new Int32Rect(0, 0, width, height), ImageArray, width * 4, 0);
  64. return writeBitmap;
  65. }
  66. return null;
  67. }
  68. }
  69. }