Set XMP Media Management Metadata
This guide will show how to set media management XMP metadata.
Before you begin
First of all 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 import
section in line 3-13
imports unipdf packages and other necessary libraries.
The init
function which is defined in lines 15-22
authenticates your request.
The main
function defined in lines 24-122
sets the media management metadata. In this function, lines 25-31
the input and the output file paths are processed from the command line argument.
The time taken to process the document is measured in lines 34-38
. A new pdf reader is instantiated using NewPdfReaderFromFile
in lines 41-45
.
A new pdfWriter
is created from the reader object in lines 48-51
using ToWriter
method of the the PdfReader
. The section in lines 54-69
extracts metadata from the input file if it already exists, otherwise it creates a new xmputil.Document
.
in lines 71-82
a new media management is created xmputil.MediaManagement
.
The media management options is set in lines 84-92
. In lines 93-95
the media management object is set to the XMP document using xmpDoc.SetMediaManagement(mmOptions)
.
The XMP document is marshaled into XML byte stream in lines 101-104
. Then a new PdfObjectStream
is created from the byte stream using core.MakeStream(metadataBytes, nil)
. In lines 113-115
the stream metadata is set to the pdfWriter
using SetCatalogMetadata(metadataStream)
.
Finally the document is written to file using pdfWriter.WriteToFile(outputPath)
in lines 118-121
.
Run the code
go run pdf_set_xmp_media_management_metadata.go <input.pdf> <output.pdf>