Get Custom XMP Metadata
This guide will walk you through the example of getting custom XMP 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.
Line 3-15
imports the necessary unipdf
packages.
The init
function in lines 17-24
authenticates your request using the UNIDOC_LICENSE_API_KEY
from the system environment.
The main
function in lines 26-95
is an entry point to the program and contains all the code that gets the metadata from the pdf file. In the main
function in lines 27-32
path to the input file is parsed from the command line argument. In lines 35-39
the time taken to finish running the program is measured. The input pdf file is read in lines 42-46
. The XMP metadata is extracted in using reader.GetCatalogMetadata()
in lines 49-52
.
The lines 55-58
the PdfObjectStream
represented by the metadata PdfObject
is retrieved using core.GetStream(metadata)
.
The XMP Document is loaded from the provided pdfObject stream in lines 61-64
. The xmp.Document
is loaded using xmpDoc.GetGoXmpDocument()
in line 67
. In lines 71-75
custom models are searched using the xmprights.FindModel(goXmpDoc)
method.
Finally the code in lines 76-94
tries to find and print some custom models such as certificate
, Owners
, Usage Terms
and Web statement
.
Run the code
go run pdf_get_custom_xmp_metadata.go <input.pdf>