Skip to content

Commit 4925ebe

Browse files
authored
Merge pull request #30 from SyncfusionExamples/my-dev
Added sample to list the Annotations present in the PdfViewer
2 parents fcaa407 + 28342d4 commit 4925ebe

15 files changed

+748
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="Syncfusion.Licensing" publicKeyToken="632609b4d040f6b4" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-20.3460.0.47" newVersion="20.3460.0.47" />
11+
</dependentAssembly>
12+
<dependentAssembly>
13+
<assemblyIdentity name="Syncfusion.Compression.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral" />
14+
<bindingRedirect oldVersion="0.0.0.0-100.2460.1.0" newVersion="100.2460.1.0" />
15+
</dependentAssembly>
16+
</assemblyBinding>
17+
</runtime>
18+
</configuration>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Application x:Class="WPF_PDFViewer_Annotations.App"
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:local="clr-namespace:WPF_PDFViewer_Annotations"
5+
StartupUri="MainWindow.xaml">
6+
<Application.Resources>
7+
8+
</Application.Resources>
9+
</Application>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Configuration;
4+
using System.Data;
5+
using System.Linq;
6+
using System.Threading.Tasks;
7+
using System.Windows;
8+
9+
namespace WPF_PDFViewer_Annotations
10+
{
11+
/// <summary>
12+
/// Interaction logic for App.xaml
13+
/// </summary>
14+
public partial class App : Application
15+
{
16+
}
17+
}
Loading
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Window
2+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
5+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
6+
xmlns:local="clr-namespace:WPF_PDFViewer_Annotations"
7+
xmlns:PdfViewer="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF" xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Class="WPF_PDFViewer_Annotations.MainWindow"
8+
mc:Ignorable="d"
9+
Title="MainWindow" Height="450" Width="800" WindowState="Maximized">
10+
<Grid>
11+
<Grid.ColumnDefinitions>
12+
<ColumnDefinition Width="*"/>
13+
<ColumnDefinition Width="250"/>
14+
</Grid.ColumnDefinitions>
15+
<PdfViewer:PdfViewerControl x:Name="pdfViewer" ZoomMode="FitWidth"/>
16+
<TreeView x:Name="treeView" Grid.Column="1" SelectedItemChanged="treeView_SelectedItemChanged"/>
17+
</Grid>
18+
</Window>
Lines changed: 242 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,242 @@
1+
using Syncfusion.Pdf.Interactive;
2+
using Syncfusion.Pdf.Parsing;
3+
using Syncfusion.SfSkinManager;
4+
using Syncfusion.Windows.PdfViewer;
5+
using System;
6+
using System.Collections.Generic;
7+
using System.Linq;
8+
using System.Text;
9+
using System.Threading.Tasks;
10+
using System.Windows;
11+
using System.Windows.Controls;
12+
using System.Windows.Data;
13+
using System.Windows.Documents;
14+
using System.Windows.Input;
15+
using System.Windows.Media;
16+
using System.Windows.Media.Imaging;
17+
using System.Windows.Navigation;
18+
using System.Windows.Shapes;
19+
20+
namespace WPF_PDFViewer_Annotations
21+
{
22+
/// <summary>
23+
/// Interaction logic for MainWindow.xaml
24+
/// </summary>
25+
public partial class MainWindow : Window
26+
{
27+
public MainWindow()
28+
{
29+
SfSkinManager.SetTheme(this, new Theme("FluentLight"));
30+
InitializeComponent();
31+
pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
32+
pdfViewer.FreeTextAnnotationChanged += PdfViewer_FreeTextAnnotationChanged;
33+
pdfViewer.StickyNoteAnnotationChanged += PdfViewer_StickyNoteAnnotationChanged;
34+
pdfViewer.InkAnnotationChanged += PdfViewer_InkAnnotationChanged;
35+
pdfViewer.TextMarkupAnnotationChanged += PdfViewer_TextMarkupAnnotationChanged;
36+
pdfViewer.ShapeAnnotationChanged += PdfViewer_ShapeAnnotationChanged;
37+
pdfViewer.StampAnnotationChanged += PdfViewer_StampAnnotationChanged;
38+
pdfViewer.Load("../../Data/Sample Document.pdf");
39+
treeView.Background = new SolidColorBrush(Color.FromRgb(235,235,238));
40+
}
41+
42+
private void PdfViewer_StampAnnotationChanged(object sender, StampAnnotationChangedEventArgs e)
43+
{
44+
PerformAddorRemoveAnnotations("RubberStampAnnotation", e.Name, e.Action, e.PageNumber);
45+
}
46+
47+
private void PdfViewer_ShapeAnnotationChanged(object sender, ShapeAnnotationChangedEventArgs e)
48+
{
49+
if(e.Type == ShapeAnnotationType.Arrow || e.Type == ShapeAnnotationType.Line)
50+
PerformAddorRemoveAnnotations("LineAnnotation", e.Name, e.Action, e.PageNumber);
51+
else if(e.Type == ShapeAnnotationType.Circle)
52+
PerformAddorRemoveAnnotations("EllipseAnnotation", e.Name, e.Action, e.PageNumber);
53+
else if (e.Type == ShapeAnnotationType.Polygon)
54+
PerformAddorRemoveAnnotations("PolygonAnnotation", e.Name, e.Action, e.PageNumber);
55+
else if (e.Type == ShapeAnnotationType.Rectangle)
56+
PerformAddorRemoveAnnotations("SquareAnnotation", e.Name, e.Action, e.PageNumber);
57+
else if (e.Type == ShapeAnnotationType.Polyline)
58+
PerformAddorRemoveAnnotations("PolylineAnnotation", e.Name, e.Action, e.PageNumber);
59+
}
60+
61+
private void PdfViewer_TextMarkupAnnotationChanged(object sender, TextMarkupAnnotationChangedEventArgs e)
62+
{
63+
if(e.Type == TextMarkupAnnotationType.Highlight)
64+
PerformAddorRemoveAnnotations("HighlightAnnotation", e.Name, e.Action, e.PageNumber);
65+
else if(e.Type == TextMarkupAnnotationType.Strikeout)
66+
PerformAddorRemoveAnnotations("StrikeoutAnnotation", e.Name, e.Action, e.PageNumber);
67+
else if(e.Type == TextMarkupAnnotationType.Underline)
68+
PerformAddorRemoveAnnotations("UnderlineAnnotation", e.Name, e.Action, e.PageNumber);
69+
else if(e.Type == TextMarkupAnnotationType.Squiggly)
70+
PerformAddorRemoveAnnotations("SquigglyAnnotation", e.Name, e.Action, e.PageNumber);
71+
}
72+
73+
private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e)
74+
{
75+
PerformAddorRemoveAnnotations("InkAnnotation",e.Name,e.Action,e.PageNumber);
76+
}
77+
78+
private void PerformAddorRemoveAnnotations(string name ,string annotationName ,AnnotationChangedAction action ,int annotationPageNumber)
79+
{
80+
if (action == AnnotationChangedAction.Add)
81+
{
82+
PdfLoadedDocument lDoc = pdfViewer.LoadedDocument;
83+
if (treeView.Items.Count > 0)
84+
{
85+
for (int i = 0; i < treeView.Items.Count; i++)
86+
{
87+
string[] page = (treeView.Items[i] as TreeViewItem).Header.ToString().Split(' ');
88+
int pageNumber = int.Parse(page[page.Length - 1]);
89+
if (pageNumber == annotationPageNumber)
90+
{
91+
TreeViewItem item = treeView.Items[i] as TreeViewItem;
92+
TreeViewItem childItem = new TreeViewItem();
93+
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
94+
{
95+
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
96+
{
97+
childItem.Header = name;
98+
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
99+
break;
100+
}
101+
}
102+
item.Items.Add(childItem);
103+
break;
104+
}
105+
else if (pageNumber > annotationPageNumber)
106+
{
107+
TreeViewItem item = new TreeViewItem();
108+
item.Header = "PAGE - " + annotationPageNumber;
109+
treeView.Items.Insert(i, item);
110+
TreeViewItem childItem = new TreeViewItem();
111+
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
112+
{
113+
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
114+
{
115+
childItem.Header = name;
116+
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
117+
break;
118+
}
119+
}
120+
item.Items.Add(childItem);
121+
}
122+
}
123+
}
124+
else
125+
{
126+
TreeViewItem item = new TreeViewItem();
127+
item.Header = "PAGE - " + annotationPageNumber;
128+
item.FontSize = 16;
129+
treeView.Items.Add(item);
130+
TreeViewItem childItem = new TreeViewItem();
131+
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
132+
{
133+
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
134+
{
135+
childItem.Header = name;
136+
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
137+
break;
138+
}
139+
}
140+
item.Items.Add(childItem);
141+
}
142+
}
143+
else if (action == AnnotationChangedAction.Remove)
144+
{
145+
for (int i = 0; i < treeView.Items.Count; i++)
146+
{
147+
string[] page = (treeView.Items[i] as TreeViewItem).Header.ToString().Split(' ');
148+
int pageNumber = int.Parse(page[page.Length - 1]);
149+
if (pageNumber == annotationPageNumber)
150+
{
151+
TreeViewItem item = treeView.Items[i] as TreeViewItem;
152+
for (int j = 0; j < item.Items.Count; j++)
153+
{
154+
PdfLoadedAnnotation loadedAnnotation = (item.Items[j] as TreeViewItem).Tag as PdfLoadedAnnotation;
155+
PdfAnnotation annotation = (item.Items[j] as TreeViewItem).Tag as PdfAnnotation;
156+
if (loadedAnnotation != null)
157+
{
158+
if (annotationName == loadedAnnotation.Name)
159+
{
160+
item.Items.RemoveAt(j);
161+
break;
162+
}
163+
}
164+
else if (annotation != null)
165+
{
166+
if (annotationName == annotation.Name)
167+
{
168+
item.Items.RemoveAt(j);
169+
break;
170+
}
171+
}
172+
}
173+
if (item.Items.Count == 0)
174+
{
175+
treeView.Items.RemoveAt(i);
176+
}
177+
break;
178+
}
179+
}
180+
}
181+
}
182+
183+
private void PdfViewer_StickyNoteAnnotationChanged(object sender, StickyNoteAnnotationChangedEventArgs e)
184+
{
185+
PerformAddorRemoveAnnotations("PopupAnnotation", e.Name, e.Action, e.PageNumber);
186+
}
187+
188+
private void PdfViewer_FreeTextAnnotationChanged(object sender, FreeTextAnnotationChangedEventArgs e)
189+
{
190+
PerformAddorRemoveAnnotations("FreeTextAnnotation", e.Name, e.Action, e.PageNumber);
191+
}
192+
193+
private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
194+
{
195+
PdfLoadedDocument loadedDocument = pdfViewer.LoadedDocument;
196+
treeView.Items.Clear();
197+
for (int i = 0; i < loadedDocument.Pages.Count; i++)
198+
{
199+
TreeViewItem viewItem = new TreeViewItem();
200+
if (loadedDocument.Pages[i].Annotations.Count > 0)
201+
{
202+
viewItem.Header = "PAGE - " + (i + 1);
203+
viewItem.FontSize = 16;
204+
treeView.Items.Add(viewItem);
205+
viewItem.IsExpanded = true;
206+
for (int j = 0; j < loadedDocument.Pages[i].Annotations.Count; j++)
207+
{
208+
PdfLoadedAnnotation annotation = loadedDocument.Pages[i].Annotations[j] as PdfLoadedAnnotation;
209+
if (annotation is PdfLoadedInkAnnotation || annotation is PdfLoadedTextMarkupAnnotation || annotation is PdfLoadedEllipseAnnotation
210+
|| annotation is PdfLoadedLineAnnotation || annotation is PdfLoadedRectangleAnnotation || annotation is PdfLoadedSquareAnnotation
211+
|| annotation is PdfLoadedCircleAnnotation || annotation is PdfLoadedFreeTextAnnotation || annotation is PdfLoadedRubberStampAnnotation
212+
|| annotation is PdfLoadedPopupAnnotation || annotation is PdfLoadedPolygonAnnotation || annotation is PdfLoadedPolyLineAnnotation)
213+
{
214+
string name = annotation.ToString();
215+
string[] annotNames = name.Split('.');
216+
TreeViewItem childItem = new TreeViewItem();
217+
childItem.Header = annotation.Type.ToString();
218+
childItem.Tag = annotation;
219+
viewItem.Items.Add(childItem);
220+
}
221+
}
222+
if (viewItem.Items.Count == 0)
223+
treeView.Items.Remove(viewItem);
224+
}
225+
}
226+
}
227+
228+
private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
229+
{
230+
var selectedItem = treeView.SelectedItem as TreeViewItem;
231+
if (selectedItem != null && !selectedItem.Header.ToString().Contains("PAGE"))
232+
{
233+
PdfLoadedAnnotation loadedAnnotation = selectedItem.Tag as PdfLoadedAnnotation;
234+
PdfAnnotation annotation = selectedItem.Tag as PdfAnnotation;
235+
if (loadedAnnotation != null)
236+
pdfViewer.SelectAnnotation(loadedAnnotation.Name, true);
237+
else if (annotation != null)
238+
pdfViewer.SelectAnnotation(annotation.Name, true);
239+
}
240+
}
241+
}
242+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System.Reflection;
2+
using System.Resources;
3+
using System.Runtime.CompilerServices;
4+
using System.Runtime.InteropServices;
5+
using System.Windows;
6+
7+
// General Information about an assembly is controlled through the following
8+
// set of attributes. Change these attribute values to modify the information
9+
// associated with an assembly.
10+
[assembly: AssemblyTitle("WPF_PDFViewer_Annotations")]
11+
[assembly: AssemblyDescription("")]
12+
[assembly: AssemblyConfiguration("")]
13+
[assembly: AssemblyCompany("")]
14+
[assembly: AssemblyProduct("WPF_PDFViewer_Annotations")]
15+
[assembly: AssemblyCopyright("Copyright © 2022")]
16+
[assembly: AssemblyTrademark("")]
17+
[assembly: AssemblyCulture("")]
18+
19+
// Setting ComVisible to false makes the types in this assembly not visible
20+
// to COM components. If you need to access a type in this assembly from
21+
// COM, set the ComVisible attribute to true on that type.
22+
[assembly: ComVisible(false)]
23+
24+
//In order to begin building localizable applications, set
25+
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
26+
//inside a <PropertyGroup>. For example, if you are using US english
27+
//in your source files, set the <UICulture> to en-US. Then uncomment
28+
//the NeutralResourceLanguage attribute below. Update the "en-US" in
29+
//the line below to match the UICulture setting in the project file.
30+
31+
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
32+
33+
34+
[assembly: ThemeInfo(
35+
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
36+
//(used if a resource is not found in the page,
37+
// or application resource dictionaries)
38+
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
39+
//(used if a resource is not found in the page,
40+
// app, or any theme specific resource dictionaries)
41+
)]
42+
43+
44+
// Version information for an assembly consists of the following four values:
45+
//
46+
// Major Version
47+
// Minor Version
48+
// Build Number
49+
// Revision
50+
//
51+
// You can specify all the values or you can default the Build and Revision Numbers
52+
// by using the '*' as shown below:
53+
// [assembly: AssemblyVersion("1.0.*")]
54+
[assembly: AssemblyVersion("1.0.0.0")]
55+
[assembly: AssemblyFileVersion("1.0.0.0")]

0 commit comments

Comments
 (0)