AnnotationListItem.xaml.cs 18 KB

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