DOCX, XSLX to PDF Conversion - OfficeToPDF

Sample code for using Apryse Server 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. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby, Go and VB.

Learn more about our Server SDK and Office Document Conversion Library.

1'
2' Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
3'
4
5Imports System
6
7Imports pdftron
8Imports pdftron.Common
9Imports pdftron.Filters
10Imports pdftron.SDF
11Imports pdftron.PDF
12
13
14' The following sample illustrates how to use the PDF::Convert utility class to convert
15' .docx files to PDF
16'
17' This conversion is performed entirely within the PDFNet and has *no* external or
18' system dependencies dependencies
19'
20' Please contact us if you have any questions.
21Module OfficeToPDFTestVB
22 Dim pdfNetLoader As PDFNetLoader
23 Sub New()
24 pdfNetLoader = pdftron.PDFNetLoader.Instance()
25 End Sub
26
27 ' Relative path to the folder containing test files.
28 Dim input_path As String = "../../../../TestFiles/"
29 Dim output_path As String = "../../../../TestFiles/Output/"
30
31 Private Sub SimpleConvert(ByVal input_filename As String, ByVal output_filename As String)
32 ' Start with a PDFDoc (the conversion destination)
33 Using pdfdoc As PDFDoc = New PDFDoc
34
35 ' perform the conversion with no optional parameters
36 pdftron.PDF.Convert.OfficeToPDF(pdfdoc, input_path + input_filename, Nothing)
37
38 ' save the result
39 pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized)
40
41 ' And we're done!
42 Console.WriteLine("Saved " + (output_path + output_filename))
43 End Using
44 End Sub
45
46 Private Sub FlexibleConvert(ByVal input_filename As String, ByVal output_filename As String)
47 ' Start with a PDFDoc (the conversion destination)
48 Using pdfdoc As PDFDoc = New PDFDoc
49 Dim options As OfficeToPDFOptions = New OfficeToPDFOptions
50 options.SetResourceDocPath("SomePath")
51 ' perform the conversion with no optional parameters
52 Using conversion As DocumentConversion = pdftron.PDF.Convert.StreamingPDFConversion(pdfdoc, input_path + input_filename, options)
53
54 If conversion.TryConvert() = DocumentConversionResult.e_document_conversion_success Then
55 Dim num_warnings As Integer = conversion.GetNumWarnings()
56 For i As Integer = 0 To num_warnings - 1
57 Console.WriteLine("Warning: " + conversion.GetWarningString(i))
58 Next i
59
60 ' save the result
61 pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized)
62
63 ' And we're done!
64 Console.WriteLine("Saved " + (output_path + output_filename))
65 Else
66 Console.WriteLine("Error: " + conversion.GetErrorString())
67 End If
68 End Using
69 End Using
70 End Sub
71
72
73
74 Sub Main()
75
76 PDFNet.Initialize(PDFTronLicense.Key)
77
78 Try
79 ' first the one-line conversion method
80 SimpleConvert("Fishermen.docx", "Fishermen.pdf")
81
82 ' Then the more flexible conversion process
83 FlexibleConvert("simple-word_2007.docx", "simple-word_2007_b.pdf")
84
85 ' conversion of RTL content
86 FlexibleConvert("factsheet_Arabic.docx", "factsheet_Arabic.pdf")
87
88 Catch ex As PDFNetException
89
90 Console.WriteLine(ex.Message)
91
92 Catch ex As Exception
93
94 MsgBox(ex.Message)
95
96 End Try
97
98 PDFNet.Terminate()
99
100 End Sub
101
102End Module

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales