Get XML Metadata
In this guide getting the XML metadata of root catalog for PDF files will be demonstrated.
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.
The import
section defined in lines 13-24
imports necessary unipdf
packages and other go libraries.
The init
function in lines 26-33
loads UNIDOC_LICENSE_API_KEY
from system environment and authenticates your request.
The main
function is defined in lines 35-52
and is used to get the XML pdf metadata. In lines 36-42
the input files is parsed from the command line argument. The loop in lines 43-51
iterates through each input file and prints XML metadata for each pdf document using printXMLMetadataForPdf
.
The printXMLMetadataForPdf
function defined in lines 54-98
is used to get the XML metadata and print it to standard output. In this function in lines 55-64
a new model.PdfReader
is created using the input file given as an argument of the function.
In lines 66-69
the root catalog is obtained using the function getRootCatalog
. The code in lines 72-84
gets the XML metadata. Then it is decoded into xmp metadata format in lines 87-92
using xmlDecoder.Decode(&xmp)
. Finally the metadata is printed using xmp.print()
in line 95
.
The getRootCatalog
function defined in lines 102-123
returns the root catalog PdfObjectDictionary
type.
In lines 127-138
the function resolve
which is used to resolve reference objects is defined.
In lines 143-151
the xmpMetadata
type is defined which is used to hold the xmp metadata. Two methods of this xmpMetadata
type, namely keyValMap()
and print()
are defined in lines 155-180
.
Run the code
go run pdf_metadata_get_xml.go <input1.pdf> <input2.pdf> ...