Get Doc Info Metadata

This guide will walk you through the example of getting XMP doc info meta data from a pdf file.

Before you begin

You should get your API key from your UniCloud account.

If this is your first time using UniPDF SDK, follow this guide to set up a local development environment.

Project setup

Clone the project repository

In your terminal, clone examples repository. It contains the Go code we will be using for this guide.

git clone https://github.com/unidoc/unipdf-examples.git

Navigate to the metadata folder in the unipdf-examples directory.

cd unipdf-examples/metadata

Configure environment variables

Replace the UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.

Linux/Mac

export UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

Windows

set UNIDOC_LICENSE_API_KEY=PUT_YOUR_API_KEY_HERE

How it works

The following example code gets metadata from pdf file.

In the above code in lines 9-16 unipdf packages and other necessary libraries are imported. The init function in lines 18-25 loads your api key and authenticates your request.

The main function is defined in lines 27-44. In lines 28-32 the input files are read from the command line arguments. The loop in lines 35-43 iterates through each file and extracts the document info and prints it using printPdfDocInfo(inputPath).

In lines 46-119 the function printPdfDocInfo which is used to print the document info is defined. In line 47-51 the file is opened using os.Open(inputPath). In lines 53-61 a new pdfReader object is instantiated and the number of pages is retrieved using pdfReader.GetNumPages(). The document info is extracted using pdfReader.GetPdfInfo() in line 63.

In lines 68-71 a new pdfDocInfo object is instantiated using the following code.


    di := pdfDocInfo{
      Filename: path.Base(inputPath),
      NumPages: numPages,
    }

In lines 73-114 all the available information is extracted from the pdfInfo and is set to the pdfDocInfo. And in line 116 the information is printed using di.print().

In lines 122-136 the pdfDocInfo type is defined. In lines 139-157 The print method of pdfDocInfo which is used to print the extracted pdf document information is defined.

Run the code

go run pdf_metadata_get_docinfo.go <input1.pdf> <input2.pdf> ...

Got any Questions?

We're here to help you.