66using Syncfusion . Pdf . Interactive ;
77using Syncfusion . Pdf . Redaction ;
88using Syncfusion . Maui . PdfViewer ;
9+ using System . Collections . Specialized ;
910using System . Collections . ObjectModel ;
1011
1112namespace SmartRedaction
@@ -25,21 +26,26 @@ public MainPage()
2526 sensitiveInfoView . NodeChecked += SensitiveInfoView_NodeChecked ;
2627 sensitiveInfoViewMobile . NodeChecked += SensitiveInfoView_NodeChecked ;
2728 MarkRedaction . StateChanged += MarkRedaction_StateChanged ;
28- PdfViewer . AnnotationAdded += PdfViewer_AnnotationAdded ;
2929 AddRedact . PropertyChanged += AddRedact_PropertyChanged ;
3030 PdfViewer . DocumentLoaded += PdfViewer_DocumentLoaded ;
31+ var redactionMarks = PdfViewer . RedactionMarks as INotifyCollectionChanged ;
32+ if ( redactionMarks != null )
33+ redactionMarks . CollectionChanged += OnRedactionMarksChanged ;
34+ }
35+
36+ private void OnRedactionMarksChanged ( object ? sender , NotifyCollectionChangedEventArgs e )
37+ {
38+ var hasItems = PdfViewer . RedactionMarks != null && PdfViewer . RedactionMarks . Count > 0 && MarkRedaction ? . IsChecked == true ;
39+ if ( hasItems == true )
40+ AddRedact . IsEnabled = hasItems ;
3141 }
3242
3343 private void MarkRedaction_StateChanged ( object ? sender , Syncfusion . Maui . Buttons . StateChangedEventArgs e )
3444 {
3545 if ( e . IsChecked . HasValue && e . IsChecked . Value )
36- {
37- PdfViewer . AnnotationMode = AnnotationMode . Square ;
38- PdfViewer . AnnotationSettings . Square . BorderWidth = 1 ;
39- PdfViewer . AnnotationSettings . Author = "RedactedRect" ;
40- }
46+ PdfViewer . RedactionMode = RedactionMode . Rect ;
4147 else
42- PdfViewer . AnnotationMode = AnnotationMode . None ;
48+ PdfViewer . RedactionMode = RedactionMode . None ;
4349 }
4450
4551 private void PdfViewer_DocumentLoaded ( object ? sender , EventArgs ? e )
@@ -55,6 +61,8 @@ private void PdfViewer_DocumentLoaded(object? sender, EventArgs? e)
5561 MobileScan . IsEnabled = true ;
5662 DesktopScanButton . IsEnabled = true ;
5763 }
64+ ViewModel . ChildNodes . Clear ( ) ;
65+ ViewModel . SensitiveInfo . Clear ( ) ;
5866 }
5967
6068 private void AddRedact_PropertyChanged ( object ? sender , System . ComponentModel . PropertyChangedEventArgs e )
@@ -81,144 +89,116 @@ private void OkClicked(object sender, EventArgs e)
8189
8290 }
8391
84- private void PdfViewer_AnnotationAdded ( object ? sender , AnnotationEventArgs e )
85- {
86- if ( ( bool ) MarkRedaction . IsChecked && e . Annotation is SquareAnnotation )
87- {
88- e . Annotation . Name = $ "RedactedRect{ PdfViewer . Annotations . Count } ";
89- e . Annotation . Author = "RedactedRect" ;
90- AddRedact . IsEnabled = true ;
91- }
92- else if ( e . Annotation is SquareAnnotation )
93- SelectRedactitem . IsEnabled = true ;
94- }
95-
9692 private void SensitiveInfoView_NodeChecked ( object ? sender , NodeCheckedEventArgs e )
9793 {
9894 if ( e . Node ? . Content is TreeItem treeItem )
9995 {
10096 // Access the NodeText
10197 string nodeId = treeItem . NodeId ;
10298 string nodeText = treeItem . NodeText ;
103- // Add or remove annotation for "Select All" nodes
99+ // Add or remove Redaction mark for "Select All" nodes
104100 if ( nodeId == "Select All" )
105101 {
106102 foreach ( TreeItem item in ViewModel . ChildNodes )
107103 {
104+ RectF bounds = new RectF ( )
105+ {
106+ X = item . Bounds . X ,
107+ Y = item . Bounds . Y ,
108+ Width = item . Bounds . Width ,
109+ Height = item . Bounds . Height
110+ } ;
108111 if ( e . Node . IsChecked == true )
109112 {
110- // Create a rectangle annotation
111- RectF Bounds = new RectF ( )
112- {
113- X = item . Bounds . X ,
114- Y = item . Bounds . Y ,
115- Width = item . Bounds . Width ,
116- Height = item . Bounds . Height
117- } ;
118- SquareAnnotation annotation = new SquareAnnotation ( Bounds , item . PageNumber )
119- {
120- Color = Colors . Red , // Set stroke color
121- BorderWidth = 1 , // Set stroke thickness
122- Name = item . NodeId // Set annotation ID
123- } ;
124- // Add the annotation to the PDF viewer
125- PdfViewer . AddAnnotation ( annotation ) ;
113+ // Create a Reedaction Mark
114+ RedactionMark mark = new RedactionMark ( bounds , item . PageNumber ) ;
115+ PdfViewer . AddRedactionMark ( mark ) ;
126116 }
127117 else
128118 {
129- // Find and remove the corresponding annotation
130- ReadOnlyObservableCollection < Annotation > annotations = PdfViewer . Annotations ;
131- foreach ( Annotation annotation in annotations )
119+ // Find and remove the corresponding Redaction Mark
120+ ReadOnlyObservableCollection < RedactionMark > redactions = PdfViewer . RedactionMarks ;
121+ foreach ( RedactionMark redaction in redactions )
132122 {
133- if ( annotation . Name == item . NodeId )
123+ if ( redaction . Bounds == bounds && redaction . PageNumber == item . PageNumber )
134124 {
135- PdfViewer . RemoveAnnotation ( annotation ) ;
125+ PdfViewer . RemoveRedactionMark ( redaction ) ;
136126 break ;
137127 }
138128 }
139129 }
140130 }
141131 }
142- // Add or remove annotation for a specific page
132+ // Add or remove Redaction Mark for a specific page
143133 else if ( nodeText . Contains ( "Page" ) )
144134 {
145135 foreach ( TreeItem item in ViewModel . ChildNodes )
146136 {
147- if ( item . PageNumber == int . Parse ( treeItem . NodeId ) )
137+ RectF bounds = new RectF ( )
138+ {
139+ X = item . Bounds . X ,
140+ Y = item . Bounds . Y ,
141+ Width = item . Bounds . Width ,
142+ Height = item . Bounds . Height
143+ } ;
144+ if ( item . PageNumber == int . Parse ( treeItem ? . NodeId ) )
148145 {
149146 if ( e . Node . IsChecked == true )
150147 {
151- // Create and add annotation for the specific page
152- RectF Bounds = new RectF ( )
153- {
154- X = item . Bounds . X ,
155- Y = item . Bounds . Y ,
156- Width = item . Bounds . Width ,
157- Height = item . Bounds . Height
158- } ;
159- SquareAnnotation annotation = new SquareAnnotation ( Bounds , item . PageNumber )
160- {
161- Color = Colors . Red ,
162- BorderWidth = 1 ,
163- Name = item . NodeId
164- } ;
165- PdfViewer . AddAnnotation ( annotation ) ;
148+ // Create and add Redaction mark for the specific page
149+ RedactionMark mark = new RedactionMark ( bounds , item . PageNumber ) ;
150+ PdfViewer . AddRedactionMark ( mark ) ;
166151 }
167152 else
168153 {
169- // Remove annotation for the specific page
170- ReadOnlyObservableCollection < Annotation > annotations = PdfViewer . Annotations ;
171- foreach ( Annotation annotation in annotations )
154+ // Remove Redaction mark for the specific page
155+ ReadOnlyObservableCollection < RedactionMark > redactions = PdfViewer . RedactionMarks ;
156+ foreach ( RedactionMark redaction in redactions )
172157 {
173- if ( annotation . Name == item . NodeId )
158+ if ( redaction . Bounds == bounds && redaction . PageNumber == item . PageNumber )
174159 {
175- PdfViewer . RemoveAnnotation ( annotation ) ;
160+ PdfViewer . RemoveRedactionMark ( redaction ) ;
176161 break ;
177162 }
178163 }
179164 }
180165 }
181166 }
182167 }
183- // Add or remove annotation for other nodes
168+ // Add or remove Redaction Marks for other nodes
184169 else
185170 {
171+
172+ // Create and Add Redaction Marks
173+ RectF bounds = new RectF ( )
174+ {
175+ X = treeItem . Bounds . X ,
176+ Y = treeItem . Bounds . Y ,
177+ Width = treeItem . Bounds . Width ,
178+ Height = treeItem . Bounds . Height
179+ } ;
186180 // Handle other specific nodes
187181 if ( e . Node . IsChecked == true )
188182 {
189- // Create and add annotation
190- RectF Bounds = new RectF ( )
191- {
192- X = treeItem . Bounds . X ,
193- Y = treeItem . Bounds . Y ,
194- Width = treeItem . Bounds . Width ,
195- Height = treeItem . Bounds . Height
196- } ;
197- SquareAnnotation annotation = new SquareAnnotation ( Bounds , treeItem . PageNumber )
198- {
199- Color = Colors . Red ,
200- BorderWidth = 1 ,
201- Name = treeItem . NodeId
202- } ;
203- PdfViewer . AddAnnotation ( annotation ) ;
183+ RedactionMark marks = new RedactionMark ( bounds , treeItem . PageNumber ) ;
184+ PdfViewer . AddRedactionMark ( marks ) ;
204185 }
205186 else
206187 {
207- // Remove annotation
208- ReadOnlyObservableCollection < Annotation > annotations = PdfViewer . Annotations ;
209- foreach ( Annotation annotation in annotations )
188+ // Remove Redaction mark
189+ ReadOnlyObservableCollection < RedactionMark > redactions = PdfViewer . RedactionMarks ;
190+ foreach ( RedactionMark redaction in redactions )
210191 {
211- if ( annotation . Name == treeItem . NodeId )
192+ if ( redaction . Bounds == bounds && redaction . PageNumber == treeItem . PageNumber )
212193 {
213- PdfViewer . RemoveAnnotation ( annotation ) ;
194+ PdfViewer . RemoveRedactionMark ( redaction ) ;
214195 break ;
215196 }
216197 }
217198 }
218199 }
219200 }
220- if ( PdfViewer . Annotations . Count >= 1 &&
221- PdfViewer . Annotations . OfType < SquareAnnotation > ( ) . Count ( a => ! string . IsNullOrEmpty ( a . Author ) || a . Name . Contains ( "RedactedRect" ) || a . Author . Contains ( "RedactedRect" ) ) >= 1 )
201+ if ( PdfViewer . RedactionMarks != null && PdfViewer . RedactionMarks . Count >= 0 )
222202 {
223203 SelectRedactitem . IsEnabled = true ;
224204 SelectRedactItem_Mobile . IsEnabled = true ;
@@ -438,40 +418,7 @@ private List<string> RemovePrefix(List<string> sensitiveInfo, List<string> selec
438418 private void Redact ( )
439419 {
440420 MarkRedaction . IsChecked = false ;
441- MemoryStream pdf = new MemoryStream ( ) ;
442- PdfViewer . SaveDocument ( pdf ) ;
443- PdfLoadedDocument loadedDocument = new PdfLoadedDocument ( pdf ) ;
444- foreach ( PdfLoadedPage page in loadedDocument . Pages )
445- {
446- List < PdfLoadedAnnotation > removeAnnotations = new List < PdfLoadedAnnotation > ( ) ;
447- foreach ( PdfLoadedAnnotation annotation in page . Annotations )
448- {
449- // Iterate through the annotations that highlight the sensitive information and redact the content.
450- if ( annotation is PdfLoadedRectangleAnnotation )
451- {
452- //Check the annot for Redaction
453- if ( annotation . Name . Contains ( "RedactedRect" ) || annotation . Author . Contains ( "RedactedRect" ) )
454- {
455- removeAnnotations . Add ( annotation ) ;
456- PdfRedaction redaction = new PdfRedaction ( annotation . Bounds , Syncfusion . Drawing . Color . Black ) ;
457- page . AddRedaction ( redaction ) ;
458- annotation . Flatten = true ;
459- }
460-
461- }
462- }
463- //Remove from the Annotation list
464- foreach ( PdfLoadedAnnotation annotation in removeAnnotations )
465- {
466- page . Annotations . Remove ( annotation ) ;
467- }
468- }
469- loadedDocument . Redact ( ) ;
470- //Reload the document to view the redaction
471- MemoryStream stream = new MemoryStream ( ) ;
472- loadedDocument . Save ( stream ) ;
473- PdfViewer . LoadDocument ( stream ) ;
474- loadedDocument . Close ( true ) ;
421+ PdfViewer . RedactAsync ( ) ;
475422 AddRedact . IsEnabled = false ;
476423 }
477424
0 commit comments