Some test text!

Search
Hamburger Icon

Convert Office documents (Excel word PowerPoint) to PDF using Go

More languages

More languages
Java (Android)
C++
C#
C# (.NET Core)
Go
Java
Kotlin
Obj-C
JS (Node.js)
PHP
Python
Ruby
Swift
C# (UWP)
VB
C# (Xamarin)

Sample Go code for using PDFTron SDK to convert Office documents to PDF (including Word, Excel, PowerPoint and Publisher) without needing any external dependencies or MS Office licenses. Office to PDF conversion can be performed on a Linux or Windows server to automate Office-centric workflows, or entirely in the user's client (web browser, mobile device). The conversion functionality can be combined with our Viewer to display or annotate Office files (docx, xlsx, pptx) on all major platforms, including Web, Android, iOS, Xamarin, UWP, and Windows. Learn more about our Go PDF Library and Office Document Conversion Library.

Get Started Samples Download

To run this sample, get started with a free trial of Apryse SDK.

//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2021 by PDFTron Systems Inc. All Rights Reserved.
// Consult LICENSE.txt regarding license information.
//---------------------------------------------------------------------------------------

package main
import (
	"fmt"
	. "pdftron"
)

import  "pdftron/Samples/LicenseKey/GO"

//------------------------------------------------------------------------------
// The following sample illustrates how to use the PDF.Convert utility class 
// to convert MS Office files to PDF
//
// This conversion is performed entirely within the PDFNet and has *no* 
// external or system dependencies dependencies -- Conversion results will be
// the same whether on Windows, Linux or Android.
//
// Please contact us if you have any questions.
//------------------------------------------------------------------------------

// Relative path to the folder containing the test files.
var inputPath = "../../TestFiles/"
var outputPath = "../../TestFiles/Output/"

func SimpleDocxConvert(inputFileName string, outputFileName string){
	// Start with a PDFDoc (the conversion destination)
    pdfdoc := NewPDFDoc()

    // perform the conversion with no optional parameters
    ConvertOfficeToPDF(pdfdoc, inputPath + inputFileName, NewConversionOptions())

    // save the result
    pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))

    // And we're done!
    fmt.Println("Saved " + outputFileName )
}

func FlexibleDocxConvert(inputFileName string , outputFileName string){
    // Start with a PDFDoc (the conversion destination)
    pdfdoc :=  NewPDFDoc()

    options :=  NewOfficeToPDFOptions() 

    // set up smart font substitutions to improve conversion results
    // in situations where the original fonts are not available
    options.SetSmartSubstitutionPluginPath(inputPath)

    // create a conversion object -- this sets things up but does not yet
    // perform any conversion logic.
    // in a multithreaded environment, this object can be used to monitor
    // the conversion progress and potentially cancel it as well
    conversion := ConvertStreamingPDFConversion(pdfdoc, inputPath + inputFileName, options)

    // Print the progress of the conversion.
    // print( "Status: " + str(conversion.GetProgress()*100) +"%, " +
    //        conversion.GetProgressLabel())

    // actually perform the conversion
    // this particular method will not throw on conversion failure, but will
    // return an error status instead
	for {
		if (conversion.GetConversionStatus() != DocumentConversionEIncomplete){
			break
		}
		conversion.ConvertNextPage()
		// print out the progress status as we go
		// print("Status: " + str(conversion.GetProgress()*100) + "%, " +
		//     conversion.GetProgressLabel() )
	}

    if(conversion.GetConversionStatus() == DocumentConversionESuccess){
        numWarnings := conversion.GetNumWarnings()
        // print information about the conversion
        for i := uint(0); i < numWarnings; i++ {
            fmt.Println("Conversion Warning: " + conversion.GetWarningString(i) )
            i = i + 1
		}
        // save the result
        pdfdoc.Save(outputPath + outputFileName, uint(SDFDocE_linearized))
        // done
        fmt.Println("Saved " + outputFileName )
	}else{
        fmt.Println("Encountered an error during conversion: " + conversion.GetErrorString() )
	}
}

func main(){
    // The first step in every application using PDFNet is to initialize the 
    // library. The library is usually initialized only once, but calling 
    // Initialize() multiple times is also fine.
    PDFNetInitialize(PDFTronLicense.Key)
    PDFNetSetResourcesPath("../../Resources")

    // first the one-line conversion function
    SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007.pdf")

    // then the more flexible line-by-line conversion API
    FlexibleDocxConvert("the_rime_of_the_ancient_mariner.docx", "the_rime_of_the_ancient_mariner.pdf")
    PDFNetTerminate()

}