如何获取PDF文件的页数?

Aka*_*iye 0 javascript typescript ionic-framework angular

这是要上传的文档的输入字段

<ion-item>
<ion-input name='File' required type="file" (change)="getNoOfPages($event)"></ion-input>
</ion-item>
Run Code Online (Sandbox Code Playgroud)

这是选择文件后调用的函数,我使用字符串拆分方法来查找类型,因为'type'并不总是保存文件类型信息,我可以使用任何js库来查找上传文档中的页数吗(在这种情况下为pdf)还是我必须使用任何特定的东西才能使其在android上运行?如何 ?

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;
 }
}
Run Code Online (Sandbox Code Playgroud)

1an*_*es1 5

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);
     }
}
Run Code Online (Sandbox Code Playgroud)

I tested it on many pdf docs and it works.

-Best regards.