Set Doc Info Metadata

This guide will explain the process of setting docinfo metadata step by step.

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 lines 10-17 imports unipdf packages and other necessary libraries. The init function loads your metered License API key and authenticates your library request.

In lines 28-84 the main function is defined. Here in lines 29-33 the number of command lines arguments is checked. The document’s author is set in lines 35-36. In lines 38-48 a new model.PdfReader is constructed.

In lines 51-53 a new ReaderToWriterOpts is initialized and the SkipInfo attribute is set to true to avoid copying document info. In lines 56-60 a new model.PdfWriter is initialized from the model.PdfReader using pdfReader.ToWriter(opt) method.

In line 65 a copy of the defaultPdfWriter is set to customPdfWriter for later use with custom document info. The content of the defaultPdfWriter is written to file in line 65-69. In lines 72-77 a custom info is added to the writer using customPdfWriter. Finally in lines 79-83 the document is written to a file.

Run the code

go run pdf_metadata_set_docinfo.go <input.pdf>

Got any Questions?

We're here to help you.