CPDFAnnotationControl.xaml.cs 40 KB

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