AnnotationListItem.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_Office.Helper;
  9. using PDF_Office.Model.BOTA;
  10. using PDF_Office.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_Office.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. Grid grid = e.Source as Grid;
  44. var args = grid.DataContext as AnnotationHandlerEventArgs;
  45. AnnotHandlerEventArgs data = args.AnnotHandlerEventArgs;
  46. TxbMarkupContent.Text = data.MarkupContent;
  47. TxbContent.Text = data.Content;
  48. if (data == null)
  49. {
  50. return;
  51. }
  52. switch (data.EventType)
  53. {
  54. case AnnotArgsType.AnnotFreeText:
  55. BtnAnnotFreeText.Visibility = Visibility.Visible;
  56. TxbMarkupContent.Foreground = new SolidColorBrush((data as FreeTextAnnotArgs).FontColor);
  57. break;
  58. case AnnotArgsType.AnnotHighlight:
  59. BtnHighlight.Visibility = Visibility.Visible;
  60. PathHighlight.Background = new SolidColorBrush((data as TextHighlightAnnotArgs).Color);
  61. //BOTA - 注释,MVP不处理高亮 / 下划线 / 删除线的样式,只显示文字
  62. //if (!string.IsNullOrEmpty(TxbMarkupContent.Text))
  63. //{
  64. // TxbMarkupContent.Background = new SolidColorBrush((data as TextHighlightAnnotArgs).Color);
  65. //}
  66. break;
  67. case AnnotArgsType.AnnotFreehand:
  68. BtnFreeHand.Visibility = Visibility.Visible;
  69. PathFreehand.Fill = new SolidColorBrush((data as FreehandAnnotArgs).InkColor);
  70. ImageContext.Visibility = Visibility.Visible;
  71. TxbMarkupContent.Visibility = Visibility.Collapsed;
  72. SetImageContext(args, data as FreehandAnnotArgs);
  73. //var encoder = new PngBitmapEncoder();
  74. //encoder.Frames.Add(BitmapFrame.Create((BitmapSource)ImageContext.Source));
  75. //FileStream file = new FileStream(String.Format($@"C:\Users\oyxh\Desktop\images\PDFText\{data.AnnotIndex}.png"), FileMode.Create);
  76. //encoder.Save(file);
  77. //file.Close();
  78. break;
  79. case AnnotArgsType.AnnotSquiggly://波浪线
  80. AnnotSquiggly.Visibility = Visibility.Visible;
  81. AnnotSquigglycolor.Stroke = new SolidColorBrush((data as TextSquigglyAnnotArgs).Color);
  82. #region TO DO
  83. //TextDecoration mySquiggly = new TextDecoration();
  84. //Pen myPen = new Pen();
  85. //myPen.Brush = new SolidColorBrush((data as TextSquigglyAnnotArgs).Color);
  86. //myPen.Brush.Opacity = 0.8;
  87. //myPen.Thickness = 2;
  88. //myPen.DashStyle = DashStyles.Dash;
  89. //mySquiggly.Pen = myPen;
  90. //mySquiggly.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  91. //TextDecorationCollection myCollection = new TextDecorationCollection();
  92. //myCollection.Add(mySquiggly);
  93. //TxbContext.TextDecorations = myCollection;
  94. #endregion TO DO
  95. break;
  96. case AnnotArgsType.AnnotStamp:
  97. BtnAnnotStamp.Visibility = Visibility.Visible;
  98. break;
  99. case AnnotArgsType.AnnotStrikeout://删除线
  100. BtnAnnotStrikeout.Visibility = Visibility.Visible;
  101. PathStrikeoutyColor.Fill = new SolidColorBrush((data as TextStrikeoutAnnotArgs).Color);
  102. #region to do
  103. //TextDecoration myStrikeout = new TextDecoration();
  104. //Pen myPen2 = new Pen();
  105. //myPen2.Brush = new SolidColorBrush((data as TextStrikeoutAnnotArgs).Color);
  106. //myPen2.Brush.Opacity = 0.8;
  107. //myPen2.Thickness = 2;
  108. //myStrikeout.Pen = myPen2;
  109. //myStrikeout.PenOffset = -4;
  110. //myStrikeout.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  111. //TextDecorationCollection myCollection2 = new TextDecorationCollection();
  112. //myCollection2.Add(myStrikeout);
  113. ////因为波浪线无法实现 暂时只显示文字 不显示下划线,删除线等
  114. //TxbMarkupContent.TextDecorations = myCollection2;
  115. #endregion to do
  116. break;
  117. case AnnotArgsType.AnnotSticky://便签
  118. BtnAnnotSticky.Visibility = Visibility;
  119. PathSticky.Fill = new SolidColorBrush((data as StickyAnnotArgs).Color);
  120. break;
  121. case AnnotArgsType.AnnotUnderline:
  122. BtnUnderLine.Visibility = Visibility.Visible;
  123. RectangleUnderline.Fill = new SolidColorBrush((data as TextUnderlineAnnotArgs).Color);
  124. #region TO DO
  125. //TextDecoration myUnderline = new TextDecoration();
  126. //Pen myPen1 = new Pen();
  127. //myPen1.Brush = new SolidColorBrush((data as TextUnderlineAnnotArgs).Color);
  128. //myPen1.Brush.Opacity = 0.8;
  129. //myPen1.Thickness = 2;
  130. //myUnderline.Pen = myPen1;
  131. //myUnderline.PenThicknessUnit = TextDecorationUnit.FontRecommended;
  132. //TextDecorationCollection myCollection1 = new TextDecorationCollection();
  133. //myCollection1.Add(myUnderline);
  134. //TxbMarkupContent.TextDecorations = myCollection1;
  135. #endregion TO DO
  136. break;
  137. case AnnotArgsType.AnnotLine:
  138. if ((data as LineAnnotArgs).HeadLineType >= (C_LINE_TYPE)1 || (data as LineAnnotArgs).TailLineType >= (C_LINE_TYPE)1)
  139. {
  140. BtnSharpArrow.Visibility = Visibility.Visible;
  141. PathArrow.Fill = new SolidColorBrush((data as LineAnnotArgs).LineColor);
  142. }
  143. else
  144. {
  145. PathSharpLine.Fill = new SolidColorBrush((data as LineAnnotArgs).LineColor);
  146. BtnSharpLine.Visibility = Visibility.Visible;
  147. }
  148. ///对于形状注释等只有Note 的处理
  149. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  150. {
  151. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  152. TxbMarkupContent.Text = "";
  153. }
  154. break;
  155. case AnnotArgsType.AnnotSquare:
  156. BtnAnnotSquare.Visibility = Visibility.Visible;
  157. RectAnnotSquare.Stroke = new SolidColorBrush((data as SquareAnnotArgs).LineColor);
  158. RectAnnotSquare.Fill = new SolidColorBrush((data as SquareAnnotArgs).BgColor);
  159. ///对于形状注释等只有Note 的处理
  160. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  161. {
  162. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  163. TxbMarkupContent.Text = "";
  164. }
  165. break;
  166. case AnnotArgsType.AnnotCircle:
  167. BtnAnnotCircle.Visibility = Visibility.Visible;
  168. EllipseCircle.Stroke = new SolidColorBrush((data as CircleAnnotArgs).LineColor);
  169. EllipseCircle.Fill = new SolidColorBrush((data as CircleAnnotArgs).BgColor);
  170. ///对于形状注释等只有Note 的处理
  171. if (!string.IsNullOrEmpty(data.Content) && string.IsNullOrEmpty(data.MarkupContent))
  172. {
  173. TxbMarkupContent.Text = Regex.Replace(data.Content, "[\r\n]", " ");
  174. TxbMarkupContent.Text = "";
  175. }
  176. break;
  177. case AnnotArgsType.AnnotLink:
  178. BtnAnnotLink.Visibility = Visibility.Visible;
  179. if (data is LinkAnnotArgs linkAnnotArgs)
  180. {
  181. if (string.IsNullOrEmpty(linkAnnotArgs.Content))
  182. {
  183. if (linkAnnotArgs.DestIndex != -1)
  184. {
  185. TxbMarkupContent.Text = string.Format($"To Page {linkAnnotArgs.DestIndex + 1}");
  186. }
  187. else
  188. {
  189. TxbMarkupContent.Text = string.Format($"{linkAnnotArgs.URI}");
  190. }
  191. }
  192. }
  193. break;
  194. default:
  195. break;
  196. }
  197. }
  198. private void SetImageContext(AnnotationHandlerEventArgs args, FreehandAnnotArgs freehandAnnotArgs)
  199. {
  200. double width = args.WriteableBitmap.Width;
  201. double height = args.WriteableBitmap.Height;
  202. #region 方案一
  203. CPDFDocument doc = args.Document;
  204. CPDFPage docPage = doc.PageAtIndex(args.PageIndex, false);
  205. if (docPage != null)
  206. {
  207. //BitmapImage bitmapImage = new BitmapImage();
  208. //bitmapImage.BeginInit();
  209. //bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
  210. //bitmapImage.StreamSource = args.WriteableBitmap.StreamSource;
  211. //switch (docPage.Rotation)
  212. //{
  213. // case 0:
  214. // bitmapImage.Rotation = Rotation.Rotate0;
  215. // break;
  216. // case 1:
  217. // bitmapImage.Rotation = Rotation.Rotate90;
  218. // break;
  219. // case 2:
  220. // bitmapImage.Rotation = Rotation.Rotate180;
  221. // break;
  222. // case 3:
  223. // bitmapImage.Rotation = Rotation.Rotate270;
  224. // break;
  225. //}
  226. //bitmapImage.EndInit();
  227. //ImageContext.Source = bitmapImage;
  228. //switch (rotate)
  229. //{
  230. // case 0:
  231. // RotateTransform rotateTransform = new RotateTransform(0);
  232. // ImageContext.RenderTransform = rotateTransform;
  233. // break;
  234. // case 1:
  235. // //rotateTransform = new RotateTransform(90);//90度
  236. // //ImageContext.RenderTransform = rotateTransform;
  237. // TransformGroup tg = ImageContext.RenderTransform as TransformGroup;
  238. // var tgnew = tg.CloneCurrentValue();
  239. // if (tgnew != null)
  240. // {
  241. // RotateTransform rt = tgnew.Children[2] as RotateTransform;
  242. // ImageContext.RenderTransformOrigin = new Point(0.5, 0.5);
  243. // rt.Angle = 90;
  244. // }
  245. // // 重新给图像赋值Transform变换属性
  246. // ImageContext.RenderTransform = tgnew;
  247. // break;
  248. // case 2:
  249. // rotateTransform = new RotateTransform(180);
  250. // ImageContext.RenderTransform = rotateTransform;
  251. // break;
  252. // case 3:
  253. // rotateTransform = new RotateTransform(270);
  254. // ImageContext.RenderTransform = rotateTransform;
  255. // break;
  256. //}
  257. }
  258. #endregion 方案一
  259. if (width > 180)
  260. {
  261. ImageContext.Stretch = Stretch.Uniform;
  262. }
  263. else
  264. {
  265. if (height >= 900)
  266. {
  267. ImageContext.MaxHeight = 900;
  268. ImageContext.Stretch = Stretch.Uniform;
  269. }
  270. if (height < 900)
  271. {
  272. if (height <= 20)
  273. {
  274. ImageContext.MinHeight = height;
  275. ImageContext.MinHeight = 30;
  276. }
  277. ImageContext.Stretch = Stretch.None;
  278. }
  279. else
  280. {
  281. ImageContext.Stretch = Stretch.Uniform;
  282. }
  283. }
  284. }
  285. }
  286. }