Read PDF information
In this documentation, you can learn how to implement specific features of the PDF Web SDK. If you prefer to test a demo or would like to explore the basic functionalities of this SDK, review Getting started with the PDF Viewer SDK.
Learn how to read information from a PDF document using the PDF Web SDK.
The PDF Web SDK lets you access document information without instantiating a viewer and without the need of a server to process the document. Use this functionality to display document information such as the author or page count to the user before rendering the document (for example, in a file chooser).
Steps to read information from a PDF document:
- Initialize the SDK
- Create controller
- Open PDF
- Read document information programmatically
- Full example
Learn how to get started and make static assets available.
Initialize the SDK
Before instantiating objects and working with PDF Web SDK API, it needs to be initialized.
import { pdfToolsWebSdk, Pdf, UI } from '@pdftools/pdf-web-sdk';
async function initialize() {
await pdfToolsWebSdk.initialize({
path: './pdftools-web-sdk/',
});
}
initialize();
Create controller
A Pdf.Controller
needs to be created to open the desired document. This controller is responsible for reading document information.
const controller = new Pdf.Controller();
Open PDF
Finally the document can be loaded to be later used to access the document information.
const pdfDocument = await controller.openDocument({
uri: '/pdf/WebViewer_Demo.pdf',
});
Read document information programmatically
The document loaded via the Pdf.Controller
has several properties that can be used to access document information.
Some of them are directly accessible, others are stored in the metadata
.
// properties stored in the metadata
const metadata = await pdfDocument.getMetadata();
console.log(`title = ${metadata.title}`);
console.log(`author = ${metadata.author}`);
console.log(`creation date = ${metadata.creationDate}`);
// direct properties
console.log(`page count = ${pdfDocument.pageCount}`);
The Metadata API is still under development and not all information available in the PDF might already be available on the API.
For a full list of information refer to the api-references for Pdf.Document and Pdf.Metadata.
Full example
This full sample demonstrates how to open a document and log document information to the console.
import { pdfToolsWebSdk, Pdf } from '@pdftools/pdf-web-sdk';
pdfToolsWebSdk.initialize().then(async () => {
const controller = new Pdf.Controller();
const pdfDocument = await controller.openDocument({
uri: '/pdf/WebViewer_Demo.pdf',
});
// properties stored in the metadata
const metadata = await pdfDocument.getMetadata();
console.log(`title = ${metadata.title}`);
console.log(`author = ${metadata.author}`);
console.log(`creation date = ${metadata.creationDate}`);
// direct properties
console.log(`page count = ${pdfDocument.pageCount}`);
});