שתף באמצעות


Print using custom Paper Size...

Question

Sunday, August 12, 2007 5:31 PM

Dear All
I am facing a problem in printing document using custom paper sizes,
i created a new paper size thru code and try to use the same paper size for printing, in preview it shows exactly [ giving output in same pape size ], but while printing the paper size is resetting to A4 / letter.
How can i solve this ...

i am using vb.dot net 2005.

i read somewhere that it is possible thru Winspooler API, but i dont know how to use that.

All replies (26)

Wednesday, August 15, 2007 8:12 AM ✅Answered

MahroofNM,

 

I hope the following code snippet and thread can help you with the custom page size problem:

 

Code Snippet

    Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

        Dim prn As New Printing.PrintDocument

        Dim psz As New Printing.PaperSize

        ' Select the PDF Printer

        prn.PrinterSettings.PrinterName = "pdf995"

        ' Ensure we have LETTER Sized Paper

        psz.RawKind = Printing.PaperKind.Custom

        psz.Width = 350

        psz.Height = 350

        ' Set the paper size

        prn.DefaultPageSettings.PaperSize = psz

        Debug.WriteLine(prn.DefaultPageSettings.Bounds)

        Debug.WriteLine(prn.DefaultPageSettings.PrintableArea.Size)

        ' Handle the page events

        AddHandler prn.PrintPage, AddressOf Me.PrintFormHandler

        ' Do the print (Printing handled by the print page handler)

        prn.Print()

        ' Remove the page handler

        RemoveHandler prn.PrintPage, AddressOf Me.PrintFormHandler

    End Sub

 

 

Setting page size in code

 

Code Snippet

        Dim dlg As New PageSetupDialog

        With dlg

            .EnableMetric = True

            .AllowMargins = False

            .Document = poprintdoc

            .ShowDialog()

        End With

        With poprintdoc.DefaultPageSettings

            'Pageheight = CInt(.PaperSize.Height / 100 * 25.4)

            Pagewidth = CInt(.PaperSize.Width / 100 * 25.4)

            paper = .PaperSize.Kind.ToString

            Me.lblSize.Text = CStr(Pagewidth & " x " & Pageheight)

            Me.lblPaper.Text = paper

        End With

 

 


Wednesday, August 15, 2007 9:45 AM ✅Answered

Every place I check the paper size in the following code the size is the same.

Code Snippet

Imports System.Runtime.InteropServices

Imports System.Drawing.Printing

Public Class Form1

Private Psze As PaperSize

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

PrintDocument1.DefaultPageSettings.PaperSize = New PaperSize("Custom", 350, 350)

PrintPreviewDialog1.ShowDialog()

End Sub

Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

PageSetupDialog1.ShowDialog()

Psze = PageSetupDialog1.PageSettings.PaperSize

End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

PrintDialog1.ShowDialog()

Psze = PrintDialog1.PrinterSettings.DefaultPageSettings.PaperSize

End Sub

End Class

In the PrintDocument1_PrintPage event the paper size remains the same for both Preview and Print. If I show a dialog (PageSetup or PrintDialog) the paper size is shown as Letter, but when I check the size in the above Button Click events the size is Custom.


Friday, August 17, 2007 5:50 PM ✅Answered

Add this line in your doPageSettingsNew routine:

PrintingDocument.PrinterSettings.DefaultPageSettings.PaperSize = PrintingDocument.DefaultPageSettings.PaperSize

after you set the PrintingDocument.DefaultPageSettings.PaperSize.

And verify that the PaperSize is correct in the QueryPageSettings event.


Tuesday, August 28, 2007 9:09 AM ✅Answered

Could you start a new Windows Application and replace the Form1 code with:

Code Snippet

Imports System.Drawing.Printing

Public Class Form1

Private WithEvents PPDlg As New PrintPreviewDialog

Private WithEvents PDoc As New PrintDocument

Private WithEvents Tmr As New Timer

Private TB As New TextBox

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

TB.Multiline = True

TB.AcceptsReturn = True

TB.AcceptsTab = True

TB.Width = Width - 8

TB.Height = Height - 28

Controls.Add(TB)

PPDlg.Document = PDoc

PDoc.DefaultPageSettings.PaperSize = New PaperSize("Custom", 350, 350)

PDoc.PrinterSettings.DefaultPageSettings.PaperSize = PDoc.DefaultPageSettings.PaperSize

Tmr.Tag = "Hide PPDlg"

PPDlg.Show()

End Sub

Private Sub PDoc_EndPrint(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintEventArgs) Handles PDoc.EndPrint

Tmr.Start()

End Sub

Private Sub PDoc_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PDoc.PrintPage

With PDoc.DefaultPageSettings.PaperSize

TB.AppendText(.PaperName & vbTab & .Width & vbTab & .Height & vbNewLine)

End With

With PDoc.PrinterSettings.DefaultPageSettings.PaperSize

TB.AppendText(.PaperName & vbTab & .Width & vbTab & .Height & vbNewLine)

End With

End Sub

Private Sub Tmr_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tmr.Tick

Tmr.Stop()

If Tmr.Tag.ToString = "Hide PPDlg" Then

PPDlg.Hide()

Tmr.Tag = ""

PDoc.Print()

End If

End Sub

End Class

 

 

Run the program, copy the text in the textbox and post it here.


Friday, August 17, 2007 1:20 PM

Thanks for your reply, But still i am getting the output same as bofore. Please find below my code and also please note that i am using HP Laser Jet 1018.

 

 Private WithEvents PrintingDocument As New Printing.PrintDocument

 

 Public Sub getPrit(ByVal templatID As String, ByVal ChqNo As Integer)
        Try
        
            doPageSettingsNew()
          
        Catch ex As Exception

            MsgBox(ex.Message)
        End Try
  End Sub

 Public Sub doPageSettingsNew()
        Try
            Dim psz As New Printing.PaperSize
        
            PrintingDocument.PrinterSettings.PrinterName = "MyPrinterName"
            psz.RawKind = Printing.PaperKind.Custom
            psz.Width = ObjTemplateMaster.PaperWidth
            psz.Width = ObjTemplateMaster.Paperheight
            PrintingDocument.DefaultPageSettings.PaperSize = psz
            PrintingDocument.Print()

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
  End Sub

 

  Private Sub PrintingDocument_PrintPage(ByVal sender As Object, _
        ByVal e As System.Drawing.Printing.PrintPageEventArgs) _
             Handles PrintingDocument.PrintPage
          doFeildsPrinting(e)
   End Sub

 Public Sub doFeildsPrinting(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Try

                 Dim ImageToPrint As Bitmap
                 ImageToPrint = System.Drawing.Image.FromFile(ImgName)
             
                    e.Graphics.DrawImage(ImageToPrint, 1, 1)           
           
                    e.Graphics.DrawString("Column1", objFont, Brushes.Black, Column1_X, Column1_Y)
                    e.Graphics.DrawString("Column2", objFont, Brushes.Black, Column2_X, Column2_Y)

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub


Friday, August 17, 2007 2:40 PM

Where does the paper size change?  Is the paper size correct in the BeginPrint event?


Wednesday, August 22, 2007 4:40 PM

Still it print the same way, even the page size at "QueryPageSettingsEventArgs" seems to be same..

i havnt set the margins, is it affect the printing....

 

is there any shortcuts to acheive this ...as i am already delayed to submit this project.


Thursday, August 23, 2007 8:14 AM

Where is the paper size correct and where does it change?  Or just set it in the QueryPageSettingEvent and make sure it is correct in the PrintPage event.


Friday, August 24, 2007 6:04 AM

Pagesize remain same in PrintPage event too.

 


Friday, August 24, 2007 6:40 AM

So your problem is solved?  I don't know why you could only set the paper size in the QueryPageSettings event.  You should be able to set it anywhere before the PrintPage event.


Tuesday, August 28, 2007 4:58 AM

My Problem is not yet solved, still it gives me output in A4 sheet. But i am sure my printer support the custom pages,because when i print using custom pages thru MS word it giving me correct output.


Sunday, September 2, 2007 3:27 AM

Thanks ...

Finally i think this helps me.

PDoc.PrinterSettings.DefaultPageSettings.PaperSize = PDoc.DefaultPageSettings.PaperSize

Now it is working properly, only thing is i have to keep paper to the left side of tray,i hope that may be because of margin settings.

Here the problem is Paper Sensor of my printer tray is in middle, so if i want to print in a small paper, it can't detect the paper from this tray, But if i use MSWord with the same paper size it print exactly when i keep paper in middle of the tray with the feeder adjuster.

 


Sunday, January 16, 2011 1:12 PM

Hai, 

I am also facing the same problem like Mahroof,  I use all these technique it is not printing printing only in A4 or letter which is default, in printer there is a message "paper detected does not match paper size or type selected", If I insert A4 then its  printing ....

 

but it is working if I create one paper in printer driver and use the following code

 

For i As Integer = 0 To PrintDocument1.PrinterSettings.PaperSizes.Count - 1
   If PrintDocument1.PrinterSettings.PaperSizes(i).Width = 386 And PrintDocument1.PrinterSettings.PaperSizes(i).Height = 813 Then
    PrintDocument1.DefaultPageSettings.PaperSize = PrintDocument1.PrinterSettings.PaperSizes(i)
    Exit For
   End If
  Next

 

Why like this????????

Please help me....


Sunday, January 16, 2011 3:16 PM

You may only change the size of the "Custom" paper. 

Setting it is quite easy:

    Dim PS As New PaperSize("Custom", DesiredWidth, DesiredHeight)
    PrintDocument1.DefaultPageSettings.PaperSize = PS


Sunday, January 16, 2011 4:10 PM

Thank u John,

 

I am doing like ,

Dim ps As New PaperSize("Custom", 386, 813)

 PrintDocument1.DefaultPageSettings.PaperSize = ps

        PrintDocument1.PrinterSettings.DefaultPageSettings.PaperSize = PrintDocument1.DefaultPageSettings.PaperSize

        PrintDocument1.Print()

 

This is working with pdf printer,

But HP 2600 Photosmart, Samsung Laser Jet is not working with this settings

On preview is OK but when i am printing it is printing only A4 and there is a message in printer "Paper detected is does not match the size or type"

Have any problem related to printer???

 

 


Sunday, January 16, 2011 4:23 PM

Are you wanting the printer to actually change paper?  For that you have to change the PaperSource.


Sunday, January 16, 2011 4:29 PM

Thanks again

Actually I want to make an application for cheque printing, there is different size checks.

 

How I can use PaperSource for this purpose

Kindly Explain...


Sunday, January 16, 2011 4:32 PM

Thanks again

Actually I want to make an application for cheque printing, there is different size checks.

 

How I can use PaperSource for this purpose

Kindly Explain...

Select the tray containing the check you want to print.  Since your question has nothing to do with the subject of this thread, you should start a new thread.


Sunday, January 16, 2011 4:41 PM

The same tray using there is only one tray in printer, My problem is same as the Thread starter

( i created a new paper size thru code and try to use the same paper size for printing, in preview it shows exactly [ giving output in same paper size ], but while printing the paper size is resetting to A4 / letter.)

I am using VB.Net 2005, 

HP 2600 Photosmart Printer

 

 


Sunday, January 16, 2011 4:45 PM

( i created a new paper size thru code and try to use the same paper size for printing, in preview it shows exactly [ giving output in same paper size ], but while printing the paper size is resetting to A4 / letter.)

How are you determining this? 


Monday, January 17, 2011 5:39 AM

I am using PrintPreviewDialog for preview printdocument,  by this it show correct size and when I check the papersize in begin print and endprint the papersize width and height is correct when printing the message is "Paper detected is does not match the size or type"


Monday, January 17, 2011 7:44 AM

Could you paste this code into a new WF application, 
run it, copy the contents of the textbox and paste 
into a post on this thread?

Imports System.Drawing.Printing
Public Class Form1
  Dim WithEvents Print As New Button
  Dim WithEvents PDoc As New PrintDocument
  Dim PPDlg As New PrintPreviewDialog
  Dim TB As New TextBox
  Sub New()
    InitializeComponent()
    PPDlg.Document = PDoc
    Print.Text = "Print"
    Print.Parent = Me
    TB.Multiline = True
    TB.Size = Me.ClientSize
    TB.Top = Print.Bottom
    TB.Parent = Me
  End Sub
  Private Sub Print_Click(ByVal sender As Object, _
                          ByVal e As EventArgs) Handles Print.Click
    PDoc.DefaultPageSettings.PaperSize = New PaperSize("Custom", 386, 813)
    PPDlg.ShowDialog()
    PDoc.Print()
  End Sub
  Private Sub PDoc_PrintPage(ByVal sender As Object, _
                             ByVal e As PrintPageEventArgs) Handles PDoc.PrintPage
    TB.AppendText(e.PageSettings.ToString + _
                  Environment.NewLine + _
                  Environment.NewLine)
    PPDlg.Close()
    e.Cancel = True
  End Sub
End Class


Monday, January 17, 2011 8:39 AM

 

 

[PageSettings: Color=True, Landscape=False, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Custom Kind=Custom Height=813 Width=386], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]

 

[PageSettings: Color=True, Landscape=False, Margins=[Margins Left=100 Right=100 Top=100 Bottom=100], PaperSize=[PaperSize Custom Kind=Custom Height=813 Width=386], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]

 

 

Now I change printer HP Deskjet D1663

 

When I am Using the following code for setting papersize

 

 

For i As Integer = 0 To PrnDoc.PrinterSettings.PaperSizes.Count - 1
      If PrnDoc.PrinterSettings.PaperSizes.Item(i).Width = width AndAlso PrnDoc.PrinterSettings.PaperSizes(i).Height = height Then
        PrnDoc.DefaultPageSettings.PaperSize = PrnDoc.PrinterSettings.PaperSizes.Item(i)
      End If
    Next

With PrnDoc.DefaultPageSettings
        .Margins.Bottom = 0
        .Margins.Top = 0
        .Margins.Left = 0
        .Margins.Right = 0
      End With

the result of e.Pagesettings.ToString is like;

"[PageSettings: Color=True, Landscape=True, Margins=[Margins Left=0 Right=0 Top=0 Bottom=0], PaperSize=[PaperSize cheque1 Kind=Custom Height=813 Width=386], PaperSource=[PaperSource Automatically Select Kind=FormSource], PrinterResolution=[PrinterResolution X=600 Y=600]]"

 

 

the difference is only PaperSize Cheque1 Kind

 

Thanks in advance for your Support and replay

 


Monday, January 17, 2011 8:52 AM

Remove or comment the e.Cancel = True statement in the PrintPage event and run the program again.  Do you get an error message?


Monday, January 17, 2011 9:24 AM

No error, but in printer there is error "Paper detected is does not match the size or type"

if I put A4 Paper it is printing without error


Monday, January 17, 2011 9:27 AM

No error, but in printer there is error "Paper detected is does not match the size or type"

if I put A4 Paper it is printing without error

Consult your printer manual or contact the printer manufacturer for assistance.