Get and Set Print Options Example (VB.NET)

This example shows how to get and set print options.

'--------------------------------------------------------------
' Preconditions:
' 1. Create a VB.NET Windows console project.
' 2. Copy and paste this example into the VB.NET IDE.
' 3. Add a reference to:
'    install_dir\APISDK\tlb\DraftSight.Interop.dsAutomation.dll.
' 4. Start DraftSight and open a document.
' 5. Start debugging the project.
'
' Postconditions: Message boxes pop up if setting any printer
' option fails for the specified printer. Read the text in each
' message box before clicking OK to close it.
'----------------------------------------------------------------
Imports DraftSight.Interop.dsAutomation
Module Module1
    Sub Main()
        Dim dsApp As Application
        Dim dsDoc As Document
        'Connect to DraftSight
        dsApp = GetObject(, "DraftSight.Application")
	dsApp.AbortRunningCommand() ' abort any command currently running in DraftSight to avoid nested commands
        'Get active document
        dsDoc = dsApp.GetActiveDocument()
        If Not dsDoc Is Nothing Then
            'Get and set printing options
            GetSetPrintOptions(dsApp)
        End If
    End Sub
    Sub GetSetPrintOptions(ByVal dsApp As Application)
        Dim dsPrintMgr As PrintManager
        Dim printerName As String
        Dim dsVarPaperSizes As Object
        Dim paperLength As Double
        Dim paperWidth As Double
        Dim rightLength As Double
        Dim rightWidth As Double
        Dim paperSize As String
        Dim precision As Double
        Dim xOffset As Double
        Dim yOffset As Double
        Dim printOrientation As dsPrintOrientation_e
        Dim centerPrint As Boolean
        Dim printQuality As Long
        Dim scaleLineWeight As Boolean
        Dim scaleToFit As Boolean
        Dim styleTable As String
        Dim userScale As Boolean
        Dim viewDisplayStyle As dsViewDisplayStyle_e
        'Get PrintManager
        dsPrintMgr = dsApp.GetPrintManager
        If Not dsPrintMgr Is Nothing Then
            'Get printer name
            printerName = "JPG"
            dsPrintMgr.Printer = printerName
            If printerName = "" Then
                MsgBox("Failed to set IPrintManager.Printer property " & printerName & " value.")
            End If
            'Get available paper sizes
            dsVarPaperSizes = dsPrintMgr.AvailablePaperSizes
            If IsArray(dsVarPaperSizes) And UBound(dsVarPaperSizes) = 0 Then
                MsgBox("List of available paper sizes is empty for " & dsPrintMgr.Printer & " printer.")
            End If
            'Set paper size to "UserDefinedRaster (87.00 x 134.00Pixels)"
            paperSize = "UserDefinedRaster (87.00 x 134.00Pixels)"
            dsPrintMgr.paperSize = paperSize
            If paperSize <> dsPrintMgr.paperSize Then
                MsgBox("Failed to set IPrintManager.PaperSize property to " & paperSize & " value.")
            End If
            'Get paper size
            rightLength = 134
            rightWidth = 87
            dsPrintMgr.GetPaperSize(paperLength, paperWidth)
            'Verify paper length value
            precision = 0.000000001
            If Math.Abs(rightLength - paperLength) > precision Then
                MsgBox("IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size.")
            End If
            'Verify paper width value
            If Math.Abs(rightWidth - paperWidth) > precision Then
                MsgBox("IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size.")
            End If
            'Set paper size to "VGA_(640.00_x_480.00_Pixels)"
            paperSize = "VGA_(640.00_x_480.00_Pixels)"
            dsPrintMgr.paperSize = paperSize
            If paperSize <> dsPrintMgr.paperSize Then
                MsgBox("Failed to set IPrintManager.PaperSize property to " & paperSize & " value.")
            End If
            'Get paper size
            rightLength = 480
            rightWidth = 640
            dsPrintMgr.GetPaperSize(paperLength, paperWidth)
            'Verify paper length value
            If Math.Abs(rightLength - paperLength) > precision Then
                MsgBox("IPrintManager.GetPaperSize method returns wrong paper length for '" & paperSize & "' paper size.")
            End If
            'Verify paper width value
            If Math.Abs(rightWidth - paperWidth) > precision Then
                MsgBox("IPrintManager.GetPaperSize method returns wrong paper width for '" & paperSize & "' paper size.")
            End If
            'Get print margins
            Dim top As Double
            Dim bottom As Double
            Dim left As Double
            Dim right As Double
            dsPrintMgr.GetPrintMargins(top, bottom, left, right)
            'Set print offset
            xOffset = 5.0#
            yOffset = 10.0#
            dsPrintMgr.SetPrintOffset(xOffset, yOffset)
            'Get print margins
            Dim resultXOffset As Double
            Dim resultYOffset As Double
            dsPrintMgr.GetPrintOffset(resultXOffset, resultYOffset)
            'Verify print offset values
            If Math.Abs(resultXOffset - xOffset) > precision Then
                MsgBox("IPrintManager.GetPrintOffset method returns wrong XOffset value. It should be " & xOffset & ", but it is " & resultXOffset & ".")
            End If
            If Math.Abs(resultYOffset - yOffset) > precision Then
                MsgBox("IPrintManager.GetPrintOffset method returns wrong YOffset value. It should be " & yOffset & ", but it is " & resultYOffset & ".")
            End If
            'Set print orientation
            printOrientation = dsPrintOrientation_e.dsPrintOrientation_Landscape
            dsPrintMgr.Orientation = printOrientation
            'Verify if print orientation was set
            If printOrientation <> dsPrintMgr.Orientation Then
                MsgBox("Failed to set IPrintManager.Orientation property. It should be " & printOrientation & ", but it is " & dsPrintMgr.Orientation & ".")
            End If
            'Set PrintOnCenter property
            centerPrint = False
            dsPrintMgr.PrintOnCenter = centerPrint
            If centerPrint <> dsPrintMgr.PrintOnCenter Then
                MsgBox("Failed to set IPrintManager.PrintOnCenter property to " & centerPrint)
            End If
            'Set print quality
            printQuality = 300
            dsPrintMgr.Quality = printQuality
            'Set ScaleLineWeight property
            scaleLineWeight = False
            dsPrintMgr.scaleLineWeight = scaleLineWeight
            If scaleLineWeight <> dsPrintMgr.scaleLineWeight Then
                MsgBox("Failed to set IPrintManager.ScaleLineWeight property.")
            End If
            'Set ScaleToFit property
            scaleToFit = False
            dsPrintMgr.scaleToFit = scaleToFit
            If scaleToFit <> dsPrintMgr.scaleToFit Then
                MsgBox("Failed to set IPrintManager.ScaleToFit property to " & scaleToFit)
            End If
            'Set StyleTable property
            styleTable = "default.ctb"
            dsPrintMgr.styleTable = styleTable
            If styleTable <> dsPrintMgr.styleTable Then
                MsgBox("Failed to set IPrintManager.StyleTable property to " & styleTable)
            End If
            'Set UserScale property
            userScale = False
            dsPrintMgr.userScale = userScale
            'Set ViewDisplayStyle property
            viewDisplayStyle = dsViewDisplayStyle_e.dsViewDisplayStyle_Rendered
            dsPrintMgr.viewDisplayStyle = viewDisplayStyle
            If viewDisplayStyle <> dsPrintMgr.viewDisplayStyle Then
                MsgBox("Failed to set IPrintManager.ViewDisplayStyle property to " & viewDisplayStyle)
            End If
        Else
            MsgBox("IDocument.GetPrintManager returns Nothing.")
        End If
    End Sub
End Module