How to add custom paper to windows system

Andrzej 20 Reputation points
2024-11-08T19:08:10.8966667+00:00

I'm looking for help with programmically (VB.NET) adding a custom paper size to window system (not a printer).

There are plenty of solutions showing how to define custom paper for printing. But none about adding custom paper to system.

Windows have a "printing manager" (printmanagement.msc). With this tool I can add a custom paper (form) to system - without Administrative privileges.

I'd like to the same what "printmanagement.msc" does, but using VB.NET

My app needs just to add/remove custom paper to system, nothing more. No document printing will be done using this app.

Can anyone help me?

regards,

Andrzej

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,924 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,740 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. KOZ6.0 6,510 Reputation points
    2024-11-09T01:27:39.5066667+00:00

    Try this code.

    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
    

  2. Andrzej 20 Reputation points
    2024-11-09T19:09:17.9766667+00:00

    I've found it!!

    The "type" of added form is set by "FormFlags".

    AddCustomPaperSize(Nothing, 
                               "Custom Paper Sample",
                               FormFlags.Printer,
                               10000, 10000, 
                               9000, 9000)
    

    So changing the line "FormFlags.Printer" is the solution:

    • 0: "user defined"
    • 1: "built-in"
    • 2: "printer"

    Now it works!!!

    Thanks a lot!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.