CPDFTempStampUI.xaml.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. viewControl.UpdateAnnotFrame();
  42. }
  43. }
  44. }
  45. private WriteableBitmap GetAnnotImage()
  46. {
  47. if(stampAnnot!=null && stampAnnot.IsValid())
  48. {
  49. CRect rawRect=stampAnnot.GetRect();
  50. int width = (int)(rawRect.width() / 72D * 96D);
  51. int height = (int)(rawRect.height() / 72D * 96D);
  52. Rect rotateRect = new Rect(0, 0, width, height);
  53. Matrix rotateMatrix = new Matrix();
  54. rotateMatrix.RotateAt(-90 * stampAnnot.Page.Rotation, width / 2, height / 2);
  55. rotateRect.Transform(rotateMatrix);
  56. byte[] ImageArray = new byte[(int)rotateRect.Width * (int)rotateRect.Height * 4];
  57. stampAnnot.RenderAnnot((int)rotateRect.Width, (int)rotateRect.Height, ImageArray);
  58. WriteableBitmap writeBitmap = new WriteableBitmap(width, height, 96, 96, PixelFormats.Bgra32, null);
  59. writeBitmap.WritePixels(new Int32Rect(0, 0, width, height), ImageArray, width * 4, 0);
  60. return writeBitmap;
  61. }
  62. return null;
  63. }
  64. }
  65. }