Skip to content

Added sample to list the Annotations present in the PdfViewer #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Syncfusion.Licensing" publicKeyToken="632609b4d040f6b4" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-20.3460.0.47" newVersion="20.3460.0.47" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Syncfusion.Compression.Base" publicKeyToken="3d67ed1f87d44c89" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-100.2460.1.0" newVersion="100.2460.1.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Application x:Class="WPF_PDFViewer_Annotations.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPF_PDFViewer_Annotations"
StartupUri="MainWindow.xaml">
<Application.Resources>

</Application.Resources>
</Application>
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;

namespace WPF_PDFViewer_Annotations
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF_PDFViewer_Annotations"
xmlns:PdfViewer="clr-namespace:Syncfusion.Windows.PdfViewer;assembly=Syncfusion.PdfViewer.WPF" xmlns:syncfusion="http://schemas.syncfusion.com/wpf" x:Class="WPF_PDFViewer_Annotations.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" WindowState="Maximized">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="250"/>
</Grid.ColumnDefinitions>
<PdfViewer:PdfViewerControl x:Name="pdfViewer" ZoomMode="FitWidth"/>
<TreeView x:Name="treeView" Grid.Column="1" SelectedItemChanged="treeView_SelectedItemChanged"/>
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -0,0 +1,242 @@
using Syncfusion.Pdf.Interactive;
using Syncfusion.Pdf.Parsing;
using Syncfusion.SfSkinManager;
using Syncfusion.Windows.PdfViewer;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
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;

namespace WPF_PDFViewer_Annotations
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
SfSkinManager.SetTheme(this, new Theme("FluentLight"));
InitializeComponent();
pdfViewer.DocumentLoaded += PdfViewer_DocumentLoaded;
pdfViewer.FreeTextAnnotationChanged += PdfViewer_FreeTextAnnotationChanged;
pdfViewer.StickyNoteAnnotationChanged += PdfViewer_StickyNoteAnnotationChanged;
pdfViewer.InkAnnotationChanged += PdfViewer_InkAnnotationChanged;
pdfViewer.TextMarkupAnnotationChanged += PdfViewer_TextMarkupAnnotationChanged;
pdfViewer.ShapeAnnotationChanged += PdfViewer_ShapeAnnotationChanged;
pdfViewer.StampAnnotationChanged += PdfViewer_StampAnnotationChanged;
pdfViewer.Load("../../Data/Sample Document.pdf");
treeView.Background = new SolidColorBrush(Color.FromRgb(235,235,238));
}

private void PdfViewer_StampAnnotationChanged(object sender, StampAnnotationChangedEventArgs e)
{
PerformAddorRemoveAnnotations("RubberStampAnnotation", e.Name, e.Action, e.PageNumber);
}

private void PdfViewer_ShapeAnnotationChanged(object sender, ShapeAnnotationChangedEventArgs e)
{
if(e.Type == ShapeAnnotationType.Arrow || e.Type == ShapeAnnotationType.Line)
PerformAddorRemoveAnnotations("LineAnnotation", e.Name, e.Action, e.PageNumber);
else if(e.Type == ShapeAnnotationType.Circle)
PerformAddorRemoveAnnotations("EllipseAnnotation", e.Name, e.Action, e.PageNumber);
else if (e.Type == ShapeAnnotationType.Polygon)
PerformAddorRemoveAnnotations("PolygonAnnotation", e.Name, e.Action, e.PageNumber);
else if (e.Type == ShapeAnnotationType.Rectangle)
PerformAddorRemoveAnnotations("SquareAnnotation", e.Name, e.Action, e.PageNumber);
else if (e.Type == ShapeAnnotationType.Polyline)
PerformAddorRemoveAnnotations("PolylineAnnotation", e.Name, e.Action, e.PageNumber);
}

private void PdfViewer_TextMarkupAnnotationChanged(object sender, TextMarkupAnnotationChangedEventArgs e)
{
if(e.Type == TextMarkupAnnotationType.Highlight)
PerformAddorRemoveAnnotations("HighlightAnnotation", e.Name, e.Action, e.PageNumber);
else if(e.Type == TextMarkupAnnotationType.Strikeout)
PerformAddorRemoveAnnotations("StrikeoutAnnotation", e.Name, e.Action, e.PageNumber);
else if(e.Type == TextMarkupAnnotationType.Underline)
PerformAddorRemoveAnnotations("UnderlineAnnotation", e.Name, e.Action, e.PageNumber);
else if(e.Type == TextMarkupAnnotationType.Squiggly)
PerformAddorRemoveAnnotations("SquigglyAnnotation", e.Name, e.Action, e.PageNumber);
}

private void PdfViewer_InkAnnotationChanged(object sender, InkAnnotationChangedEventArgs e)
{
PerformAddorRemoveAnnotations("InkAnnotation",e.Name,e.Action,e.PageNumber);
}

private void PerformAddorRemoveAnnotations(string name ,string annotationName ,AnnotationChangedAction action ,int annotationPageNumber)
{
if (action == AnnotationChangedAction.Add)
{
PdfLoadedDocument lDoc = pdfViewer.LoadedDocument;
if (treeView.Items.Count > 0)
{
for (int i = 0; i < treeView.Items.Count; i++)
{
string[] page = (treeView.Items[i] as TreeViewItem).Header.ToString().Split(' ');
int pageNumber = int.Parse(page[page.Length - 1]);
if (pageNumber == annotationPageNumber)
{
TreeViewItem item = treeView.Items[i] as TreeViewItem;
TreeViewItem childItem = new TreeViewItem();
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
{
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
{
childItem.Header = name;
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
break;
}
}
item.Items.Add(childItem);
break;
}
else if (pageNumber > annotationPageNumber)
{
TreeViewItem item = new TreeViewItem();
item.Header = "PAGE - " + annotationPageNumber;
treeView.Items.Insert(i, item);
TreeViewItem childItem = new TreeViewItem();
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
{
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
{
childItem.Header = name;
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
break;
}
}
item.Items.Add(childItem);
}
}
}
else
{
TreeViewItem item = new TreeViewItem();
item.Header = "PAGE - " + annotationPageNumber;
item.FontSize = 16;
treeView.Items.Add(item);
TreeViewItem childItem = new TreeViewItem();
for (int j = 0; j < lDoc.Pages[annotationPageNumber - 1].Annotations.Count; j++)
{
if (lDoc.Pages[annotationPageNumber - 1].Annotations[j].Name == annotationName)
{
childItem.Header = name;
childItem.Tag = lDoc.Pages[annotationPageNumber - 1].Annotations[j];
break;
}
}
item.Items.Add(childItem);
}
}
else if (action == AnnotationChangedAction.Remove)
{
for (int i = 0; i < treeView.Items.Count; i++)
{
string[] page = (treeView.Items[i] as TreeViewItem).Header.ToString().Split(' ');
int pageNumber = int.Parse(page[page.Length - 1]);
if (pageNumber == annotationPageNumber)
{
TreeViewItem item = treeView.Items[i] as TreeViewItem;
for (int j = 0; j < item.Items.Count; j++)
{
PdfLoadedAnnotation loadedAnnotation = (item.Items[j] as TreeViewItem).Tag as PdfLoadedAnnotation;
PdfAnnotation annotation = (item.Items[j] as TreeViewItem).Tag as PdfAnnotation;
if (loadedAnnotation != null)
{
if (annotationName == loadedAnnotation.Name)
{
item.Items.RemoveAt(j);
break;
}
}
else if (annotation != null)
{
if (annotationName == annotation.Name)
{
item.Items.RemoveAt(j);
break;
}
}
}
if (item.Items.Count == 0)
{
treeView.Items.RemoveAt(i);
}
break;
}
}
}
}

private void PdfViewer_StickyNoteAnnotationChanged(object sender, StickyNoteAnnotationChangedEventArgs e)
{
PerformAddorRemoveAnnotations("PopupAnnotation", e.Name, e.Action, e.PageNumber);
}

private void PdfViewer_FreeTextAnnotationChanged(object sender, FreeTextAnnotationChangedEventArgs e)
{
PerformAddorRemoveAnnotations("FreeTextAnnotation", e.Name, e.Action, e.PageNumber);
}

private void PdfViewer_DocumentLoaded(object sender, EventArgs args)
{
PdfLoadedDocument loadedDocument = pdfViewer.LoadedDocument;
treeView.Items.Clear();
for (int i = 0; i < loadedDocument.Pages.Count; i++)
{
TreeViewItem viewItem = new TreeViewItem();
if (loadedDocument.Pages[i].Annotations.Count > 0)
{
viewItem.Header = "PAGE - " + (i + 1);
viewItem.FontSize = 16;
treeView.Items.Add(viewItem);
viewItem.IsExpanded = true;
for (int j = 0; j < loadedDocument.Pages[i].Annotations.Count; j++)
{
PdfLoadedAnnotation annotation = loadedDocument.Pages[i].Annotations[j] as PdfLoadedAnnotation;
if (annotation is PdfLoadedInkAnnotation || annotation is PdfLoadedTextMarkupAnnotation || annotation is PdfLoadedEllipseAnnotation
|| annotation is PdfLoadedLineAnnotation || annotation is PdfLoadedRectangleAnnotation || annotation is PdfLoadedSquareAnnotation
|| annotation is PdfLoadedCircleAnnotation || annotation is PdfLoadedFreeTextAnnotation || annotation is PdfLoadedRubberStampAnnotation
|| annotation is PdfLoadedPopupAnnotation || annotation is PdfLoadedPolygonAnnotation || annotation is PdfLoadedPolyLineAnnotation)
{
string name = annotation.ToString();
string[] annotNames = name.Split('.');
TreeViewItem childItem = new TreeViewItem();
childItem.Header = annotation.Type.ToString();
childItem.Tag = annotation;
viewItem.Items.Add(childItem);
}
}
if (viewItem.Items.Count == 0)
treeView.Items.Remove(viewItem);
}
}
}

private void treeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
var selectedItem = treeView.SelectedItem as TreeViewItem;
if (selectedItem != null && !selectedItem.Header.ToString().Contains("PAGE"))
{
PdfLoadedAnnotation loadedAnnotation = selectedItem.Tag as PdfLoadedAnnotation;
PdfAnnotation annotation = selectedItem.Tag as PdfAnnotation;
if (loadedAnnotation != null)
pdfViewer.SelectAnnotation(loadedAnnotation.Name, true);
else if (annotation != null)
pdfViewer.SelectAnnotation(annotation.Name, true);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Reflection;
using System.Resources;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Windows;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("WPF_PDFViewer_Annotations")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("WPF_PDFViewer_Annotations")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

//In order to begin building localizable applications, set
//<UICulture>CultureYouAreCodingWith</UICulture> in your .csproj file
//inside a <PropertyGroup>. For example, if you are using US english
//in your source files, set the <UICulture> to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.

//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]


[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]


// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
Loading