Imports System.Drawing.Printing
Imports
System.Runtime.InteropServices
Public Class Page
<Runtime.InteropServices.DllImport("winspool.Drv",
EntryPoint:="OpenPrinterW", _
SetLastError:=True,
CharSet:=CharSet.Unicode, _
ExactSpelling:=True,
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function
OpenPrinter(ByVal src As String, ByRef hPrinter As IntPtr, ByVal pd As Long) As
Boolean
End Function
<System.Runtime.InteropServices.DllImportAttribute("winspool.drv",
SetLastError:=True)> _
Public Shared Function ClosePrinter(ByVal
phPrinter As IntPtr) As Integer
End Function
<DllImport("winspool.drv")> _
Public Shared Function
SetPrinter(ByVal hPrinter As IntPtr, ByVal level As Integer, ByVal lpBytes As
IntPtr, ByVal command As Integer) As Boolean
End Function
Public Shared Function Setting(ByVal PageName As String) As Boolean
Try
Dim printerSettings As PrinterSettings
Dim
document As New PrintDocument()
printerSettings =
document.PrinterSettings
Dim hPrinter As IntPtr
OpenPrinter(printerSettings.PrinterName, hPrinter, 0)
Dim p
As PaperSize = Nothing
Dim ps As PaperSize
For
Each ps In document.PrinterSettings.PaperSizes
If
ps.PaperName.Equals(PageName) Then
p =
ps
End If
Next
document.DefaultPageSettings.PaperSize = p
Dim hDevMode As
IntPtr =
printerSettings.GetHdevmode(document.DefaultPageSettings)
Dim ret
As Boolean = SetPrinter(hPrinter, 9, hDevMode, 0)
ClosePrinter(hPrinter)
Return True
Catch ex As
Exception
End Try
End Function
End Class