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.
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
import section in line
3-13 imports unipdf packages and other necessary libraries.
init function which is defined in lines
15-22 authenticates your request.
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
pdfWriter is created from the reader object in lines
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
71-82 a new media management is created
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
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
Finally the document is written to file using
pdfWriter.WriteToFile(outputPath) in lines
Run the code
go run pdf_set_xmp_media_management_metadata.go <input.pdf> <output.pdf>