2014-03-30 65 views
3

有没有什么方法可以使用pdf.js从pdf文档如作者或标题获取元数据?如何使用pdf.js从pdf文档获取元数据

在这个例子中:http://mozilla.github.io/pdf.js/web/viewer.html?file=compressed.tracemonkey-pldi-09.pdf

<div class="row"> 
<span data-l10n-id="document_properties_author"> 
    Autor: 
</span> 
<p id="authorField"> 
    - 
</p> 

而且authorField是空的。有什么方法可以获得这些信息吗?

+0

您能否包含您使用的代码片段或其他内容? – amoebe

+0

PDF没有填充作者字段。显示不同的文档,例如http://mozilla.github.io/pdf.js/web/viewer.html?file=/deuxdrop/pdf-docs/conversation-protocol.pdf – async5

回答

0

您可以从PDFViewerApplication.documentInfo对象中获取文档基本元数据信息。例如:要获取作者使用PDFViewerApplication.documentInfo.Author

6

只使用PDF.js库没有第三方查看器,您可以使用承诺获取像这样的元数据。

PDFJS.getDocument(url).then(function (pdfDoc_) { 
     pdfDoc = pdfDoc_; 
     pdfDoc.getMetadata().then(function(stuff) { 
      console.log(stuff); // Metadata object here 
     }).catch(function(err) { 
      console.log('Error getting meta data'); 
      console.log(err); 
     }); 

     // Render the first page or whatever here 
     // More code . . . 
    }).catch(function(err) { 
     console.log('Error getting PDF from ' + url); 
     console.log(err); 
    }); 

我的pdfDoc对象倾销到控制台,并通过其功能和性能看后发现了这一点。我在其原型中找到了该方法,并决定只是试一试。你看,它的工作!

+0

我认为你的短语“利用承诺”是一个引入的错误在拼写检查过程中? :) – unforgettableid

0
pdfDoc.getMetadata(url).then(function(stuff) { 
    var metadata = stuff.info.Title; 
    if (metadata) { 
     $('#element-html').text(stuff.info.Title); // Print metadata to html 
    } 
console.log(stuff); // Print metadata to console 
}).catch(function(err) { 
    console.log('Error getting meta data'); 
    console.log(err); 
});