@@ -625,6 +625,194 @@ import { PdfViewerComponent, LinkAnnotationService, BookmarkViewService,
625
625
626
626
> Run the [ web service] ( https://github.com/SyncfusionExamples/EJ2-PDFViewer-WebServices/tree/main/ASP.NET%20Core/PdfViewerWebService_3.0 ) and then the angular code. Also note that, the XFDF file for importing the annotation should be placed in the location as specified in the GetDocumentPath method of the PdfViewerController.
627
627
628
+ ## Importing Annotation Using Base64 Data
629
+
630
+ You can import annotations into the Syncfusion Angular PDF Viewer by decoding Base64-encoded JSON annotation data at runtime and passing the parsed object to the importAnnotation API, as shown in the following code snippet.
631
+
632
+ {% tabs %}
633
+ {% highlight ts tabtitle="Standalone" %}
634
+
635
+ import { Component, OnInit } from '@angular/core ';
636
+ import {
637
+ LinkAnnotationService, BookmarkViewService, MagnificationService,
638
+ ThumbnailViewService, ToolbarService, NavigationService,
639
+ TextSearchService, TextSelectionService, PrintService,
640
+ FormDesignerService, FormFieldsService, AnnotationService,
641
+ PageOrganizerService
642
+ } from '@syncfusion/ej2-angular-pdfviewer ';
643
+
644
+ @Component ({
645
+ selector: 'app-root',
646
+ template: `
647
+ <!-- Button to trigger annotation import -->
648
+ <button (click)="fileInput.click()">Import Annotation</button >
649
+
650
+ <!-- Hidden file input (only JSON files allowed) -->
651
+ <input type="file" #fileInput accept=".json" style="display:none" (change)="onFileSelected($event)" />
652
+
653
+ <!-- PDF Viewer container -->
654
+ <div class="content-wrapper">
655
+ <ejs-pdfviewer id="pdfViewer"
656
+ [documentPath]="document"
657
+ [resourceUrl]="resource"
658
+ style="height:640px;display:block">
659
+ </ejs-pdfviewer>
660
+ </div>
661
+ `,
662
+ // Register all necessary PDF viewer services
663
+ providers: [
664
+ LinkAnnotationService, BookmarkViewService, MagnificationService,
665
+ ThumbnailViewService, ToolbarService, NavigationService,
666
+ TextSearchService, TextSelectionService, PrintService,
667
+ AnnotationService, FormDesignerService, FormFieldsService, PageOrganizerService
668
+ ]
669
+ })
670
+ export class AppComponent implements OnInit {
671
+ // URL of the sample PDF document
672
+ public document: string = 'https://cdn.syncfusion.com/content/pdf/handwritten-signature.pdf ';
673
+
674
+ // Syncfusion PDF viewer resource URL
675
+ public resource: string = 'https://cdn.syncfusion.com/ej2/28.1.33/dist/ej2-pdfviewer-lib ';
676
+
677
+ ngOnInit(): void {}
678
+
679
+ /**
680
+ * Triggered when a file is selected from the input.
681
+ * Handles reading and importing the JSON annotation file.
682
+ * /
683
+ onFileSelected(event: any): void {
684
+ const file = event.target.files[ 0] ;
685
+
686
+ // Validate that the selected file is a .json file
687
+ if (!file || !file.name.endsWith('.json')) {
688
+ alert('Please select a valid JSON file.');
689
+ return;
690
+ }
691
+
692
+ const reader = new FileReader();
693
+
694
+ // When file reading is done
695
+ reader.onload = (e: any) => {
696
+ // Extract Base64 string (after the "data: application /json;base64," part)
697
+ const base64String = e.target.result.split(',')[ 1] ;
698
+
699
+ // Decode Base64 to original JSON string
700
+ const decodedJsonString = atob(base64String);
701
+
702
+ try {
703
+ // Parse the JSON string into a JavaScript object
704
+ const annotationData = JSON.parse(decodedJsonString);
705
+
706
+ // Get reference to the PDF viewer instance
707
+ const viewer = (document.getElementById('pdfViewer') as any).ej2_instances[0];
708
+
709
+ // Import the parsed annotation data into the viewer
710
+ viewer.importAnnotation(annotationData);
711
+ } catch (err) {
712
+ // Handle errors (e.g., invalid JSON)
713
+ console.error('Invalid JSON file:', err);
714
+ alert('Failed to parse JSON.');
715
+ }
716
+ };
717
+
718
+ // Start reading the file as a data URL (Base64 encoded)
719
+ reader.readAsDataURL(file);
720
+ }
721
+ }
722
+
723
+ {% endhighlight %}
724
+ {% highlight ts tabtitle="Server-Backed" %}
725
+
726
+ import { Component, OnInit } from '@angular/core ';
727
+ import {
728
+ LinkAnnotationService, BookmarkViewService, MagnificationService,
729
+ ThumbnailViewService, ToolbarService, NavigationService,
730
+ TextSearchService, TextSelectionService, PrintService,
731
+ FormDesignerService, FormFieldsService, AnnotationService,
732
+ PageOrganizerService
733
+ } from '@syncfusion/ej2-angular-pdfviewer ';
734
+
735
+ @Component ({
736
+ selector: 'app-root',
737
+ template: `
738
+ <!-- Button to trigger annotation import -->
739
+ <button (click)="fileInput.click()">Import Annotation</button >
740
+
741
+ <!-- Hidden file input (only JSON files allowed) -->
742
+ <input type="file" #fileInput accept=".json" style="display:none" (change)="onFileSelected($event)" />
743
+
744
+ <!-- PDF Viewer container -->
745
+ <div class="content-wrapper">
746
+ <ejs-pdfviewer id="pdfViewer"
747
+ [documentPath]="document"
748
+ [serviceUrl]="service"
749
+ style="height:640px;display:block">
750
+ </ejs-pdfviewer>
751
+ </div>
752
+ `,
753
+ // Register all necessary PDF viewer services
754
+ providers: [
755
+ LinkAnnotationService, BookmarkViewService, MagnificationService,
756
+ ThumbnailViewService, ToolbarService, NavigationService,
757
+ TextSearchService, TextSelectionService, PrintService,
758
+ AnnotationService, FormDesignerService, FormFieldsService, PageOrganizerService
759
+ ]
760
+ })
761
+ export class AppComponent implements OnInit {
762
+ // URL of the sample PDF document
763
+ public document: string = 'https://cdn.syncfusion.com/content/pdf/handwritten-signature.pdf ';
764
+
765
+ // Syncfusion PDF viewer resource URL
766
+ public service: string = 'https://services.syncfusion.com/angular/production/api/pdfviewer ';
767
+
768
+ ngOnInit(): void {}
769
+
770
+ /**
771
+ * Triggered when a file is selected from the input.
772
+ * Handles reading and importing the JSON annotation file.
773
+ * /
774
+ onFileSelected(event: any): void {
775
+ const file = event.target.files[ 0] ;
776
+
777
+ // Validate that the selected file is a .json file
778
+ if (!file || !file.name.endsWith('.json')) {
779
+ alert('Please select a valid JSON file.');
780
+ return;
781
+ }
782
+
783
+ const reader = new FileReader();
784
+
785
+ // When file reading is done
786
+ reader.onload = (e: any) => {
787
+ // Extract Base64 string (after the "data: application /json;base64," part)
788
+ const base64String = e.target.result.split(',')[ 1] ;
789
+
790
+ // Decode Base64 to original JSON string
791
+ const decodedJsonString = atob(base64String);
792
+
793
+ try {
794
+ // Parse the JSON string into a JavaScript object
795
+ const annotationData = JSON.parse(decodedJsonString);
796
+
797
+ // Get reference to the PDF viewer instance
798
+ const viewer = (document.getElementById('pdfViewer') as any).ej2_instances[0];
799
+
800
+ // Import the parsed annotation data into the viewer
801
+ viewer.importAnnotation(annotationData);
802
+ } catch (err) {
803
+ // Handle errors (e.g., invalid JSON)
804
+ console.error('Invalid JSON file:', err);
805
+ alert('Failed to parse JSON.');
806
+ }
807
+ };
808
+
809
+ // Start reading the file as a data URL (Base64 encoded)
810
+ reader.readAsDataURL(file);
811
+ }
812
+ }
813
+ {% endhighlight %}
814
+ {% endtabs %}
815
+
628
816
## Exporting annotation from the PDF document
629
817
630
818
The PDF Viewer control provides the support to export the annotations as JSON file or JSON object and XFDF file using annotation toolbar.
0 commit comments