Skip to content

Commit 1ae9ef7

Browse files
932599: Updated Sample to standalone and perform OCR in server
1 parent 64196e7 commit 1ae9ef7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+56
-40570
lines changed

How to/Perform OCR before loading the document/Angular Client/src/app/app.component.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Component, OnInit } from '@angular/core';
2+
import { HttpClient } from '@angular/common/http';
23
import {
34
LinkAnnotationService,
45
BookmarkViewService,
@@ -18,9 +19,10 @@ import {
1819
selector: 'app-root',
1920
template: `
2021
<div class="content-wrapper">
22+
<button (click)="performOCR()">Perform OCR</button>
2123
<ejs-pdfviewer
2224
id="pdfViewer"
23-
[serviceUrl]="serviceUrl"
25+
[resourceUrl]="resource"
2426
[documentPath]="document"
2527
style="height: 640px; display: block;">
2628
</ejs-pdfviewer>
@@ -42,9 +44,49 @@ import {
4244
]
4345
})
4446
export class AppComponent implements OnInit {
45-
public document: string = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf';
46-
public serviceUrl: string ='https://localhost:44309/pdfviewer';
47+
public document: string = 'https://cdn.syncfusion.com/content/pdf/pdf-succinctly.pdf'; // Initial PDF file URL
48+
public resource: string = "https://cdn.syncfusion.com/ej2/26.2.11/dist/ej2-pdfviewer-lib"; // Syncfusion library URL
4749

48-
ngOnInit(): void { }
50+
constructor(private http: HttpClient) {}
4951

52+
ngOnInit(): void {}
53+
54+
performOCR() {
55+
// Fetch the PDF file as a Blob
56+
this.http.get(this.document, { responseType: 'blob' }).subscribe((pdfBlob) => {
57+
const reader = new FileReader();
58+
reader.onloadend = () => {
59+
let base64PDF = reader.result as string;
60+
61+
// Remove the prefix if it exists (e.g., data:application/pdf;base64,)
62+
if (base64PDF.startsWith('data:application/pdf;base64,')) {
63+
base64PDF = base64PDF.substring('data:application/pdf;base64,'.length);
64+
}
65+
66+
// Construct the body to match the Dictionary<string, string> on the server
67+
const body = {
68+
documentBase64: base64PDF
69+
};
70+
71+
// Send the Base64 PDF to the server to perform OCR
72+
this.http.post('https://localhost:44309/pdfviewer/PerformOCR', body, { responseType: 'text' })
73+
.subscribe({
74+
next: (response: string) => {
75+
// Handle the server response (processed PDF in Base64 format)
76+
this.document = response;
77+
},
78+
error: (err: any) => {
79+
// Handle any errors
80+
console.error('OCR processing failed', err);
81+
},
82+
complete: () => {
83+
console.log('OCR processing complete');
84+
}
85+
});
86+
};
87+
88+
// Read the PDF Blob as a Base64 string
89+
reader.readAsDataURL(pdfBlob);
90+
});
91+
}
5092
}

How to/Perform OCR before loading the document/Angular Client/src/app/app.module.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
TextSelectionService,
1313
PrintService
1414
} from '@syncfusion/ej2-angular-pdfviewer';
15+
import { HttpClientModule } from '@angular/common/http'; // Import HttpClientModule
1516
import { AppComponent } from './app.component';
1617

1718
@NgModule({
@@ -20,17 +21,20 @@ import { AppComponent } from './app.component';
2021
],
2122
imports: [
2223
BrowserModule,
23-
PdfViewerModule
24+
PdfViewerModule,
25+
HttpClientModule // Add HttpClientModule here
2426
],
25-
providers: [LinkAnnotationService,
27+
providers: [
28+
LinkAnnotationService,
2629
BookmarkViewService,
2730
MagnificationService,
2831
ThumbnailViewService,
2932
ToolbarService,
3033
NavigationService,
3134
TextSearchService,
3235
TextSelectionService,
33-
PrintService],
36+
PrintService
37+
],
3438
bootstrap: [AppComponent]
3539
})
3640
export class AppModule { }

0 commit comments

Comments
 (0)