AnnotationListItem.xaml.cs 15 KB

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