< Windows samples

WordToPDFTest - VB

These samples illustrate how to use the pdftron.PDF.Convert utility class to convert DOCX files to PDF. This conversion is performed entirely within PDFNet and has no external or system dependencies dependencies. Conversion results will be the same on all platforms.

'
' Copyright (c) 2001-2018 by PDFTron Systems Inc. All Rights Reserved.
'

Imports System

Imports pdftron
Imports pdftron.Common
Imports pdftron.Filters
Imports pdftron.SDF
Imports pdftron.PDF


' The following sample illustrates how to use the PDF::Convert utility class to convert 
' .docx files to PDF
'
' This conversion is performed entirely within the PDFNet and has *no* external or
' system dependencies dependencies 
'
' Please contact us if you have any questions.	
Module WordToPDFTestVB
    Dim pdfNetLoader As PDFNetLoader = pdftron.PDFNetLoader.Instance()

    ' Relative path to the folder containing test files.
    Dim input_path As String = "../../../../TestFiles/"
    Dim output_path As String = "../../../../TestFiles/Output/"

    Private Sub SimpleDocxConvert(ByVal input_filename As String, ByVal output_filename As String)
        ' Start with a PDFDoc (the conversion destination)
        Using pdfdoc As PDFDoc = New PDFDoc

            ' perform the conversion with no optional parameters
            pdftron.PDF.Convert.WordToPDF(pdfdoc, input_path + input_filename, Nothing)

            ' save the result
            pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized)

            ' And we're done!
            Console.WriteLine("Saved " + (output_path + output_filename))
        End Using
    End Sub

    Private Sub FlexibleDocxConvert(ByVal input_filename As String, ByVal output_filename As String)
        ' Start with a PDFDoc (the conversion destination)
        Using pdfdoc As PDFDoc = New PDFDoc
            Dim options As WordToPDFOptions = New WordToPDFOptions
            options.SetResourceDocPath("SomePath")
            ' perform the conversion with no optional parameters
            Using conversion As DocumentConversion = pdftron.PDF.Convert.WordToPDFConversion(pdfdoc, input_path + input_filename, options)

                If conversion.TryConvert() = DocumentConversionResult.e_document_conversion_success Then
                    Dim num_warnings As Integer = conversion.GetNumWarnings()
                    For i As Integer = 0 To num_warnings - 1
                        Console.WriteLine("Warning: " + conversion.GetWarningString(i))
                    Next i

                    ' save the result
                    pdfdoc.Save(output_path + output_filename, SDFDoc.SaveOptions.e_linearized)

                    ' And we're done!
                    Console.WriteLine("Saved " + (output_path + output_filename))
                Else
                    Console.WriteLine("Error: " + conversion.GetErrorString())
                End If
            End Using
        End Using
    End Sub



    Sub Main()

        PDFNet.Initialize()

        Try

            ' first the one-line conversion method
            SimpleDocxConvert("simple-word_2007.docx", "simple-word_2007_a.pdf")

            ' Then the more flexible conversion process
            FlexibleDocxConvert("simple-word_2007.docx", "simple-word_2007_b.pdf")

        Catch ex As PDFNetException

            Console.WriteLine(ex.Message)

        Catch ex As Exception

            MsgBox(ex.Message)

        End Try

        PDFNet.Terminate()

    End Sub

End Module