CPDFAnnotationControl.xaml.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. using compdfkit_tools.Data;
  2. using compdfkit_tools.PDFControl;
  3. using compdfkit_tools.PDFControlUI;
  4. using ComPDFKitViewer.AnnotEvent;
  5. using ComPDFKitViewer;
  6. using ComPDFKitViewer.PdfViewer;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Data;
  15. using System.Windows.Documents;
  16. using System.Windows.Input;
  17. using System.Windows.Media.Imaging;
  18. using System.Windows.Navigation;
  19. using System.Windows.Shapes;
  20. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  21. using compdfkit_tools.Annotation.PDFAnnotationPanel.PDFAnnotationUI;
  22. using System.IO;
  23. using System.Windows.Ink;
  24. using compdfkit_tools.Annotation.PDFAnnotationUI;
  25. using Microsoft.Win32;
  26. using System.Windows.Media;
  27. namespace compdfkit_tools.PDFControl
  28. {
  29. /// <summary>
  30. /// CPDFAnnotationControl.xaml 的交互逻辑
  31. /// </summary>
  32. public partial class CPDFAnnotationControl : UserControl
  33. {
  34. private bool isTempPanel = false;
  35. private CPDFAnnotationType currentAnnotationType = CPDFAnnotationType.Unknow;
  36. private CPDFViewer pdfViewer;
  37. private UIElement annotationPanel = null;
  38. private UIElement tempAnnotationPanel = null;
  39. private CPDFMarkupUI pdfHighlightUI;
  40. private CPDFMarkupUI pdfUnderlineUI;
  41. private CPDFMarkupUI pdfSquigglyUI;
  42. private CPDFMarkupUI pdfStrikeoutUI;
  43. private CPDFShapeUI pdfSquareUI;
  44. private CPDFShapeUI pdfCircleUI;
  45. private CPDFShapeUI pdfLineUI;
  46. private CPDFShapeUI pdfArrowUI;
  47. private CPDFFreehandUI pdfFreehandUI;
  48. private CPDFFreeTextUI pdfFreeTextUI;
  49. private CPDFNoteUI pdfNoteUI;
  50. private CPDFStampUI pdfStampUI;
  51. private CPDFSignatureUI pdfSignatureUI;
  52. private CPDFLinkUI pdfLinkUI;
  53. private AnnotHandlerEventArgs annotArgs;
  54. private EraseArgs eraseArgs;
  55. private bool disableClean;
  56. public event EventHandler ClearAnnotationBar;
  57. public CPDFAnnotationControl()
  58. {
  59. InitializeComponent();
  60. }
  61. public void SetPDFViewer(CPDFViewer pdfViewer)
  62. {
  63. if (this.pdfViewer != null)
  64. {
  65. UnLoadPDFViewHandler();
  66. }
  67. this.pdfViewer = pdfViewer;
  68. LoadPDFViewHandler();
  69. }
  70. public void LoadPDFViewHandler()
  71. {
  72. if (this.pdfViewer != null)
  73. {
  74. this.pdfViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  75. this.pdfViewer.AnnotActiveHandler += PDFViewer_AnnotActiveHandler;
  76. this.pdfViewer.AnnotEditHandler -= PdfViewer_AnnotEditHandler;
  77. this.pdfViewer.AnnotEditHandler += PdfViewer_AnnotEditHandler;
  78. }
  79. }
  80. private void PdfViewer_AnnotEditHandler(object sender, List<AnnotEditEvent> e)
  81. {
  82. if (e != null && e.Count > 0 && e[0].EditAction==ActionType.Del)
  83. {
  84. if(pdfViewer.MouseMode==MouseModes.AnnotCreate && pdfViewer.ToolManager.CurrentAnnotArgs is EraseArgs)
  85. {
  86. return;
  87. }
  88. SetAnnotEventData(null);
  89. }
  90. }
  91. public void UnLoadPDFViewHandler()
  92. {
  93. if (this.pdfViewer != null)
  94. {
  95. this.pdfViewer.AnnotActiveHandler -= PDFViewer_AnnotActiveHandler;
  96. }
  97. }
  98. private void AnnotationControl_Loaded(object sender, RoutedEventArgs e)
  99. {
  100. LoadPDFViewHandler();
  101. }
  102. private void AnnotationControl_Unloaded(object sender, RoutedEventArgs e)
  103. {
  104. UnLoadPDFViewHandler();
  105. }
  106. /// <summary>
  107. /// 获取Annotation面板
  108. /// </summary>
  109. /// <returns></returns>
  110. private UIElement GetAnnotationPanel()
  111. {
  112. return AnnotationPanelContainer.Child;
  113. }
  114. private void SetAnnotationPanel(UIElement newChild)
  115. {
  116. AnnotationPanelContainer.Child = newChild;
  117. }
  118. /// <summary>
  119. /// 展开Annotation面板
  120. /// </summary>
  121. /// <param name="isExpand"></param>
  122. private void ExpandPanel()
  123. {
  124. AnnotationPanelContainer.Visibility = Visibility.Visible;
  125. }
  126. private void ChangeAnnotationData()
  127. {
  128. switch (currentAnnotationType)
  129. {
  130. case CPDFAnnotationType.Highlight:
  131. case CPDFAnnotationType.Underline:
  132. case CPDFAnnotationType.Squiggly:
  133. case CPDFAnnotationType.Strikeout:
  134. (annotationPanel as CPDFMarkupUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  135. (annotationPanel as CPDFMarkupUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  136. SetAnnotationProperty((annotationPanel as CPDFMarkupUI).GetMarkupData());
  137. break;
  138. case CPDFAnnotationType.Square:
  139. case CPDFAnnotationType.Circle:
  140. case CPDFAnnotationType.Line:
  141. case CPDFAnnotationType.Arrow:
  142. (annotationPanel as CPDFShapeUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  143. (annotationPanel as CPDFShapeUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  144. SetAnnotationProperty((annotationPanel as CPDFShapeUI).GetShapeData());
  145. break;
  146. case CPDFAnnotationType.Note:
  147. (annotationPanel as CPDFNoteUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  148. (annotationPanel as CPDFNoteUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  149. SetAnnotationProperty((annotationPanel as CPDFNoteUI).GetNoteData());
  150. break;
  151. case CPDFAnnotationType.Freehand:
  152. (annotationPanel as CPDFFreehandUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  153. (annotationPanel as CPDFFreehandUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  154. SetAnnotationProperty((annotationPanel as CPDFFreehandUI).GetFreehandData());
  155. break;
  156. case CPDFAnnotationType.FreeText:
  157. (annotationPanel as CPDFFreeTextUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  158. (annotationPanel as CPDFFreeTextUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  159. SetAnnotationProperty((annotationPanel as CPDFFreeTextUI).GetFreeTextData());
  160. break;
  161. case CPDFAnnotationType.Stamp:
  162. (annotationPanel as CPDFStampUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  163. (annotationPanel as CPDFStampUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  164. break;
  165. case CPDFAnnotationType.Signature:
  166. (annotationPanel as CPDFSignatureUI).PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  167. (annotationPanel as CPDFSignatureUI).PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. /// <summary>
  174. /// 参数解析
  175. /// </summary>
  176. /// <param name="pdfAnnotationData"></param>
  177. private void SetAnnotationProperty(CPDFAnnotationData pdfAnnotationData = null)
  178. {
  179. if (pdfAnnotationData == null)
  180. {
  181. pdfViewer.SetMouseMode(MouseModes.PanTool);
  182. return;
  183. }
  184. CPDFAnnotationType annotationType = pdfAnnotationData.AnnotationType;
  185. AnnotHandlerEventArgs annotHandlerEventArgs = null;
  186. //this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  187. Dictionary<AnnotAttrib, object> annotAttribsList = new Dictionary<AnnotAttrib, object>();
  188. switch (annotationType)
  189. {
  190. case CPDFAnnotationType.Highlight:
  191. CPDFMarkupData highlightData = pdfAnnotationData as CPDFMarkupData;
  192. annotHandlerEventArgs = new TextHighlightAnnotArgs();
  193. (annotHandlerEventArgs as TextHighlightAnnotArgs).Color = highlightData.Color;
  194. (annotHandlerEventArgs as TextHighlightAnnotArgs).Transparency = highlightData.Opacity;
  195. (annotHandlerEventArgs as TextHighlightAnnotArgs).Content = highlightData.Note;
  196. (annotHandlerEventArgs as TextHighlightAnnotArgs).Author = highlightData.Author;
  197. (annotHandlerEventArgs as TextHighlightAnnotArgs).Locked = highlightData.IsLocked;
  198. break;
  199. case CPDFAnnotationType.Underline:
  200. CPDFMarkupData underlineData = pdfAnnotationData as CPDFMarkupData;
  201. annotHandlerEventArgs = new TextUnderlineAnnotArgs();
  202. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Color = underlineData.Color;
  203. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Transparency = underlineData.Opacity;
  204. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Author = underlineData.Author;
  205. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Content = underlineData.Note;
  206. (annotHandlerEventArgs as TextUnderlineAnnotArgs).Locked = underlineData.IsLocked;
  207. break;
  208. case CPDFAnnotationType.Strikeout:
  209. CPDFMarkupData strikeoutData = pdfAnnotationData as CPDFMarkupData;
  210. annotHandlerEventArgs = new TextStrikeoutAnnotArgs();
  211. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Color = strikeoutData.Color;
  212. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Transparency = strikeoutData.Opacity;
  213. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Locked = strikeoutData.IsLocked;
  214. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Author = strikeoutData.Author;
  215. (annotHandlerEventArgs as TextStrikeoutAnnotArgs).Content = strikeoutData.Note;
  216. break;
  217. case CPDFAnnotationType.Squiggly:
  218. CPDFMarkupData squigglyData = pdfAnnotationData as CPDFMarkupData;
  219. annotHandlerEventArgs = new TextSquigglyAnnotArgs();
  220. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Color = squigglyData.Color;
  221. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Transparency = squigglyData.Opacity;
  222. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Locked = squigglyData.IsLocked;
  223. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Author = squigglyData.Author;
  224. (annotHandlerEventArgs as TextSquigglyAnnotArgs).Content = squigglyData.Note;
  225. break;
  226. case CPDFAnnotationType.Square:
  227. CPDFShapeData squareData = pdfAnnotationData as CPDFShapeData;
  228. annotHandlerEventArgs = new SquareAnnotArgs();
  229. (annotHandlerEventArgs as SquareAnnotArgs).LineColor = squareData.BorderColor;
  230. (annotHandlerEventArgs as SquareAnnotArgs).BgColor = squareData.FillColor;
  231. (annotHandlerEventArgs as SquareAnnotArgs).LineWidth = squareData.Thickness;
  232. (annotHandlerEventArgs as SquareAnnotArgs).Transparency = squareData.Opacity;
  233. (annotHandlerEventArgs as SquareAnnotArgs).LineDash = squareData.DashStyle;
  234. (annotHandlerEventArgs as SquareAnnotArgs).Author = squareData.Author;
  235. (annotHandlerEventArgs as SquareAnnotArgs).Content = squareData.Note;
  236. break;
  237. case CPDFAnnotationType.Circle:
  238. CPDFShapeData cicleData = pdfAnnotationData as CPDFShapeData;
  239. annotHandlerEventArgs = new CircleAnnotArgs();
  240. (annotHandlerEventArgs as CircleAnnotArgs).LineColor = cicleData.BorderColor;
  241. (annotHandlerEventArgs as CircleAnnotArgs).BgColor = cicleData.FillColor;
  242. (annotHandlerEventArgs as CircleAnnotArgs).LineWidth = cicleData.Thickness;
  243. (annotHandlerEventArgs as CircleAnnotArgs).Transparency = cicleData.Opacity;
  244. (annotHandlerEventArgs as CircleAnnotArgs).LineDash = cicleData.DashStyle;
  245. (annotHandlerEventArgs as CircleAnnotArgs).Author = cicleData.Author;
  246. (annotHandlerEventArgs as CircleAnnotArgs).Content = cicleData.Note;
  247. break;
  248. case CPDFAnnotationType.Arrow:
  249. case CPDFAnnotationType.Line:
  250. CPDFLineShapeData lineData = pdfAnnotationData as CPDFLineShapeData;
  251. annotHandlerEventArgs = new LineAnnotArgs();
  252. (annotHandlerEventArgs as LineAnnotArgs).LineColor = lineData.BorderColor;
  253. (annotHandlerEventArgs as LineAnnotArgs).LineWidth = lineData.Thickness;
  254. (annotHandlerEventArgs as LineAnnotArgs).Transparency = lineData.Opacity;
  255. (annotHandlerEventArgs as LineAnnotArgs).LineDash = lineData.DashStyle;
  256. (annotHandlerEventArgs as LineAnnotArgs).HeadLineType = lineData.LineType.HeadLineType;
  257. (annotHandlerEventArgs as LineAnnotArgs).TailLineType = lineData.LineType.TailLineType;
  258. (annotHandlerEventArgs as LineAnnotArgs).Author = lineData.Author;
  259. (annotHandlerEventArgs as LineAnnotArgs).Content = lineData.Note;
  260. break;
  261. case CPDFAnnotationType.Note:
  262. CPDFNoteData noteData = pdfAnnotationData as CPDFNoteData;
  263. annotHandlerEventArgs = new StickyAnnotArgs();
  264. (annotHandlerEventArgs as StickyAnnotArgs).Color = noteData.BorderColor;
  265. (annotHandlerEventArgs as StickyAnnotArgs).StickyNote = noteData.Note;
  266. (annotHandlerEventArgs as StickyAnnotArgs).Transparency = 1;
  267. (annotHandlerEventArgs as StickyAnnotArgs).Author = noteData.Author;
  268. break;
  269. case CPDFAnnotationType.Freehand:
  270. CPDFFreehandData freehandData = pdfAnnotationData as CPDFFreehandData;
  271. annotHandlerEventArgs = new FreehandAnnotArgs();
  272. (annotHandlerEventArgs as FreehandAnnotArgs).InkColor = freehandData.BorderColor;
  273. (annotHandlerEventArgs as FreehandAnnotArgs).LineWidth = freehandData.Thickness;
  274. (annotHandlerEventArgs as FreehandAnnotArgs).Transparency = freehandData.Opacity;
  275. (annotHandlerEventArgs as FreehandAnnotArgs).Content = freehandData.Note;
  276. (annotHandlerEventArgs as FreehandAnnotArgs).Author = freehandData.Author;
  277. break;
  278. case CPDFAnnotationType.FreeText:
  279. CPDFFreeTextData freeTextData = pdfAnnotationData as CPDFFreeTextData;
  280. annotHandlerEventArgs = new FreeTextAnnotArgs();
  281. (annotHandlerEventArgs as FreeTextAnnotArgs).Transparency = freeTextData.Opacity;
  282. (annotHandlerEventArgs as FreeTextAnnotArgs).FontName = freeTextData.FontFamily.ToString();
  283. (annotHandlerEventArgs as FreeTextAnnotArgs).FontColor = freeTextData.BorderColor;
  284. (annotHandlerEventArgs as FreeTextAnnotArgs).IsBold = freeTextData.IsBold;
  285. (annotHandlerEventArgs as FreeTextAnnotArgs).IsItalic = freeTextData.IsItalic;
  286. (annotHandlerEventArgs as FreeTextAnnotArgs).FontSize = freeTextData.FontSize;
  287. (annotHandlerEventArgs as FreeTextAnnotArgs).Align = freeTextData.TextAlignment;
  288. (annotHandlerEventArgs as FreeTextAnnotArgs).Content = freeTextData.Note;
  289. (annotHandlerEventArgs as FreeTextAnnotArgs).Author = freeTextData.Author;
  290. break;
  291. case CPDFAnnotationType.Stamp:
  292. annotHandlerEventArgs = new StampAnnotArgs();
  293. StampAnnotArgs stampAnnot = annotHandlerEventArgs as StampAnnotArgs;
  294. CPDFStampData stampData = pdfAnnotationData as CPDFStampData;
  295. SetStamp(ref stampAnnot, stampData);
  296. break;
  297. case CPDFAnnotationType.Signature:
  298. annotHandlerEventArgs = new StampAnnotArgs();
  299. StampAnnotArgs SignatureAnnot = annotHandlerEventArgs as StampAnnotArgs;
  300. CPDFSignatureData SignatureData = pdfAnnotationData as CPDFSignatureData;
  301. SetSignature(ref SignatureAnnot, SignatureData);
  302. break;
  303. case CPDFAnnotationType.Link:
  304. if (annotHandlerEventArgs != null)
  305. {
  306. pdfViewer.SetToolParam(annotHandlerEventArgs);
  307. }
  308. break;
  309. case CPDFAnnotationType.Unknow:
  310. pdfViewer.SetMouseMode(MouseModes.PanTool);
  311. return;
  312. default:
  313. break;
  314. }
  315. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  316. pdfViewer.SetToolParam(annotHandlerEventArgs);
  317. }
  318. public void SetSignature(ref StampAnnotArgs Args, CPDFSignatureData stamp)
  319. {
  320. switch (stamp.Type)
  321. {
  322. case SignatureType.TextType:
  323. case SignatureType.ImageType:
  324. {
  325. Args.Opacity = 1;
  326. Args.Type = StampType.IMAGE_STAMP;
  327. Args.ImagePath = stamp.SourcePath;
  328. }
  329. break;
  330. case SignatureType.Drawing:
  331. {
  332. Args.SetInkData(GetPoints(stamp.DrawingPath), stamp.inkThickness, stamp.inkColor);
  333. }
  334. break;
  335. default:
  336. break;
  337. }
  338. }
  339. private List<List<Point>> GetPoints(string Path)
  340. {
  341. StrokeCollection Strokes;
  342. List<List<Point>> RawPointList = new List<List<Point>>();
  343. using (FileStream strokeStream = File.OpenRead(Path))
  344. {
  345. Strokes = new StrokeCollection(strokeStream);
  346. }
  347. for (int kk = 0; kk < Strokes.Count; kk++)
  348. {
  349. List<Point> p = new List<Point>();
  350. RawPointList.Add(p);
  351. for (int gg = 0; gg < Strokes[kk].StylusPoints.Count; gg++)
  352. {
  353. var point = Strokes[kk].StylusPoints[gg].ToPoint();
  354. if (point.X >= 0 && point.Y >= 0)
  355. RawPointList[kk].Add(point);
  356. }
  357. }
  358. return RawPointList;
  359. }
  360. private void SetStamp(ref StampAnnotArgs Args, CPDFStampData stamp)
  361. {
  362. Args.StampText = stamp.StampText;
  363. Args.Author = stamp.Author;
  364. Args.Opacity = stamp.Opacity;
  365. if (stamp.Type == StampType.IMAGE_STAMP)
  366. {
  367. Args.ImageWidth = stamp.MaxWidth;
  368. Args.ImageHeight = stamp.MaxHeight;
  369. }
  370. else
  371. {
  372. Args.MaxWidth = stamp.MaxWidth;
  373. Args.MaxHeight = stamp.MaxHeight;
  374. }
  375. Args.StampTextDate = stamp.StampTextDate;
  376. Args.TextColor = stamp.TextColor;
  377. Args.TextSharp = stamp.TextSharp;
  378. if (!string.IsNullOrEmpty(stamp.SourcePath))
  379. {
  380. BitmapImage image = new BitmapImage(new Uri(stamp.SourcePath));
  381. Args.ImageArray = new byte[image.PixelWidth * image.PixelHeight * 4];
  382. image.CopyPixels(Args.ImageArray, image.PixelWidth * 4, 0);
  383. Args.ImageHeight = image.PixelHeight;
  384. Args.ImageWidth = image.PixelWidth;
  385. }
  386. else
  387. {
  388. Args.ImageArray = new byte[stamp.ImageSource.PixelWidth * stamp.ImageSource.PixelHeight * 4];
  389. stamp.ImageSource.CopyPixels(Args.ImageArray, stamp.ImageSource.PixelWidth * 4, 0);
  390. Args.ImageHeight = stamp.ImageSource.PixelHeight;
  391. Args.ImageWidth = stamp.ImageSource.PixelWidth;
  392. }
  393. Args.Type = stamp.Type;
  394. }
  395. public void AnnotationCancel()
  396. {
  397. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  398. ClearPanel();
  399. }
  400. private void CPDFAnnotationControl_PropertyChanged(object sender, CPDFAnnotationData e)
  401. {
  402. SetAnnotationProperty(e);
  403. try
  404. {
  405. if (pdfViewer != null && pdfViewer.MouseMode == MouseModes.AnnotCreate && pdfViewer.ToolManager.CurrentAnnotArgs is FreehandAnnotArgs)
  406. {
  407. FreehandAnnotArgs freehandArgs = pdfViewer.ToolManager.CurrentAnnotArgs as FreehandAnnotArgs;
  408. CPDFFreehandData freehandData = e as CPDFFreehandData;
  409. if(freehandData != null)
  410. {
  411. freehandArgs.InkColor = freehandData.BorderColor;
  412. freehandArgs.LineWidth = freehandData.Thickness;
  413. freehandArgs.Transparency = freehandData.Opacity;
  414. freehandArgs.Content = freehandData.Note;
  415. freehandArgs.Author = freehandData.Author;
  416. }
  417. }
  418. }
  419. catch(Exception ex)
  420. {
  421. }
  422. }
  423. /// <summary>
  424. /// 选择对应注释面板
  425. /// 若未创建则创建
  426. /// 已创建则使用已有项
  427. /// </summary>
  428. /// <param name="annotationType"></param>
  429. public void InitAnnotationPanel(CPDFAnnotationType annotationType)
  430. {
  431. switch (annotationType)
  432. {
  433. case CPDFAnnotationType.Highlight:
  434. if (pdfHighlightUI == null)
  435. {
  436. pdfHighlightUI = new CPDFMarkupUI();
  437. }
  438. annotationPanel = pdfHighlightUI;
  439. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  440. break;
  441. case CPDFAnnotationType.Underline:
  442. if (pdfUnderlineUI == null)
  443. {
  444. pdfUnderlineUI = new CPDFMarkupUI();
  445. }
  446. annotationPanel = pdfUnderlineUI;
  447. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  448. break;
  449. case CPDFAnnotationType.Strikeout:
  450. if (pdfStrikeoutUI == null)
  451. {
  452. pdfStrikeoutUI = new CPDFMarkupUI();
  453. }
  454. annotationPanel = pdfStrikeoutUI;
  455. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  456. break;
  457. case CPDFAnnotationType.Squiggly:
  458. if (pdfSquigglyUI == null)
  459. {
  460. pdfSquigglyUI = new CPDFMarkupUI();
  461. }
  462. annotationPanel = pdfSquigglyUI;
  463. (annotationPanel as CPDFMarkupUI).InitWithAnnotationType(annotationType);
  464. break;
  465. case CPDFAnnotationType.Square:
  466. if (pdfSquareUI == null)
  467. {
  468. pdfSquareUI = new CPDFShapeUI();
  469. }
  470. annotationPanel = pdfSquareUI;
  471. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  472. break;
  473. case CPDFAnnotationType.Circle:
  474. if (pdfCircleUI == null)
  475. {
  476. pdfCircleUI = new CPDFShapeUI();
  477. }
  478. annotationPanel = pdfCircleUI;
  479. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  480. break;
  481. case CPDFAnnotationType.Arrow:
  482. if (pdfArrowUI == null)
  483. {
  484. pdfArrowUI = new CPDFShapeUI();
  485. }
  486. annotationPanel = pdfArrowUI;
  487. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  488. break;
  489. case CPDFAnnotationType.Line:
  490. if (pdfLineUI == null)
  491. {
  492. pdfLineUI = new CPDFShapeUI();
  493. }
  494. annotationPanel = pdfLineUI;
  495. (annotationPanel as CPDFShapeUI).InitWithAnnotationType(annotationType);
  496. break;
  497. case CPDFAnnotationType.Freehand:
  498. if (pdfFreehandUI == null)
  499. {
  500. pdfFreehandUI = new CPDFFreehandUI();
  501. pdfFreehandUI.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  502. pdfFreehandUI.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  503. }
  504. annotationPanel = pdfFreehandUI;
  505. break;
  506. case CPDFAnnotationType.FreeText:
  507. if (pdfFreeTextUI == null)
  508. {
  509. pdfFreeTextUI = new CPDFFreeTextUI();
  510. }
  511. annotationPanel = pdfFreeTextUI;
  512. break;
  513. case CPDFAnnotationType.Note:
  514. if (pdfNoteUI == null)
  515. {
  516. pdfNoteUI = new CPDFNoteUI();
  517. }
  518. annotationPanel = pdfNoteUI;
  519. break;
  520. case CPDFAnnotationType.Stamp:
  521. if (pdfStampUI == null)
  522. {
  523. pdfStampUI = new CPDFStampUI();
  524. }
  525. annotationPanel = pdfStampUI;
  526. break;
  527. case CPDFAnnotationType.Signature:
  528. if (pdfSignatureUI == null)
  529. {
  530. pdfSignatureUI = new CPDFSignatureUI();
  531. }
  532. annotationPanel = pdfSignatureUI;
  533. break;
  534. case CPDFAnnotationType.Link:
  535. if (pdfLinkUI == null)
  536. {
  537. pdfLinkUI = new CPDFLinkUI();
  538. }
  539. LinkAnnotArgs linkAnnotArgs = new LinkAnnotArgs();
  540. if (this.pdfViewer != null && this.pdfViewer.Document != null)
  541. {
  542. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  543. pdfViewer.SetToolParam(linkAnnotArgs);
  544. pdfLinkUI.InitLinkAnnotArgs(linkAnnotArgs, pdfViewer.Document.PageCount);
  545. }
  546. annotationPanel = pdfLinkUI;
  547. break;
  548. case CPDFAnnotationType.Audio:
  549. SoundAnnotArgs soundArgs = new SoundAnnotArgs();
  550. OpenFileDialog openAudioDialog = new OpenFileDialog();
  551. openAudioDialog.Filter = "Wave Files(*.wav;*.wave;)|*.wav;*.wave|All Files(*.*;)|*.*;";
  552. if (openAudioDialog.ShowDialog() == true)
  553. {
  554. soundArgs.SoundFilePath = openAudioDialog.FileName;
  555. this.pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  556. pdfViewer.SetToolParam(soundArgs);
  557. }
  558. else
  559. {
  560. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  561. ClearAnnotationBar?.Invoke(this, EventArgs.Empty);
  562. }
  563. ClearPanel();
  564. break;
  565. default:
  566. break;
  567. }
  568. }
  569. private void PdfFreehandUI_EraseChangeHandler(object sender, double e)
  570. {
  571. if (pdfViewer != null && eraseArgs!=null)
  572. {
  573. eraseArgs.Thickness = e;
  574. }
  575. }
  576. private void PdfFreehandUI_EraseClickHandler(object sender, bool e)
  577. {
  578. if(pdfViewer!=null)
  579. {
  580. CPDFFreehandUI freehandUI = sender as CPDFFreehandUI;
  581. if (e)
  582. {
  583. annotArgs = pdfViewer.ToolManager.CurrentAnnotArgs;
  584. eraseArgs =new EraseArgs();
  585. eraseArgs.UIBorderColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  586. eraseArgs.UIFillColor = Color.FromArgb(0x1A, 0x00, 0x00, 0x00);
  587. if(freehandUI!=null)
  588. {
  589. eraseArgs.Thickness = freehandUI.GetEraseThickness();
  590. }
  591. else
  592. {
  593. eraseArgs.Thickness = 1;
  594. }
  595. disableClean = true;
  596. pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  597. pdfViewer.SetToolParam(eraseArgs);
  598. disableClean = false;
  599. EmptyMessage.Visibility = Visibility.Collapsed;
  600. }
  601. else
  602. {
  603. pdfViewer.SetMouseMode(MouseModes.AnnotCreate);
  604. FreehandAnnotArgs freehandAnnotArgs = annotArgs as FreehandAnnotArgs;
  605. if (freehandAnnotArgs==null)
  606. {
  607. freehandAnnotArgs=new FreehandAnnotArgs();
  608. freehandAnnotArgs.InkColor = Colors.Red;
  609. freehandAnnotArgs.Transparency = 1;
  610. freehandAnnotArgs.LineWidth = 1;
  611. annotArgs = freehandAnnotArgs;
  612. }
  613. if(freehandUI!=null)
  614. {
  615. freehandUI.PropertyChanged -= CPDFAnnotationControl_PropertyChanged;
  616. Dictionary<AnnotAttrib, object> attribDict = new Dictionary<AnnotAttrib, object>();
  617. attribDict[AnnotAttrib.Color] = freehandAnnotArgs.InkColor;
  618. attribDict[AnnotAttrib.Transparency] = freehandAnnotArgs.Transparency;
  619. attribDict[AnnotAttrib.Thickness] = freehandAnnotArgs.LineWidth;
  620. attribDict[AnnotAttrib.NoteText] = freehandAnnotArgs.Content;
  621. AnnotAttribEvent annotEvent= AnnotAttribEvent.GetAnnotAttribEvent(freehandAnnotArgs, attribDict);
  622. freehandUI.SetPresentAnnotAttrib(annotEvent);
  623. freehandUI.PropertyChanged += CPDFAnnotationControl_PropertyChanged;
  624. freehandUI.ClearAnnotAttribEvent();
  625. }
  626. pdfViewer.SetToolParam(freehandAnnotArgs);
  627. }
  628. }
  629. }
  630. /// <summary>
  631. /// 创建临时注释面板
  632. /// </summary>
  633. /// <param name="annotArgsType"></param>
  634. public void CreatTempAnnotationPanel(AnnotAttribEvent annotAttribEvent)
  635. {
  636. AnnotArgsType annotArgsType = annotAttribEvent.GetAnnotTypes();
  637. switch (annotArgsType)
  638. {
  639. case AnnotArgsType.AnnotHighlight:
  640. case AnnotArgsType.AnnotUnderline:
  641. case AnnotArgsType.AnnotStrikeout:
  642. case AnnotArgsType.AnnotSquiggly:
  643. tempAnnotationPanel = new CPDFMarkupUI();
  644. (tempAnnotationPanel as CPDFMarkupUI).InitWithAnnotationType(CPDFAnnotationDictionary.GetAnnotArgsTypeFromAnnotationType[annotArgsType]);
  645. (tempAnnotationPanel as CPDFMarkupUI).SetPresentAnnotAttrib(annotAttribEvent);
  646. break;
  647. case AnnotArgsType.AnnotSquare:
  648. case AnnotArgsType.AnnotCircle:
  649. case AnnotArgsType.AnnotLine:
  650. tempAnnotationPanel = new CPDFShapeUI();
  651. (tempAnnotationPanel as CPDFShapeUI).InitWithAnnotationType(CPDFAnnotationDictionary.GetAnnotArgsTypeFromAnnotationType[annotArgsType]);
  652. (tempAnnotationPanel as CPDFShapeUI).SetPresentAnnotAttrib(annotAttribEvent);
  653. break;
  654. case AnnotArgsType.AnnotFreehand:
  655. CPDFFreehandUI tempFreehandPanel= new CPDFFreehandUI();
  656. tempFreehandPanel.SetPresentAnnotAttrib(annotAttribEvent);
  657. tempFreehandPanel.EraseClickHandler += PdfFreehandUI_EraseClickHandler;
  658. tempFreehandPanel.EraseChangeHandler += PdfFreehandUI_EraseChangeHandler;
  659. tempAnnotationPanel = tempFreehandPanel;
  660. break;
  661. case AnnotArgsType.AnnotFreeText:
  662. tempAnnotationPanel = new CPDFFreeTextUI();
  663. (tempAnnotationPanel as CPDFFreeTextUI).SetPresentAnnotAttrib(annotAttribEvent);
  664. break;
  665. case AnnotArgsType.AnnotSticky:
  666. tempAnnotationPanel = new CPDFNoteUI();
  667. (tempAnnotationPanel as CPDFNoteUI).SetPresentAnnotAttrib(annotAttribEvent);
  668. break;
  669. case AnnotArgsType.AnnotStamp:
  670. tempAnnotationPanel = new CPDFTempStampUI();
  671. (tempAnnotationPanel as CPDFTempStampUI).SetPresentAnnotAttrib(annotAttribEvent);
  672. break;
  673. case AnnotArgsType.AnnotLink:
  674. tempAnnotationPanel = new CPDFLinkUI();
  675. (tempAnnotationPanel as CPDFLinkUI).SetPresentAnnotAttrib(annotAttribEvent, pdfViewer.Document.PageCount);
  676. break;
  677. case AnnotArgsType.AnnotSound:
  678. tempAnnotationPanel = null;
  679. break;
  680. default:
  681. break;
  682. }
  683. }
  684. /// <summary>
  685. /// 根据注释类型显示注释面板
  686. /// </summary>
  687. /// <param name="annotationType"></param>
  688. public void LoadAnnotationPanel(CPDFAnnotationType annotationType)
  689. {
  690. this.pdfViewer.SetMouseMode(MouseModes.PanTool);
  691. currentAnnotationType = annotationType;
  692. annotationPanel = GetAnnotationPanel();
  693. InitAnnotationPanel(annotationType);
  694. ShowCurrentAnnotPanel();
  695. }
  696. /// <summary>
  697. /// 展示正在使用的属性面板
  698. /// </summary>
  699. private void ShowCurrentAnnotPanel()
  700. {
  701. if(annotationPanel != null)
  702. {
  703. if(annotationPanel is CPDFFreehandUI)
  704. {
  705. (annotationPanel as CPDFFreehandUI)?.SetEraseCheck(false);
  706. }
  707. SetAnnotationPanel(annotationPanel);
  708. ExpandPanel();
  709. ChangeAnnotationData();
  710. EmptyMessage.Visibility = Visibility.Collapsed;
  711. }
  712. else
  713. {
  714. EmptyMessage.Visibility = Visibility.Visible;
  715. SetAnnotationPanel(null);
  716. if (pdfViewer!=null && pdfViewer.ToolManager.CurrentAnnotArgs is EraseArgs)
  717. {
  718. EmptyMessage.Visibility = Visibility.Collapsed;
  719. }
  720. }
  721. }
  722. /// <summary>
  723. /// 根据参数展开指定临时面板
  724. /// </summary>
  725. /// <param name="annotArgsType"></param>
  726. private void ShowTempAnnotPanel(AnnotAttribEvent annotAttribEvent)
  727. {
  728. if(annotAttribEvent != null)
  729. {
  730. CreatTempAnnotationPanel(annotAttribEvent);
  731. SetAnnotationPanel(tempAnnotationPanel);
  732. ExpandPanel();
  733. if (tempAnnotationPanel != null)
  734. {
  735. EmptyMessage.Visibility = Visibility.Collapsed;
  736. }
  737. else
  738. {
  739. EmptyMessage.Visibility = Visibility.Visible;
  740. }
  741. }
  742. }
  743. private void PDFViewer_AnnotActiveHandler(object sender, AnnotAttribEvent e)
  744. {
  745. SetAnnotEventData(e);
  746. }
  747. public void ClearPanel()
  748. {
  749. annotationPanel = null;
  750. SetAnnotationPanel(annotationPanel);
  751. EmptyMessage.Visibility = Visibility.Visible;
  752. }
  753. public void SetAnnotEventData(AnnotAttribEvent newData)
  754. {
  755. if (newData != null)
  756. {
  757. if (newData.IsAnnotCreateReset && isTempPanel)
  758. {
  759. ShowCurrentAnnotPanel();
  760. isTempPanel = false;
  761. }
  762. else if (!newData.IsAnnotCreateReset)
  763. {
  764. AnnotArgsType annotArgsType = newData.GetAnnotTypes();
  765. ShowTempAnnotPanel(newData);
  766. isTempPanel = true;
  767. }
  768. }
  769. else
  770. {
  771. if (pdfViewer != null && pdfViewer.MouseMode == MouseModes.AnnotCreate)
  772. {
  773. ShowCurrentAnnotPanel();
  774. isTempPanel = false;
  775. }
  776. else if (annotationPanel is CPDFStampUI && currentAnnotationType == CPDFAnnotationType.Stamp)
  777. {
  778. ShowCurrentAnnotPanel();
  779. }
  780. else if (annotationPanel is CPDFSignatureUI && currentAnnotationType == CPDFAnnotationType.Signature)
  781. {
  782. ShowCurrentAnnotPanel();
  783. }
  784. else if (disableClean == false)
  785. {
  786. ClearPanel();
  787. }
  788. }
  789. }
  790. }
  791. }