954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to set custom papersize to printer from code in vb.net 2005

Hello all,

we have a vb.net windows appliacation that uses sql server as backend. we have created all reports for this application using crystal report that is embedded in visual studio 2005. everything is working fine except the reports specially which needs to be displayed is custome mode. we have used some code to dynamically set the printer settings because at runtime the original printer settings are unknown and we don't want to let user set printer settings manually everytime before taking the printout. so the question is, how to set a custom papersize programactically. here is the code snippet that we have used to set some printer settings in runtime :-

rpt.PrintOptions.PrinterName = Configuration.ConfigurationManager.AppSettings("DefaultPrinter")
rpt.PrintOptions.PaperOrientation = PaperOrientation.Landscape
rpt.PrintOptions.PaperSize = PaperSize.PaperFanfoldUS


we have usedreportdocument object to set printer options. we have two rpts which need to displayed in custom paper format. the formats are like 9X6 and 14X12. we have created these formats in server properties. now how to to set this from code? like in place of

rpt.PrintOptions.PaperSize = PaperSize.PaperFanfoldUS


.....some sort of syntax which will let us to put the papersize in place ofPaperSize.PaperFanfoldUS...

hope i have explained the topic properly.....looking forward to any positive replies...any help/suggesstion or link to any resources will be greatly appreaciated....

thanks to all in advance...

regards
Shouvik

choudhuryshouvi
Posting Pro
553 posts since May 2007
Reputation Points: 30
Solved Threads: 49
 
G_Waddell
Posting Whiz in Training
255 posts since Nov 2009
Reputation Points: 43
Solved Threads: 27
 

Hi,
Try These Functions
AddForm(hPrinter, nLevel, pForm) To Add a new Form To your printer,
you should declare a function like this

Private Declare Function AddForm Lib "winspool.drv" Alias "AddFormA" (ByVal hPrinter As IntPtr, ByVal Level As Integer, ByRef pForm As Byte) As Integer

and then define a structure Form_Info_1

Public Structure FORM_INFO_1
Dim Flags As Integer
Dim pName As String
Dim Size As SIZEL
Dim ImageableArea As RECTL
End Structure

Dim frmInf As FORM_INFO_1
ReDim arByte() As Byte

With frmInf
.Flags = 0
.pName = "MiCustomForm"
.Size.cx = 160000
.Size.cy = 250000
End With

you have to copy the structure to a byte array

ReDim arByte(Marshal.SizeOf(frmInf))

Marshal.Copy(frmInf, arByte, 0, Marshal.SizeOf(frmInf))

And Finally Call Then Function

Dim hPrinter As IntPtr
Dim defaultSetting As IntPtr
Dim PrnVar As System.Drawing.Printing.PrintDocument = New System.Drawing.Printing.PrintDocument

If OpenPrinter(PrnVar.PrinterSettings.PrinterName, hPrinter, defaultSetting) Then
Try
retVal = AddForm(hPrinter, 1, arByte(0))
Catch ex As Exception
MsgBox(ex.Message)
End Try
ClosePrinter(hPrinter)
End If

YacNoh
Newbie Poster
1 post since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: