Reputation: 335
This is the input field for the document to be uploaded
<ion-item>
<ion-input name='File' required type="file" (change)="getNoOfPages($event)"></ion-input>
</ion-item>
This is the function am calling after the file is selected, am using the string split method to find the type because 'type' does not always holds the file type info, can I use any js library to find the number of pages in uploaded document (pdf in this case) or do I have to use anything in specific to make it work on android ? and how ?
getNoOfPages(event: any) {
const fileInfo = event.target.files[0];
const type = fileInfo.name.split('.')[1];
console.log('document uploaded ', fileInfo);
switch (type) {
case 'docx':
break;
case 'pdf':
console.log('this is a pdf file');
break;
}
}
Upvotes: 2
Views: 7534
Reputation: 1214
You could use a pure javascript (typescript syntax) solution:
const reader = new FileReader();
const fileInfo = event.target.files[0];
if (fileInfo) {
reader.readAsBinaryString(event.target.files[0]);
reader.onloadend = () => {
const count = reader.result.match(/\/Type[\s]*\/Page[^s]/g).length;
console.log('Number of Pages:', count);
}
}
I tested it on many pdf docs and it works.
-Best regards.
Upvotes: 11