Print Content Streams
In this guide, the process of printing PDF content streams will be demonstrated.
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 in lines 10-19
, imports UniPDF packages and other necessary Go libraries needed for the program to run.
In lines 21-28
, the init
function is defined. This function loads the API key from the system environment and sets the license using icense.SetMeteredKey(os.Getenv(`UNIDOC_LICENSE_API_KEY`))
.
The main
function is defined in lines 30-54
. Lines 30-46
validate the value of the command line arguments.
In line 49
, the listContentStreams
function is used to print the content streams of the document.
The listContentStreams
function defined in lines 56-111
, gets the input file path and the target page number and prints the content stream of the page. First the PdfReader
is created using model.NewPdfReaderFromFile(inputPath, nil)
in line 57
. Line 63
gets the number of pages. The for loop in lines 71-108
iterates through each page and when it gets the target page, it prints the content stream of that page. If the target page number is not provided, then all the content streams of all pages is printed. In line 83
, The content stream is obtained from the page object using contentStreams, err := page.GetContentStreams()
. Then in lines 93-107
, the content streams and operations are prints as follows:
for _, cstream := range contentStreams {
pageContentStr += cstream
}
fmt.Printf("%s\n", pageContentStr)
cstreamParser := contentstream.NewContentStreamParser(pageContentStr)
operations, err := cstreamParser.Parse()
if err != nil {
return err
}
fmt.Printf("=== Full list\n")
for idx, op := range *operations {
fmt.Printf("Operation %d: %s - Params: %v\n", idx+1, op.Operand, op.Params)
}
Run the code
Run the code using the following command:
go run pdf_print_content_streams.go input.pdf [page]