forked from Atalasoft/WpfAnnotations_CS
/
Window1.xaml.cs
executable file
·1046 lines (900 loc) · 43.8 KB
/
Window1.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
using Atalasoft.Annotate.Wpf;
using Atalasoft.Annotate;
using Atalasoft.Annotate.UI;
using Atalasoft.Annotate.Icons;
using System.IO;
using Atalasoft.Imaging.Codec;
using System.Reflection;
using Atalasoft.Imaging;
namespace WpfAnnotations
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
private Dictionary<string, Color> _colors = new Dictionary<string,Color>();
private WpfVisualScaleMode _printScaleMode = WpfVisualScaleMode.ScaleToFit;
private bool _mirroredTextEditing;
private bool _rotatedTextEditing;
private Atalasoft.Annotate.Pdf.PdfMarkupType _lastPdfMarkup = Atalasoft.Annotate.Pdf.PdfMarkupType.Highlight;
private Button _pdfMarkupButton;
private bool _pdfReadSupport = false;
static Window1()
{
AtalaDemos.HelperMethods.PopulateDecoders(RegisteredDecoders.Decoders);
}
public Window1()
{
InitializeComponent();
FillColorCombos();
this.AnnotationViewer.Annotations.HotSpotClicked += new EventHandler<EventArgs<WpfAnnotationUI>>(Annotations_HotSpotClicked);
this.AnnotationViewer.Annotations.SelectedAnnotationsChanged += new EventHandler(Annotations_SelectedAnnotationsChanged);
this.AnnotationViewer.ImageViewer.IsCentered = true;
this.AnnotationViewer.Annotations.Factories.Add(new WpfAnnotationUIFactory<TriangleAnnotation, TriangleData>());
PrepareToolbar();
try
{
// This requires a PDF Reader license.
Atalasoft.Imaging.Codec.Pdf.PdfDecoder decoder = new Atalasoft.Imaging.Codec.Pdf.PdfDecoder();
decoder.RenderSettings.AnnotationSettings = Atalasoft.Imaging.Codec.Pdf.AnnotationRenderSettings.None;
Atalasoft.Imaging.Codec.RegisteredDecoders.Decoders.Add(decoder);
_pdfReadSupport = true;
}
catch
{
}
}
#region Toolbar
private void PrepareToolbar()
{
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Callout", AnnotateIcon.Callout));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Ellipse", AnnotateIcon.Ellipse));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Embedded Image", AnnotateIcon.EmbeddedImage));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Freehand", AnnotateIcon.Freehand));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Hot Spot", AnnotateIcon.RectangleHotspot));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Hot Spot Freehand", AnnotateIcon.FreehandHotspot));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Line", AnnotateIcon.Line));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Lines", AnnotateIcon.Lines));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Polygon", AnnotateIcon.Polygon));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Rectangle", AnnotateIcon.Rectangle));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Referenced Image", AnnotateIcon.ReferencedImage));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Rubber Stamp", AnnotateIcon.RubberStamp));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("Text", AnnotateIcon.Text));
this.AnnotationToolbar.Items.Add(CreateToolbarButton("PDF Line", AnnotateIcon.PdfLine));
_pdfMarkupButton = CreateToolbarButton("PDF Markup (Highlight)", AnnotateIcon.PdfMarkup);
this.AnnotationToolbar.Items.Add(_pdfMarkupButton);
}
private Button CreateToolbarButton(string tooltip, AnnotateIcon icon)
{
Image img = new Image();
img.Source = ExtractAnnotationIcon(icon, AnnotateIconSize.Size24);
Button btn = new Button();
btn.Content = img;
btn.ToolTip = tooltip;
btn.Click += new RoutedEventHandler(AnnotationToolbar_Click);
return btn;
}
void AnnotationToolbar_Click(object sender, RoutedEventArgs e)
{
Button btn = sender as Button;
if (btn == null) return;
switch ((string)btn.ToolTip)
{
case "Callout":
AddCallout();
break;
case "Ellipse":
AddEllipse();
break;
case "Embedded Image":
AddEmbeddedImage();
break;
case "Freehand":
AddFreehand();
break;
case "Hot Spot":
AddHotSpot();
break;
case "Hot Spot Freehand":
AddHotSpotFreehand();
break;
case "Line":
AddLine();
break;
case "Lines":
AddLines();
break;
case "Polygon":
AddPolygon();
break;
case "Rectangle":
AddRectangle();
break;
case "Referenced Image":
AddReferencedImage();
break;
case "Rubber Stamp":
AddRubberStamp();
break;
case "Text":
AddText();
break;
case "PDF Line":
AddPdfLine();
break;
case "PDF Markup (Highlight)":
case "PDF Markup (StrikeOut)":
case "PDF Markup (Underline)":
case "PDF Markup (Squiggly)":
AddPdfMarkup(_lastPdfMarkup);
break;
}
}
private BitmapSource ExtractAnnotationIcon(AnnotateIcon icon, AnnotateIconSize size)
{
BitmapSource returnSource = WpfObjectConverter.ConvertBitmap((System.Drawing.Bitmap)IconResource.ExtractAnnotationIcon(icon, size));
if (returnSource == null)
{
Assembly assm = Assembly.LoadFrom(@"Atalasoft.dotImage.dll");
if (assm != null)
{
System.IO.Stream stream = assm.GetManifestResourceStream("Atalasoft.Imaging.Annotate.Icons._" + size.ToString().Substring(4) + "." + icon.ToString() + ".png");
returnSource = WpfObjectConverter.ConvertBitmap((System.Drawing.Bitmap)System.Drawing.Image.FromStream(stream));
}
// if it's STILL null, then give up and make placeholders
if (returnSource == null)
{
switch(size.ToString())
{
case "size16":
returnSource = WpfObjectConverter.ConvertBitmap(new AtalaImage(16, 16, Atalasoft.Imaging.PixelFormat.Pixel24bppBgr, System.Drawing.Color.White).ToBitmap());
break;
case "size24":
returnSource = WpfObjectConverter.ConvertBitmap(new AtalaImage(24, 24, Atalasoft.Imaging.PixelFormat.Pixel24bppBgr, System.Drawing.Color.White).ToBitmap());
break;
case "size32":
returnSource = WpfObjectConverter.ConvertBitmap(new AtalaImage(32, 32, Atalasoft.Imaging.PixelFormat.Pixel24bppBgr, System.Drawing.Color.White).ToBitmap());
break;
}
}
}
return returnSource;
}
#endregion
#region Color Combobox
private void FillColorCombos()
{
_colors.Add("AliceBlue", Colors.AliceBlue);
_colors.Add("Black", Colors.Black);
_colors.Add("Blue", Colors.Blue);
_colors.Add("DarkBlue", Colors.DarkBlue);
_colors.Add("Green", Colors.Green);
_colors.Add("Lime", Colors.Lime);
_colors.Add("Maroon", Colors.Maroon);
_colors.Add("Orange", Colors.Orange);
_colors.Add("Red", Colors.Red);
_colors.Add("Transparent", Colors.Transparent);
_colors.Add("White", Colors.White);
_colors.Add("Yellow", Colors.Yellow);
this.FillComboBox.ItemsSource = _colors;
this.OutlineComboBox.ItemsSource = _colors;
}
private void SelectAddComboBoxItem(ComboBox comboBox, Color color)
{
if (!_colors.ContainsValue(color))
_colors.Add(color.ToString(), color);
int count = comboBox.Items.Count;
for (int i = 0; i < count; i++)
{
KeyValuePair<string, Color> item = (KeyValuePair<string, Color>)comboBox.Items[i];
if (item.Value == color)
{
comboBox.SelectedIndex = i;
return;
}
}
}
#endregion
#region Annotation Events
void Annotations_SelectedAnnotationsChanged(object sender, EventArgs e)
{
this.statusInfo.Content = "Selection Changed: {Count=" + this.AnnotationViewer.Annotations.SelectedAnnotations.Count.ToString() + "}";
// Update the property fields.
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
bool enabled = (ann != null);
this.textLocation.IsEnabled = enabled;
this.textSize.IsEnabled = enabled;
this.FillComboBox.IsEnabled = enabled;
this.OutlineComboBox.IsEnabled = enabled;
this.PenSizeSlider.IsEnabled = enabled;
this.ShadowCheckBox.IsEnabled = enabled;
this.OffsetSlider.IsEnabled = enabled;
if (enabled)
{
this.textLocation.Text = ann.Location.X.ToString() + ", " + ann.Location.Y.ToString();
this.textSize.Text = ann.Size.Width.ToString() + ", " + ann.Size.Height.ToString();
AnnotationBrush b = (AnnotationBrush)ann.GetValue(WpfAnnotationUI.FillProperty);
if (b != null) SelectAddComboBoxItem(this.FillComboBox, WpfObjectConverter.ConvertColor(b.Color));
AnnotationPen p = (AnnotationPen)ann.GetValue(WpfAnnotationUI.OutlineProperty);
if (p != null)
{
SelectAddComboBoxItem(this.OutlineComboBox, WpfObjectConverter.ConvertColor(p.Color));
this.PenSizeSlider.Value = p.Width;
}
b = (AnnotationBrush)ann.GetValue(WpfAnnotationUI.ShadowProperty);
this.ShadowCheckBox.IsChecked = (b != null);
if (b != null)
{
Point pt = (Point)ann.GetValue(WpfAnnotationUI.ShadowOffsetProperty);
this.OffsetSlider.Value = pt.X;
}
}
}
void Annotations_HotSpotClicked(object sender, EventArgs<WpfAnnotationUI> e)
{
this.statusInfo.Content = "Hot Spot clicked!";
}
#endregion
#region File Menu
private void FileCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
RoutedCommand rc = e.Command as RoutedCommand;
if (rc == null) return;
switch (rc.Name)
{
case "Open":
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = AtalaDemos.HelperMethods.CreateDialogFilter(true);
if (dlg.ShowDialog().Value)
this.AnnotationViewer.Open(dlg.FileName, 0, null);
break;
case "SaveAs":
SaveFileDialog save = new SaveFileDialog();
save.Filter = "JPEG (*.jpg)|*.jpg|TIFF (*.tif)|*.tif|PDF (*.pdf)|*.pdf";
if (save.ShowDialog().Value)
{
if (save.FilterIndex == 1)
SaveImage(save.FileName, new Atalasoft.Imaging.Codec.JpegEncoder());
else if (save.FilterIndex == 2)
SaveImage(save.FileName, new Atalasoft.Imaging.Codec.TiffEncoder());
else
{
if (Atalasoft.Imaging.AtalaImage.Edition == Atalasoft.Imaging.LicenseEdition.Document)
{
SaveImage(save.FileName, new Atalasoft.Imaging.Codec.Pdf.PdfEncoder());
SavePdfAnnotations(save.FileName);
}
else
MessageBox.Show("A 'DotImage Document Imaging' license is required to save as PDF.", "License Required", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
break;
case "Print":
PrintDialog pdlg = new PrintDialog();
if (pdlg.ShowDialog().Value)
{
System.Printing.PageMediaSize sz = pdlg.PrintTicket.PageMediaSize;
pdlg.PrintVisual(this.AnnotationViewer.CreateVisual(new Size(sz.Width.HasValue ? sz.Width.Value : 0, sz.Height.HasValue ? sz.Height.Value : 0), _printScaleMode, new Thickness(10)), "WPF Annotation Printing");
}
break;
}
}
private void FileCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
RoutedCommand rc = e.Command as RoutedCommand;
if (rc == null) return;
switch (rc.Name)
{
case "Open":
e.CanExecute = true;
break;
case "SaveAs":
e.CanExecute = (this.AnnotationViewer.ImageViewer.Image != null);
break;
case "Print":
// You can print the annotations without an image.
e.CanExecute = (this.AnnotationViewer.ImageViewer.Image != null || this.AnnotationViewer.Annotations.CountAnnotations() > 0);
break;
}
}
private void SaveImage(string fileName, Atalasoft.Imaging.Codec.ImageEncoder encoder)
{
this.AnnotationViewer.Save(fileName, encoder, null);
}
private void SavePdfAnnotations(string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
Atalasoft.Annotate.Exporters.PdfAnnotationDataExporter exp = new Atalasoft.Annotate.Exporters.PdfAnnotationDataExporter();
System.Drawing.Size sz = this.AnnotationViewer.ImageViewer.Image.Size;
System.Drawing.SizeF pageSize = new System.Drawing.SizeF(sz.Width, sz.Height);
Atalasoft.Imaging.Dpi resolution = this.AnnotationViewer.ImageViewer.Image.Resolution;
Atalasoft.Annotate.LayerData data = (Atalasoft.Annotate.LayerData)this.AnnotationViewer.Annotations.CurrentLayer.CreateDataSnapshot();
exp.ExportOver(fs, pageSize, AnnotationUnit.Pixel, resolution, data, 0);
}
}
private void AutoLoadXmp_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.AutoLoadXmp = ((MenuItem)sender).IsChecked;
}
private void AutoLoadWang_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.AutoLoadWang = ((MenuItem)sender).IsChecked;
}
private void AutoSaveXmp_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.AutoSaveXmp = ((MenuItem)sender).IsChecked;
}
private void AutoSaveWang_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.AutoSaveWang = ((MenuItem)sender).IsChecked;
}
private void Burn_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Burn();
this.AnnotationViewer.Annotations.Layers.Clear();
}
private void PrintScaleMode_Click(object sender, RoutedEventArgs e)
{
this.PrintScaleModeFill.IsChecked = false;
this.PrintScaleModeFit.IsChecked = false;
this.PrintScaleModeNone.IsChecked = false;
this.PrintScaleModeStretch.IsChecked = false;
MenuItem item = sender as MenuItem;
item.IsChecked = true;
string header = (string)item.Header;
switch (header)
{
case "None":
_printScaleMode = WpfVisualScaleMode.None;
break;
case "Scale To Fit":
_printScaleMode = WpfVisualScaleMode.ScaleToFit;
break;
case "Scale To Fill":
_printScaleMode = WpfVisualScaleMode.ScaleToFill;
break;
case "Stretch To Fill":
_printScaleMode = WpfVisualScaleMode.StretchToFill;
break;
}
}
private void Exit_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
#endregion
#region Edit Menu
private void EditCmdExecuted(object sender, ExecutedRoutedEventArgs e)
{
RoutedCommand rc = e.Command as RoutedCommand;
if (rc == null) return;
switch (rc.Name)
{
case "Cut":
this.AnnotationViewer.Annotations.Cut();
break;
case "Copy":
this.AnnotationViewer.Annotations.Copy();
break;
case "Paste":
this.AnnotationViewer.Annotations.Paste();
break;
}
}
private void EditCmdCanExecute(object sender, CanExecuteRoutedEventArgs e)
{
RoutedCommand rc = e.Command as RoutedCommand;
if (rc == null) return;
switch (rc.Name)
{
case "Cut":
case "Copy":
e.CanExecute = (this.AnnotationViewer.Annotations.SelectedAnnotations.Count > 0);
break;
case "Paste":
e.CanExecute = this.AnnotationViewer.Annotations.CanPaste;
break;
}
}
private void RotateDocument_Click(object sender, RoutedEventArgs e)
{
switch ((string)((MenuItem)sender).Header)
{
case "90":
this.AnnotationViewer.RotateDocument(DocumentRotation.Rotate90);
break;
case "180":
this.AnnotationViewer.RotateDocument(DocumentRotation.Rotate180);
break;
case "270":
this.AnnotationViewer.RotateDocument(DocumentRotation.Rotate270);
break;
}
}
#endregion
#region Viewer Menu
private void CenterImage_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.ImageViewer.IsCentered = ((MenuItem)sender).IsChecked;
}
private void SnapRotation_Click(object sender, RoutedEventArgs e)
{
// Make it easy for end-users to hit a 45 degree angle when rotating.
// Setting the RotationSnapInterval to 0 will disable this feature.
this.AnnotationViewer.Annotations.RotationSnapInterval = (((MenuItem)sender).IsChecked ? 45 : 0);
this.AnnotationViewer.Annotations.RotationSnapThreshold = 6;
}
private void Zoom_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (this.zoomCombo.Text.Length == 0) return;
ComboBoxItem item = (ComboBoxItem)e.AddedItems[0];
double val = Int32.Parse(((string)item.Content).Replace("%", "")) / 100.0;
this.AnnotationViewer.ImageViewer.Zoom = val;
}
#endregion
#region Annotations Menu
private void LoadAnnotationData_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "XMP Data (*.xml)|*.xml|WANG Data (*.wng)|*.wng";
if (dlg.ShowDialog().Value)
{
if (dlg.FilterIndex == 1)
this.AnnotationViewer.Annotations.Load(dlg.FileName, new Atalasoft.Annotate.Formatters.XmpFormatter());
else
this.AnnotationViewer.Annotations.Load(dlg.FileName, new Atalasoft.Annotate.Formatters.WangFormatter());
}
}
private void SaveAnnotationData_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "XMP Data (*.xml)|*.xml|WANG Data (*.wng)|*.wng";
if (dlg.ShowDialog().Value)
{
if (dlg.FilterIndex == 1)
this.AnnotationViewer.Annotations.Save(dlg.FileName, new Atalasoft.Annotate.Formatters.XmpFormatter());
else
this.AnnotationViewer.Annotations.Save(dlg.FileName, new Atalasoft.Annotate.Formatters.WangFormatter());
}
}
#region Add Annotations
private void AddAnnotation_Click(object sender, RoutedEventArgs e)
{
MenuItem item = sender as MenuItem;
if (item == null) return;
switch ((string)item.Header)
{
case "Callout":
AddCallout();
break;
case "Ellipse":
AddEllipse();
break;
case "Embedded Image":
AddEmbeddedImage();
break;
case "Freehand":
AddFreehand();
break;
case "Hot Spot":
AddHotSpot();
break;
case "Hot Spot Freehand":
AddHotSpotFreehand();
break;
case "Line":
AddLine();
break;
case "Lines":
AddLines();
break;
case "Polygon":
AddPolygon();
break;
case "Rectangle":
AddRectangle();
break;
case "Referenced Image":
AddReferencedImage();
break;
case "Rubber Stamp":
AddRubberStamp();
break;
case "Text":
AddText();
break;
case "PDF Line":
AddPdfLine();
break;
case "PDF Highlight":
AddPdfMarkup(Atalasoft.Annotate.Pdf.PdfMarkupType.Highlight);
break;
case "PDF StrikeOut":
AddPdfMarkup(Atalasoft.Annotate.Pdf.PdfMarkupType.StrikeOut);
break;
case "PDF Underline":
AddPdfMarkup(Atalasoft.Annotate.Pdf.PdfMarkupType.Underline);
break;
case "PDF Squiggly":
AddPdfMarkup(Atalasoft.Annotate.Pdf.PdfMarkupType.Squiggly);
break;
case "Triangle (Custom)":
this.AnnotationViewer.Annotations.CreateAnnotation(new TriangleAnnotation(null, new AnnotationBrush(System.Drawing.Color.Orange), new AnnotationPen(System.Drawing.Color.Gold, 1)));
break;
}
}
private void AddPdfMarkup(Atalasoft.Annotate.Pdf.PdfMarkupType markupType)
{
_lastPdfMarkup = markupType;
_pdfMarkupButton.ToolTip = "PDF Markup (" + markupType.ToString() + ")";
if (markupType == Atalasoft.Annotate.Pdf.PdfMarkupType.Highlight)
this.AnnotationViewer.Annotations.CreateAnnotation(new Atalasoft.Annotate.Wpf.Pdf.WpfPdfMarkupAnnotation(null, markupType, new AnnotationBrush(System.Drawing.Color.FromArgb(120, System.Drawing.Color.Yellow)), null, "", System.Environment.UserName, DateTime.Now));
else
this.AnnotationViewer.Annotations.CreateAnnotation(new Atalasoft.Annotate.Wpf.Pdf.WpfPdfMarkupAnnotation(null, markupType, null, new AnnotationPen(System.Drawing.Color.Black, 1), "", System.Environment.UserName, DateTime.Now));
}
private void AddPdfLine()
{
this.AnnotationViewer.Annotations.CreateAnnotation(new Atalasoft.Annotate.Wpf.Pdf.WpfPdfLineAnnotation(new Point(), new Point(), true, null, new AnnotationPen(System.Drawing.Color.Black), "PDF Line", System.Environment.UserName, DateTime.Now));
}
private void AddCallout()
{
WpfCalloutAnnotation ann = new WpfCalloutAnnotation("Callout", new AnnotationFont("Verdana", (float)(10)), new AnnotationBrush(System.Drawing.Color.White), new AnnotationPen(System.Drawing.Color.Black, 1), 20, new AnnotationBrush(System.Drawing.Color.Navy), new AnnotationPen(System.Drawing.Color.Black, 1));
ann.CanEditMirrored = _mirroredTextEditing;
ann.CanEditRotated = _rotatedTextEditing;
ann.Leader.EndCap = new AnnotationLineCap(AnnotationLineCapStyle.FilledArrow, new System.Drawing.SizeF(12, 12));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddEllipse()
{
WpfEllipseAnnotation ann = new WpfEllipseAnnotation(new Rect(0, 0, 0, 0), new Atalasoft.Annotate.AnnotationBrush(System.Drawing.Color.Green), new Atalasoft.Annotate.AnnotationPen(System.Drawing.Color.Gold, 1));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddEmbeddedImage()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images|*.jpg;*.png;*.tif";
if (dlg.ShowDialog().Value)
{
AnnotationImage image = new AnnotationImage(dlg.FileName);
WpfEmbeddedImageAnnotation ann = new WpfEmbeddedImageAnnotation(image, new Point(0, 0), null, new Point(0, 0));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
}
private void AddFreehand()
{
WpfFreehandAnnotation ann = new WpfFreehandAnnotation(new AnnotationPen(System.Drawing.Color.Red, 1), WpfFreehandLineType.Bezier, false);
ann.GripMode = AnnotationGripMode.Rectangular;
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddHotSpot()
{
this.AnnotationViewer.Annotations.CreateAnnotation(new WpfHotSpotAnnotation(new AnnotationBrush(System.Drawing.Color.Firebrick), null));
}
private void AddHotSpotFreehand()
{
this.AnnotationViewer.Annotations.CreateAnnotation(new WpfHotSpotFreehandAnnotation(new AnnotationBrush(System.Drawing.Color.Firebrick), WpfFreehandLineType.Bezier));
}
private void AddLine()
{
WpfLineAnnotation line = new WpfLineAnnotation(new Point(0, 0), new Point(0, 0), new AnnotationPen(System.Drawing.Color.Red, 2));
line.Outline.StartCap = new AnnotationLineCap(AnnotationLineCapStyle.Ellipse, new System.Drawing.SizeF(10, 10));
line.Outline.EndCap = new AnnotationLineCap(AnnotationLineCapStyle.Ellipse, new System.Drawing.SizeF(10, 10));
this.AnnotationViewer.Annotations.CreateAnnotation(line);
}
private void AddLines()
{
WpfLinesAnnotation ann = new WpfLinesAnnotation(null, new AnnotationPen(System.Drawing.Color.Red, 2));
ann.Outline.StartCap = new AnnotationLineCap(AnnotationLineCapStyle.ReversedArrow, new System.Drawing.SizeF(10, 10));
ann.Outline.EndCap = new AnnotationLineCap(AnnotationLineCapStyle.FilledArrow, new System.Drawing.SizeF(10, 10));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddPolygon()
{
WpfPolygonAnnotation ann = new WpfPolygonAnnotation(null, new AnnotationBrush(System.Drawing.Color.Orange), new AnnotationPen(System.Drawing.Color.Black, 1), null, new Point(0, 0));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddRectangle()
{
WpfRectangleAnnotation ann = new WpfRectangleAnnotation(new Rect(0, 0, 0, 0), new Atalasoft.Annotate.AnnotationBrush(System.Drawing.Color.Red), new Atalasoft.Annotate.AnnotationPen(System.Drawing.Color.Maroon, 1));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
private void AddReferencedImage()
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Filter = "Images|*.jpg;*.png;*.tif";
if (dlg.ShowDialog().Value)
{
WpfReferencedImageAnnotation ann = new WpfReferencedImageAnnotation(dlg.FileName, new Point(0, 0), null, new Point(0, 0));
this.AnnotationViewer.Annotations.CreateAnnotation(ann);
}
}
private void AddRubberStamp()
{
this.AnnotationViewer.Annotations.CreateAnnotation(new WpfRubberStampAnnotation("TOP SECRET", new AnnotationFont("Verdana", 24 * 1), new AnnotationBrush(System.Drawing.Color.Firebrick), null, new AnnotationPen(System.Drawing.Color.Firebrick, 10 * 1), 30, 4));
}
private void AddText()
{
WpfTextAnnotation txt = new WpfTextAnnotation("Testing", new Atalasoft.Annotate.AnnotationFont("Verdana", 12), new Atalasoft.Annotate.AnnotationBrush(System.Drawing.Color.Red), new Atalasoft.Annotate.AnnotationBrush(System.Drawing.Color.White), new Atalasoft.Annotate.AnnotationPen(System.Drawing.Color.Black, 1));
txt.Shadow = new Atalasoft.Annotate.AnnotationBrush(System.Drawing.Color.FromArgb(120, System.Drawing.Color.Silver));
txt.ShadowOffset = new Point(6, 6);
txt.CanEditMirrored = _mirroredTextEditing;
txt.CanEditRotated = _rotatedTextEditing;
this.AnnotationViewer.Annotations.CreateAnnotation(txt);
}
#endregion
private void Clear_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Annotations.Layers.Clear();
}
private void ClipToDocument_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Annotations.ClipToDocument = ((MenuItem)sender).IsChecked;
}
private void FlipHorizontal_Click(object sender, RoutedEventArgs e)
{
FlipAnnotation(true, false);
}
private void FlipHorizontalPivot_Click(object sender, RoutedEventArgs e)
{
FlipAnnotation(true, true);
}
private void FlipVertical_Click(object sender, RoutedEventArgs e)
{
FlipAnnotation(false, false);
}
private void FlipVerticalPivot_Click(object sender, RoutedEventArgs e)
{
FlipAnnotation(false, true);
}
private void FlipAnnotation(bool horizontal, bool pivot)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
ann.Mirror((horizontal ? MirrorDirection.Horizontal : MirrorDirection.Vertical), !pivot);
}
private void GripMode_Click(object sender, RoutedEventArgs e)
{
WpfPointBaseAnnotation ann = this.AnnotationViewer.Annotations.ActiveAnnotation as WpfPointBaseAnnotation;
if (ann != null)
{
MenuItem item = sender as MenuItem;
if ((string)item.Header == "Rectangular")
ann.GripMode = AnnotationGripMode.Rectangular;
else
ann.GripMode = AnnotationGripMode.Points;
}
}
private void InteractMode_Click(object sender, RoutedEventArgs e)
{
MenuItem item = sender as MenuItem;
switch ((string)item.Header)
{
case "None":
this.AnnotationViewer.Annotations.InteractMode = AnnotateInteractMode.None;
break;
case "Author":
this.AnnotationViewer.Annotations.InteractMode = AnnotateInteractMode.Author;
break;
case "View":
this.AnnotationViewer.Annotations.InteractMode = AnnotateInteractMode.View;
break;
}
}
private void Group_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Annotations.Group();
}
private void Ungroup_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Annotations.Ungroup();
}
private void RemoveSelected_Click(object sender, RoutedEventArgs e)
{
if (this.AnnotationViewer.Annotations.ActiveAnnotation != null)
this.AnnotationViewer.Annotations.ActiveAnnotation.Remove();
}
private void SelectAll_Click(object sender, RoutedEventArgs e)
{
this.AnnotationViewer.Annotations.SelectAll(true);
}
private void MirroredTextEditing_Click(object sender, RoutedEventArgs e)
{
_mirroredTextEditing = ((MenuItem)sender).IsChecked;
UpdateTextAnnotationEditingOptions();
}
private void RotatedTextEditing_Click(object sender, RoutedEventArgs e)
{
_rotatedTextEditing = ((MenuItem)sender).IsChecked;
UpdateTextAnnotationEditingOptions();
}
private void UpdateTextAnnotationEditingOptions()
{
foreach (WpfLayerAnnotation layer in this.AnnotationViewer.Annotations.Layers)
{
foreach (WpfAnnotationUI ann in layer.Items)
{
WpfTextAnnotation txt = ann as WpfTextAnnotation;
if (txt != null)
{
txt.CanEditMirrored = _mirroredTextEditing;
txt.CanEditRotated = _rotatedTextEditing;
if (txt.EditMode)
{
txt.EditMode = false;
txt.EditMode = true;
}
continue;
}
WpfCalloutAnnotation call = ann as WpfCalloutAnnotation;
if (call != null)
{
call.CanEditMirrored = _mirroredTextEditing;
call.CanEditRotated = _rotatedTextEditing;
if (call.EditMode)
{
call.EditMode = false;
call.EditMode = true;
}
}
}
}
}
#endregion
#region Annotation Property Box
private void ProperyExpander_Expanded(object sender, RoutedEventArgs e)
{
this.ProperyExpander.Width = 185;
// Add clipping so the grips will not render over the expander.
this.AnnotationViewer.Annotations.GripClip = new RectangleGeometry(new Rect(160, 0, this.AnnotationViewer.Width - 160, this.AnnotationViewer.Height));
}
private void ProperyExpander_Collapsed(object sender, RoutedEventArgs e)
{
this.ProperyExpander.Width = 24;
this.AnnotationViewer.Annotations.GripClip = new RectangleGeometry(new Rect(0, 0, this.AnnotationViewer.Width, this.AnnotationViewer.Height));
}
private void textLocation_TextChanged(object sender, TextChangedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
string txt = this.textLocation.Text;
if (txt.Length == 0) return;
string[] items = txt.Split(',');
if (items.Length != 2) return;
double x = 0;
double y = 0;
if (double.TryParse(items[0].Trim(), out x) && double.TryParse(items[1].Trim(), out y))
ann.Location = new Point(x, y);
}
private void textSize_TextChanged(object sender, TextChangedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
string txt = this.textSize.Text;
if (txt.Length == 0) return;
string[] items = txt.Split(',');
if (items.Length != 2) return;
double width = 0;
double height = 0;
if (double.TryParse(items[0].Trim(), out width) && double.TryParse(items[1].Trim(), out height))
ann.Size = new Size(width, height);
}
private void PenSizeSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
if (this.PenSizeSlider.Value == 0)
ann.SetValue(WpfAnnotationUI.OutlineProperty, null);
else
{
AnnotationPen pen = (AnnotationPen)ann.GetValue(WpfAnnotationUI.OutlineProperty);
if (pen == null)
{
Color clr = Colors.Black;
if (this.OutlineComboBox.SelectedIndex > -1)
clr = ((KeyValuePair<string, Color>)this.OutlineComboBox.SelectedItem).Value;
pen = new AnnotationPen(WpfObjectConverter.ConvertColor(clr), (float)this.PenSizeSlider.Value);
ann.SetValue(WpfAnnotationUI.OutlineProperty, pen);
}
else
pen.Width = (float)this.PenSizeSlider.Value;
}
}
private void OutlineComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
KeyValuePair<string, Color> val = (KeyValuePair<string, Color>)this.OutlineComboBox.SelectedItem;
AnnotationPen pen = (AnnotationPen)ann.GetValue(WpfAnnotationUI.OutlineProperty);
if (pen == null)
{
if (this.PenSizeSlider.Value > 0)
{
pen = new AnnotationPen(WpfObjectConverter.ConvertColor(val.Value), (float)this.PenSizeSlider.Value);
ann.SetValue(WpfAnnotationUI.OutlineProperty, pen);
}
}
else
pen.Color = WpfObjectConverter.ConvertColor(val.Value);
}
private void FillComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann == null) return;
KeyValuePair<string, Color> val = (KeyValuePair<string, Color>)this.FillComboBox.SelectedItem;
AnnotationBrush brush = (AnnotationBrush)ann.GetValue(WpfAnnotationUI.FillProperty);
if (brush == null)
{
brush = new AnnotationBrush(WpfObjectConverter.ConvertColor(val.Value));
ann.SetValue(WpfAnnotationUI.FillProperty, brush);
}
else
brush.Color = WpfObjectConverter.ConvertColor(val.Value);
}
private void OffsetSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann != null)
ann.SetValue(WpfAnnotationUI.ShadowOffsetProperty, new Point(e.NewValue, e.NewValue));
}
private void ShadowCheckBox_Checked(object sender, RoutedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann != null)
ann.SetValue(WpfAnnotationUI.ShadowProperty, new AnnotationBrush(System.Drawing.Color.FromArgb(120, System.Drawing.Color.Silver)));
}
private void ShadowCheckBox_Unchecked(object sender, RoutedEventArgs e)
{
WpfAnnotationUI ann = this.AnnotationViewer.Annotations.ActiveAnnotation;
if (ann != null)
ann.SetValue(WpfAnnotationUI.ShadowProperty, null);
}