Last active
November 9, 2024 01:46
-
-
Save KOZ60/3fab97d2b01a5d2b8346462a0e1d3c34 to your computer and use it in GitHub Desktop.
How to add custom paper to windows system
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Imports System.Runtime.InteropServices | |
Module Module1 | |
Sub Main() | |
AddCustomPaperSize(Nothing, "Custom Paper Sample", FormFlags.Printer, 10000, 10000, 9000, 9000) | |
End Sub | |
Public Enum FormFlags | |
User | |
Buitin | |
Printer | |
End Enum | |
Public Sub AddCustomPaperSize(printerName As String, formName As String, | |
flags As FormFlags, | |
width As Integer, height As Integer, | |
imageWidth As Integer, imageHeight As Integer) | |
Dim hPrinter As IntPtr | |
If OpenPrinter(printerName, hPrinter, IntPtr.Zero) Then | |
Dim formInfo As New FORM_INFO_1() | |
formInfo.Flags = flags | |
formInfo.pName = formName | |
formInfo.Size = New SIZEL() With {.cx = width, .cy = height} | |
formInfo.ImageableArea = New RECTL() With { | |
.left = 0, .top = 0, | |
.right = imageWidth, .bottom = imageHeight | |
} | |
If Not AddForm(hPrinter, 1, formInfo) Then | |
Console.WriteLine("Failed to add form.") | |
End If | |
ClosePrinter(hPrinter) | |
Else | |
Console.WriteLine("Failed to open printer.") | |
End If | |
End Sub | |
<DllImport("winspool.drv", SetLastError:=True, CharSet:=CharSet.Auto)> | |
Private Function AddForm(hPrinter As IntPtr, level As Integer, ByRef form As FORM_INFO_1) As Boolean | |
End Function | |
<DllImport("winspool.drv", SetLastError:=True, CharSet:=CharSet.Auto)> | |
Private Function OpenPrinter(pPrinterName As String, ByRef phPrinter As IntPtr, pDefault As IntPtr) As Boolean | |
End Function | |
<DllImport("winspool.drv", SetLastError:=True, CharSet:=CharSet.Auto)> | |
Private Function ClosePrinter(hPrinter As IntPtr) As Boolean | |
End Function | |
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> | |
Private Structure FORM_INFO_1 | |
Public Flags As FormFlags | |
<MarshalAs(UnmanagedType.LPTStr)> | |
Public pName As String | |
Public Size As SIZEL | |
Public ImageableArea As RECTL | |
End Structure | |
<StructLayout(LayoutKind.Sequential)> | |
Private Structure SIZEL | |
Public cx As Integer | |
Public cy As Integer | |
End Structure | |
<StructLayout(LayoutKind.Sequential)> | |
Private Structure RECTL | |
Public left As Integer | |
Public top As Integer | |
Public right As Integer | |
Public bottom As Integer | |
End Structure | |
End Module |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment