Get PDF Info
This guide will demonstrate how to get basic information of a PDF document.
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 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 analysis
folder in the unipdf-examples
directory.
cd unipdf-examples/analysis
How it works
The import
section is used to import necessary UniPDF packages and other Go libraries.
The init
section loads your license key prior to running the program.
The main
function in lines 25-45
, iterates through each input file in the command line arguments and gets the document information using getPdfProperties(inputPath)
.
The PdfProperties
type which is used to hold PDF information is defined in lines 47-51
.
Lines 53-93
define the getPdfProperties
function. In this function in line 63
, a new PdfReader is defined using pdfReader, err := model.NewPdfReader(f)
. Then the file is checked for encryption status using the IsEncrypted()
method of model.PdfReader
. If the document is encrypted the code in lines 77-84
, tries to decrypt the document using an empty password as follows:
// Try decrypting with an empty one.
if isEncrypted {
auth, err := pdfReader.Decrypt([]byte(""))
if err != nil {
return nil, err
}
ret.CanView = auth
return &ret, nil
}
Line 86
, gets the number of pages using pdfReader.GetNumPages()
.
Run the code
Run the code using the following command:
go run pdf_info.go input1.pdf [input2.pdf] ...
Sample output
Input file: templates/documentation/unipdf-templates-documentation.pdf
Num Pages: 58
Is Encrypted: false
Is Viewable (without pass): true