javascript - How to get the number of pages in PDF file? - Stack Overflow

This is the input field for the document to be uploaded<ion-item><ion-input name='File

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;
 }
}

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;
 }
}
Share Improve this question edited Sep 24, 2019 at 12:06 help-info.de 7,29816 gold badges42 silver badges42 bronze badges asked Sep 24, 2019 at 11:48 Akash GajbhiyeAkash Gajbhiye 3354 silver badges18 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 11

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743780860a4505769.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信