AnnotationListItem.xaml.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. using ComPDFKit.Import;
  2. using ComPDFKit.PDFAnnotation;
  3. using ComPDFKit.PDFDocument;
  4. using ComPDFKit.PDFPage;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.AnnotEvent;
  7. using ComPDFKitViewer.PdfViewer;
  8. using PDF_Master.Helper;
  9. using PDF_Master.Model.BOTA;
  10. using PDF_Master.ViewModels.BOTA;
  11. using System;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Text.RegularExpressions;
  17. using System.Threading.Tasks;
  18. using System.Windows;
  19. using System.Windows.Controls;
  20. using System.Windows.Data;
  21. using System.Windows.Documents;
  22. using System.Windows.Forms;
  23. using System.Windows.Input;
  24. using System.Windows.Media;
  25. using System.Windows.Media.Imaging;
  26. using System.Windows.Navigation;
  27. using System.Windows.Shapes;
  28. using static Dropbox.Api.Files.SearchMatchTypeV2;
  29. namespace PDF_Master.Views.BOTA
  30. {
  31. /// <summary>
  32. /// AnnotationListItem.xaml 的交互逻辑
  33. /// </summary>
  34. public partial class AnnotationListItem : System.Windows.Controls.UserControl
  35. {
  36. //private Brush strikeoutColor = new SolidColorBrush(Color.FromRgb(0xFF, 0xBB, 0x00));
  37. public AnnotationListItem()
  38. {
  39. InitializeComponent();
  40. }
  41. private void GridIco_Loaded(object sender, RoutedEventArgs e)
  42. {
  43. if (e.Source is Grid grid)
  44. {
  45. if (grid.DataContext is AnnotationHandlerEventArgs args)
  46. {
  47. //var args = grid.DataContext as AnnotationHandlerEventArgs;
  48. AnnotHandlerEventArgs data = args.AnnotHandlerEventArgs;
  49. if (data == null)
  50. {
  51. return;
  52. }
  53. TxbMarkupContent.Text = data.MarkupContent;
  54. TxbContent.Text = data.Content;
  55. switch (data.EventType)
  56. {
  57. case AnnotArgsType.AnnotFreeText:
  58. if (data is FreeTextAnnotArgs freeTextAnnotArgs)
  59. {
  60. BtnAnnotFreeText.Visibility = Visibility.Visible;
  61. TxbMarkupContent.Foreground = new SolidColorBrush(freeTextAnnotArgs.FontColor);
  62. }
  63. break;
  64. case AnnotArgsType.AnnotHighlight:
  65. if (data is TextHighlightAnnotArgs textHighlightAnnotArgs)
  66. {
  67. BtnHighlight.Visibility = Visibility.Visible;
  68. PathHighlight.Background = new SolidColorBrush(textHighlightAnnotArgs.Color);
  69. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  70. //if (!string.IsNullOrEmpty(TxbMarkupContent.Text))
  71. //{
  72. // TxbMarkupContent.Background = new SolidColorBrush((data as TextHighlightAnnotArgs).Color);
  73. //}
  74. }
  75. break;
  76. case AnnotArgsType.AnnotFreehand:
  77. if (data is FreehandAnnotArgs freehandAnnotArgs)
  78. {
  79. BtnFreeHand.Visibility = Visibility.Visible;
  80. PathFreehand.Fill = new SolidColorBrush(freehandAnnotArgs.InkColor);
  81. ImageContext.Visibility = Visibility.Visible;
  82. TxbMarkupContent.Visibility = Visibility.Collapsed;
  83. SetImageContext(args, freehandAnnotArgs);
  84. //var encoder = new PngBitmapEncoder();
  85. //encoder.Frames.Add(BitmapFrame.Create((BitmapSource)ImageContext.Source));
  86. //FileStream file = new FileStream(String.Format($@"C:\Users\oyxh\Desktop\images\PDFText\{data.AnnotIndex}.png"), FileMode.Create);
  87. //encoder.Save(file);
  88. //file.Close();
  89. }
  90. break;
  91. case AnnotArgsType.AnnotSquiggly://波浪线
  92. if (data is TextSquigglyAnnotArgs textSquigglyAnnotArgs)
  93. {
  94. AnnotSquiggly.Visibility = Visibility.Visible;
  95. AnnotSquigglycolor.Stroke = new SolidColorBrush(textSquigglyAnnotArgs.Color);
  96. }
  97. #region TO DO
  98. //TextDecoration mySquiggly = new TextDecoration();
  99. //Pen myPen = new Pen();
  100. //myPen.Brush = new SolidColorBrush((data as TextSquigglyAnnotArgs).Color);
  101. //myPen.Brush.Opacity = 0.8;
  102. //myPen.Thickness = 2;
  103. //myPen.DashStyle = DashStyles.Dash;
  104. //mySquiggly.Pen = myPen;
  105. //mySquiggly.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  106. //TextDecorationCollection myCollection = new TextDecorationCollection();
  107. //myCollection.Add(mySquiggly);
  108. //TxbContext.TextDecorations = myCollection;
  109. #endregion TO DO
  110. break;
  111. case AnnotArgsType.AnnotStamp:
  112. BtnAnnotStamp.Visibility = Visibility.Visible;
  113. break;
  114. case AnnotArgsType.AnnotStrikeout://删除线
  115. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  116. {
  117. BtnAnnotStrikeout.Visibility = Visibility.Visible;
  118. PathStrikeoutyColor.Fill = new SolidColorBrush(textStrikeoutAnnotArgs.Color);
  119. }
  120. #region to do
  121. //TextDecoration myStrikeout = new TextDecoration();
  122. //Pen myPen2 = new Pen();
  123. //myPen2.Brush = new SolidColorBrush((data as TextStrikeoutAnnotArgs).Color);
  124. //myPen2.Brush.Opacity = 0.8;
  125. //myPen2.Thickness = 2;
  126. //myStrikeout.Pen = myPen2;
  127. //myStrikeout.PenOffset = -4;
  128. //myStrikeout.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  129. //TextDecorationCollection myCollection2 = new TextDecorationCollection();
  130. //myCollection2.Add(myStrikeout);
  131. ////因为波浪线无法实现 暂时只显示文字 不显示下划线,删除线等
  132. //TxbMarkupContent.TextDecorations = myCollection2;
  133. #endregion to do
  134. break;
  135. case AnnotArgsType.AnnotSticky://便签
  136. if (data is StickyAnnotArgs stickyAnnotArgs)
  137. {
  138. BtnAnnotSticky.Visibility = Visibility;
  139. PathSticky.Fill = new SolidColorBrush(stickyAnnotArgs.Color);
  140. }
  141. break;
  142. case AnnotArgsType.AnnotUnderline:
  143. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  144. {
  145. BtnUnderLine.Visibility = Visibility.Visible;
  146. RectangleUnderline.Fill = new SolidColorBrush(textUnderlineAnnotArgs.Color);
  147. }
  148. #region TO DO
  149. //TextDecoration myUnderline = new TextDecoration();
  150. //Pen myPen1 = new Pen();
  151. //myPen1.Brush = new SolidColorBrush((data as TextUnderlineAnnotArgs).Color);
  152. //myPen1.Brush.Opacity = 0.8;
  153. //myPen1.Thickness = 2;
  154. //myUnderline.Pen = myPen1;
  155. //myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  156. //TextDecorationCollection myCollection1 = new TextDecorationCollection();
  157. //myCollection1.Add(myUnderline);
  158. //TxbMarkupContent.TextDecorations = myCollection1;
  159. #endregion TO DO
  160. break;
  161. case AnnotArgsType.AnnotLine:
  162. if (data is LineAnnotArgs lineAnnotArgs)
  163. {
  164. if (lineAnnotArgs.HeadLineType >= (C_LINE_TYPE)1 || lineAnnotArgs.TailLineType >= (C_LINE_TYPE)1)
  165. {
  166. BtnSharpArrow.Visibility = Visibility.Visible;
  167. PathArrow.Fill = new SolidColorBrush(lineAnnotArgs.LineColor);
  168. }
  169. else
  170. {
  171. PathSharpLine.Fill = new SolidColorBrush(lineAnnotArgs.LineColor);
  172. BtnSharpLine.Visibility = Visibility.Visible;
  173. }
  174. ///对于形状注释等只有Note 的处理
  175. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  176. {
  177. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  178. TxbMarkupContent.Text = "";
  179. }
  180. }
  181. break;
  182. case AnnotArgsType.AnnotSquare:
  183. if (data is SquareAnnotArgs squareAnnotArgs)
  184. {
  185. BtnAnnotSquare.Visibility = Visibility.Visible;
  186. RectAnnotSquare.Stroke = new SolidColorBrush(squareAnnotArgs.LineColor);
  187. RectAnnotSquare.Fill = new SolidColorBrush(squareAnnotArgs.BgColor);
  188. ///对于形状注释等只有Note 的处理
  189. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  190. {
  191. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  192. TxbMarkupContent.Text = "";
  193. }
  194. }
  195. break;
  196. case AnnotArgsType.AnnotCircle:
  197. if (data is CircleAnnotArgs circleAnnotArgs)
  198. {
  199. BtnAnnotCircle.Visibility = Visibility.Visible;
  200. EllipseCircle.Stroke = new SolidColorBrush(circleAnnotArgs.LineColor);
  201. EllipseCircle.Fill = new SolidColorBrush(circleAnnotArgs.BgColor);
  202. ///对于形状注释等只有Note 的处理
  203. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  204. {
  205. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  206. TxbMarkupContent.Text = "";
  207. }
  208. }
  209. break;
  210. case AnnotArgsType.AnnotLink:
  211. BtnAnnotLink.Visibility = Visibility.Visible;
  212. if (data is LinkAnnotArgs linkAnnotArgs)
  213. {
  214. if (string.IsNullOrEmpty(linkAnnotArgs.Content))
  215. {
  216. if (linkAnnotArgs.DestIndex != -1)
  217. {
  218. TxbMarkupContent.Text = string.Format($"To Page {linkAnnotArgs.DestIndex + 1}");
  219. }
  220. else
  221. {
  222. TxbMarkupContent.Text = string.Format($"{linkAnnotArgs.URI}");
  223. }
  224. }
  225. }
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. }
  232. }
  233. private void SetImageContext(AnnotationHandlerEventArgs args, FreehandAnnotArgs freehandAnnotArgs)
  234. {
  235. if (args == null && freehandAnnotArgs == null)
  236. {
  237. return;
  238. }
  239. double width = args.WriteableBitmap.Width;
  240. double height = args.WriteableBitmap.Height;
  241. CPDFDocument doc = args.Document;
  242. if (doc != null)
  243. {
  244. CPDFPage docPage = doc.PageAtIndex(args.PageIndex, false);
  245. if (docPage != null)
  246. {
  247. double maxWidth = docPage.PageSize.Width;
  248. double maxHeight = docPage.PageSize.Height;
  249. ImageContext.MaxHeight = maxHeight;
  250. if (width > 180)
  251. {
  252. ImageContext.Stretch = Stretch.Uniform;
  253. }
  254. else
  255. {
  256. if (height >= maxHeight / 2)
  257. {
  258. //ImageContext.MaxHeight = 900;
  259. ImageContext.Stretch = Stretch.None;
  260. }
  261. if (height < maxHeight / 2)
  262. {
  263. if (height <= 20)
  264. {
  265. ImageContext.MinHeight = height;
  266. ImageContext.Height = 30;
  267. }
  268. ImageContext.Stretch = Stretch.None;
  269. }
  270. //else
  271. //{
  272. // ImageContext.Stretch = Stretch.Uniform;
  273. //}
  274. }
  275. }
  276. }
  277. }
  278. }
  279. }