AnnotationListItem.xaml.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  64. //文本注释、便签、链接、表格不支持添加附注
  65. TxbContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  66. TxbContent.Text = "";
  67. break;
  68. case AnnotArgsType.AnnotHighlight:
  69. if (data is TextHighlightAnnotArgs textHighlightAnnotArgs)
  70. {
  71. BtnHighlight.Visibility = Visibility.Visible;
  72. PathHighlight.Background = new SolidColorBrush(textHighlightAnnotArgs.Color);
  73. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  74. //文本注释、便签、链接、表格不支持添加附注
  75. TxbContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  76. TxbContent.Text = "";
  77. }
  78. break;
  79. case AnnotArgsType.AnnotFreehand:
  80. if (data is FreehandAnnotArgs freehandAnnotArgs)
  81. {
  82. BtnFreeHand.Visibility = Visibility.Visible;
  83. PathFreehand.Fill = new SolidColorBrush(freehandAnnotArgs.InkColor);
  84. ImageContext.Visibility = Visibility.Visible;
  85. TxbMarkupContent.Visibility = Visibility.Collapsed;
  86. SetImageContext(args, freehandAnnotArgs);
  87. //var encoder = new PngBitmapEncoder();
  88. //encoder.Frames.Add(BitmapFrame.Create((BitmapSource)ImageContext.Source));
  89. //FileStream file = new FileStream(String.Format($@"C:\Users\oyxh\Desktop\images\PDFText\{data.AnnotIndex}.png"), FileMode.Create);
  90. //encoder.Save(file);
  91. //file.Close();
  92. }
  93. break;
  94. case AnnotArgsType.AnnotSquiggly://波浪线
  95. if (data is TextSquigglyAnnotArgs textSquigglyAnnotArgs)
  96. {
  97. AnnotSquiggly.Visibility = Visibility.Visible;
  98. AnnotSquigglycolor.Stroke = new SolidColorBrush(textSquigglyAnnotArgs.Color);
  99. }
  100. #region TO DO
  101. //TextDecoration mySquiggly = new TextDecoration();
  102. //Pen myPen = new Pen();
  103. //myPen.Brush = new SolidColorBrush((data as TextSquigglyAnnotArgs).Color);
  104. //myPen.Brush.Opacity = 0.8;
  105. //myPen.Thickness = 2;
  106. //myPen.DashStyle = DashStyles.Dash;
  107. //mySquiggly.Pen = myPen;
  108. //mySquiggly.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  109. //TextDecorationCollection myCollection = new TextDecorationCollection();
  110. //myCollection.Add(mySquiggly);
  111. //TxbContext.TextDecorations = myCollection;
  112. #endregion TO DO
  113. break;
  114. case AnnotArgsType.AnnotStamp:
  115. BtnAnnotStamp.Visibility = Visibility.Visible;
  116. break;
  117. case AnnotArgsType.AnnotStrikeout://删除线
  118. if (data is TextStrikeoutAnnotArgs textStrikeoutAnnotArgs)
  119. {
  120. BtnAnnotStrikeout.Visibility = Visibility.Visible;
  121. PathStrikeoutyColor.Fill = new SolidColorBrush(textStrikeoutAnnotArgs.Color);
  122. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  123. //文本注释、便签、链接、表格不支持添加附注
  124. TxbContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  125. TxbContent.Text = "";
  126. }
  127. #region to do
  128. //TextDecoration myStrikeout = new TextDecoration();
  129. //Pen myPen2 = new Pen();
  130. //myPen2.Brush = new SolidColorBrush((data as TextStrikeoutAnnotArgs).Color);
  131. //myPen2.Brush.Opacity = 0.8;
  132. //myPen2.Thickness = 2;
  133. //myStrikeout.Pen = myPen2;
  134. //myStrikeout.PenOffset = -4;
  135. //myStrikeout.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  136. //TextDecorationCollection myCollection2 = new TextDecorationCollection();
  137. //myCollection2.Add(myStrikeout);
  138. ////因为波浪线无法实现 暂时只显示文字 不显示下划线,删除线等
  139. //TxbMarkupContent.TextDecorations = myCollection2;
  140. #endregion to do
  141. break;
  142. case AnnotArgsType.AnnotSticky://便签
  143. if (data is StickyAnnotArgs stickyAnnotArgs)
  144. {
  145. BtnAnnotSticky.Visibility = Visibility;
  146. PathSticky.Fill = new SolidColorBrush(stickyAnnotArgs.Color);
  147. }
  148. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  149. //文本注释、便签、链接、表格不支持添加附注
  150. TxbContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  151. TxbContent.Text = "";
  152. break;
  153. case AnnotArgsType.AnnotUnderline:
  154. if (data is TextUnderlineAnnotArgs textUnderlineAnnotArgs)
  155. {
  156. BtnUnderLine.Visibility = Visibility.Visible;
  157. RectangleUnderline.Fill = new SolidColorBrush(textUnderlineAnnotArgs.Color);
  158. }
  159. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  160. //文本注释、便签、链接、表格不支持添加附注
  161. TxbContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  162. TxbContent.Text = "";
  163. #region TO DO
  164. //TextDecoration myUnderline = new TextDecoration();
  165. //Pen myPen1 = new Pen();
  166. //myPen1.Brush = new SolidColorBrush((data as TextUnderlineAnnotArgs).Color);
  167. //myPen1.Brush.Opacity = 0.8;
  168. //myPen1.Thickness = 2;
  169. //myUnderline.Pen = myPen1;
  170. //myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  171. //TextDecorationCollection myCollection1 = new TextDecorationCollection();
  172. //myCollection1.Add(myUnderline);
  173. //TxbMarkupContent.TextDecorations = myCollection1;
  174. #endregion TO DO
  175. break;
  176. case AnnotArgsType.AnnotLine:
  177. if (data is LineAnnotArgs lineAnnotArgs)
  178. {
  179. if (lineAnnotArgs.HeadLineType >= (C_LINE_TYPE)1 || lineAnnotArgs.TailLineType >= (C_LINE_TYPE)1)
  180. {
  181. BtnSharpArrow.Visibility = Visibility.Visible;
  182. PathArrow.Fill = new SolidColorBrush(lineAnnotArgs.LineColor);
  183. }
  184. else
  185. {
  186. PathSharpLine.Fill = new SolidColorBrush(lineAnnotArgs.LineColor);
  187. BtnSharpLine.Visibility = Visibility.Visible;
  188. }
  189. ///对于形状注释等只有Note 的处理
  190. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  191. {
  192. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  193. TxbMarkupContent.Text = "";
  194. }
  195. }
  196. break;
  197. case AnnotArgsType.AnnotSquare:
  198. if (data is SquareAnnotArgs squareAnnotArgs)
  199. {
  200. BtnAnnotSquare.Visibility = Visibility.Visible;
  201. RectAnnotSquare.Stroke = new SolidColorBrush(squareAnnotArgs.LineColor);
  202. RectAnnotSquare.Fill = new SolidColorBrush(squareAnnotArgs.BgColor);
  203. ///对于形状注释等只有Note 的处理
  204. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  205. {
  206. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  207. TxbMarkupContent.Text = "";
  208. }
  209. }
  210. break;
  211. case AnnotArgsType.AnnotCircle:
  212. if (data is CircleAnnotArgs circleAnnotArgs)
  213. {
  214. BtnAnnotCircle.Visibility = Visibility.Visible;
  215. EllipseCircle.Stroke = new SolidColorBrush(circleAnnotArgs.LineColor);
  216. EllipseCircle.Fill = new SolidColorBrush(circleAnnotArgs.BgColor);
  217. ///对于形状注释等只有Note 的处理
  218. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  219. {
  220. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  221. TxbMarkupContent.Text = "";
  222. }
  223. }
  224. break;
  225. case AnnotArgsType.AnnotLink:
  226. BtnAnnotLink.Visibility = Visibility.Visible;
  227. if (data is LinkAnnotArgs linkAnnotArgs)
  228. {
  229. if (string.IsNullOrEmpty(linkAnnotArgs.Content))
  230. {
  231. if (linkAnnotArgs.DestIndex != -1)
  232. {
  233. TxbMarkupContent.Text = string.Format($"To Page {linkAnnotArgs.DestIndex + 1}");
  234. }
  235. else
  236. {
  237. TxbMarkupContent.Text = string.Format($"{linkAnnotArgs.URI}");
  238. }
  239. }
  240. }
  241. break;
  242. default:
  243. break;
  244. }
  245. }
  246. }
  247. }
  248. private void SetImageContext(AnnotationHandlerEventArgs args, FreehandAnnotArgs freehandAnnotArgs)
  249. {
  250. if (args == null && freehandAnnotArgs == null)
  251. {
  252. return;
  253. }
  254. if (args.WriteableBitmap == null)
  255. {
  256. return;
  257. }
  258. double width = args.WriteableBitmap.Width;
  259. double height = args.WriteableBitmap.Height;
  260. CPDFDocument doc = args.Document;
  261. if (doc != null)
  262. {
  263. CPDFPage docPage = doc.PageAtIndex(args.PageIndex, false);
  264. if (docPage != null)
  265. {
  266. double maxWidth = docPage.PageSize.Width;
  267. double maxHeight = docPage.PageSize.Height;
  268. ImageContext.MaxHeight = maxHeight;
  269. if (width > 170)
  270. {
  271. ImageContext.Stretch = Stretch.Uniform;
  272. }
  273. else
  274. {
  275. if (height >= maxHeight / 2)
  276. {
  277. //ImageContext.MaxHeight = 900;
  278. ImageContext.Stretch = Stretch.None;
  279. }
  280. if (height < maxHeight / 2)
  281. {
  282. if (height <= 20)
  283. {
  284. ImageContext.MinHeight = height;
  285. ImageContext.Height = 30;
  286. }
  287. ImageContext.Stretch = Stretch.None;
  288. }
  289. //else
  290. //{
  291. // ImageContext.Stretch = Stretch.Uniform;
  292. //}
  293. }
  294. }
  295. }
  296. }
  297. }
  298. }