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.
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.
Configure environment variables
UNIDOC_LICENSE_API_KEY with your API credentials from your UniCloud account.
How it works
The following example code gets metadata from pdf file.
import section defined in lines
13-24 imports necessary
unipdf packages and other go libraries.
init function in lines
UNIDOC_LICENSE_API_KEY from system environment and authenticates your request.
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 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.
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
xmlDecoder.Decode(&xmp). Finally the metadata is printed using
xmp.print() in line
getRootCatalog function defined in lines
102-123 returns the root catalog
127-138 the function
resolve which is used to resolve reference objects is defined.
xmpMetadata type is defined which is used to hold the xmp metadata. Two methods of this
xmpMetadata type, namely
print() are defined in lines
Run the code
go run pdf_metadata_get_xml.go <input1.pdf> <input2.pdf> ...