Retrieving attached files

This guide will show you how to retrieve list of file attachments and extract it from the PDF file.

Before you begin

You should get your API key from yourUniCloud 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 the 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 render folder in the unipdf-examples directory.

cd unipdf-examples/attachment

How it works

Lines 7-14 import the UniPDF packages and other required dependencies.

The init function in lines 16-23 authenticates your request with your UNIDOC_LICENSE_API_KEY.

The main function in lines 25-35 defines which PDF file be used as an input file and passes it to the listAttachments function in line 37-73 that will retrive the attachment list and store it locally.

First we’ll need to open the PDF file and get the PDFReader object (line 38-48) then from there we could call the GetAttachedFiles function to get the list of file attachments.

The EmbeddedFile method returned from the GetAttachedFiles function has a Content attribute that could be written to a file (line 54-67).

Run the code

Run this command to extract the attached files from a PDF document and save it under an output folder. This will also get all the required dependencies to run the program.

go run pdf_get_attachment.go

Got any Questions?

We're here to help you.