Print PDFs - Go Sample Code

Sample code for using Apryse SDK to print a PDF file using the currently selected default printer; provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB. It is possible to use this printing functionality in both client and server applications without dependence on any third party components. Learn more about our Server SDK.

1//---------------------------------------------------------------------------------------
2// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
3// Consult LICENSE.txt regarding license information.
4//---------------------------------------------------------------------------------------
5
6package main
7import (
8 . "pdftron"
9)
10
11import "pdftron/Samples/LicenseKey/GO"
12
13// The following sample illustrates how to print PDF document using currently selected
14// default printer.
15//
16// The first example uses the new PDF::Print::StartPrintJob function to send a rasterization
17// of the document with optimal compression to the printer. If the OS is Windows 7, then the
18// XPS print path will be used to preserve vector quality. For earlier Windows versions
19// the GDI print path will be used. On other operating systems this will be a no-op
20//
21// The second example uses PDFDraw send unoptimized rasterized data via awt.print API.
22//
23// If you would like to rasterize page at high resolutions (e.g. more than 600 DPI), you
24// should use PDFRasterizer or PDFNet vector output instead of PDFDraw.
25
26func main(){
27 PDFNetInitialize(PDFTronLicense.Key)
28
29 // Relative path to the folder containing the test files.
30 inputPath := "../../TestFiles/"
31
32 doc := NewPDFDoc(inputPath + "tiger.pdf")
33 doc.InitSecurityHandler()
34
35 // Set our PrinterMode options
36 printerMode := NewPrinterMode()
37 printerMode.SetCollation(true)
38 printerMode.SetCopyCount(1)
39 printerMode.SetDPI(100); // regardless of ordering, an explicit DPI setting overrides the OutputQuality setting
40 printerMode.SetDuplexing(PrinterModeE_Duplex_Auto)
41
42 // If the XPS print path is being used, then the printer spooler file will
43 // ignore the grayscale option and be in full color
44 printerMode.SetOutputColor(PrinterModeE_OutputColor_Grayscale)
45 printerMode.SetOutputQuality(PrinterModeE_OutputQuality_Medium)
46 // printerMode.SetNUp(2,1)
47 // printerMode.SetScaleType(PrinterModeE_ScaleType_FitToOutPage)
48
49 // Print the PDF document to the default printer, using "tiger.pdf" as the document
50 // name, send the file to the printer not to an output file, print all pages, set the printerMode
51 // and a cancel flag to true
52 pageSet := NewPageSet(1, doc.GetPageCount())
53 boolValue := true
54 PrintStartPrintJob(doc, "", doc.GetFileName(), "", pageSet, printerMode, &boolValue)
55 PDFNetTerminate()
56}

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales