Setting page size in code

Hi,

Can anyone help with this please

I am trying to set the paper size for the print page method in code, however, It fails to change

I have tried many properties and ways of setting it but it fails

Basically I have a app that saves the page size to a xml file as an interger value that represents the paper size. Eg 8 = A3.

I cannot seem to change the default value of the PoPrintPage_Printpage method from the value of 9 (A4)

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

'\\ Setup paper size

With poprintdoc.DefaultPageSettings

'\\ Following line sets newpaper to integer value of pagesize eg 8(A3)
'\\ It is not defind in this method, however, I have placed it her to show how
'\\ I am obtaining the selected pagesize value from the pagesetup dialog

newpaper = poprintdoc.DefaultPageSettings.PaperSize.RawKind

'\\ The following line atempts to set the deafault papersize to the value of newpaper( 8 A3)

.PaperSize.RawKind = newpaper '\\ eg newpaper = 8

End With

Ron



Answer this question

Setting page size in code

  • Sensei_D

    Ron, glad to see you got it working. And thanks for sharing your code.

    james

    aka:Trucker


  • sky35366

    Where are the Reneec's of this world when you need them

    Please help this is driving me mad


  • Gregory English

    Is there anyone that can help with this please
  • Hems4all

    James,

    Many Thanks for the reply,

    I will give this ago. I still don't understand why the above

    code will not work.

    Ron


  • AJ_Developer

    I still have the problem. I have found that if I use the following code and set the value of the pagesize variable from within the pagesetup dialog the .papersize.rawkind=pagesize receives the value of pagesize eg (5 = letter)

    If the value of the variable pagesize is set within code the .papersize.rawkind= pagesize will not receive the value 5, even though the pagesize variable = 5. The .papersize.rawkind ias always set to 9 (A4)

    If I also code .papersize.rawkind=5 it still will not work.

    I can not see any reason for this

    HELP PLEASE

    ' pagesize = .PaperSize.RawKind

    .PaperSize.RawKind = pagesize

    What i am trying to do is this:

    The user sets the pagesize using the pagesetup dialog. the pagesize variable holds this value.

    Along with other form field data the value of the page size is written to a XML file.Data saves under a profile name entered by the end user.

    The data held within this XML file is loaded on form load and is used as the data for my dataset.

    The pagesize is saved and retrieved ok. When in debugmode I check the value of the pagesetup variable and it, forexample = 5. However the .PaperSize.Rawkind will not change.

    Must be a bug!


  • EricVB

    First of all many thanks to those who replied.

    James,

    The code suggested works for filling a combo box with the printers supported paper sizes and then setting the printpage default paper size based upon the combobox selectedIndex value.

    My development requirements were as follows:

    1) user selects papersize from the default pagesetup dialog

    2) The papersize along with other user provided settings are then saved to an XML file saved under a user provided profile name. The paper size is saved as an integer value representing the paper size eg 1=letter 9=A4. (using the paperkind class)

    3) A combobox is dynamically filled with these saved profiles which allows the user to recall the saved settings

    After lots of testing I have coded this which does the job perfectly

    '\\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    Next line placed in declarations

    Dim papertype As PaperKind

    #Region "PageSetupDialog"

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

    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

    '\\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    '\\ sets papertype = value of selected paper size integer

    papertype = CType(dlg.PageSettings.PaperSize.RawKind, PaperKind)

    '\\ sets print papersize to value of papertype

    poprintdoc.DefaultPageSettings.PaperSize.RawKind = papertype

    End Sub

    #End Region

    If at first you don't succeed try try again!!!


  • EvenOvich

    Ok let me put it another way

    The objective:

    to save the user selected papersize and then recall the saved value(papersize) and apply it to the printpage method.

    Surely there's someone ( Microsoft ) that can help with this please

    Ron


  • bitjunkie

    Try setting the DefaultPageSettings of the Printer Object you are using by setting the PaperSize property to a valid paper size to print in that size.

    Note that you can't change the printer settings in the PrintPage event (even though the HELP indicates that you can) , but you can in the QueryPageSettings event (regardless, you can't change the DefaultPageSettings after printing has started).

    Lookup DefaultPageSettings in the help.



  • Derek Ju

    After more coding and testing I have found that the code I have used is correct. I have created a simple app to test this and it works. I will re-check my code in the original project to see what is causing these lines of code to fail.

    ron


  • Mr. Saint

    There's a ton of info in the Help system. Here's a link to MSDN2 that has info on what you need:

    http://msdn2.microsoft.com/en-us/library/system.drawing.printing.pagesettings.papersize.aspx

     

    If you have not already done so, download the Framework 2.0 SDK (330+Megs) it's a large download, but, well worth the time. It contains a lot of useful info and examples.

     Here's a little of what you need:

    Find the installed printer's supported  papersizes:

    (be sure to include at the top of your class: Imports System.Drawing.Printing)

    Also add this just below the Form1 Class name:

    Dim printDoc as New PrintDocument

    Add a combobox and name it: ComboPaperSize

    Add a button and name it : ListSizes

    Private Sub ListSizes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click

    ComboPaperSize.DisplayMember = "PaperName"

    Dim i As Integer

    Dim pkSize As PaperSize

    For i = 0 To printDoc.PrinterSettings.PaperSizes.Count - 1

    pkSize = printDoc.PrinterSettings.PaperSizes.Item(i)

    ComboPaperSize.Items.Add(pkSize)

    Next

    End Sub

    Add a Print button:

    Private Sub MyButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButtonPrint.Click
    
      ' Set the paper size based upon the selection in the combo box.
      If comboPaperSize.SelectedIndex <> -1 Then
        printDoc.DefaultPageSettings.PaperSize = _
        printDoc.PrinterSettings.PaperSizes.Item(comboPaperSize.SelectedIndex)
      End If
    
      ' Set the paper source based upon the selection in the combo box.
      If comboPaperSource.SelectedIndex <> -1 Then
        printDoc.DefaultPageSettings.PaperSource = _
        printDoc.PrinterSettings.PaperSources.Item(comboPaperSource.SelectedIndex)
      End If
    
      ' Set the printer resolution based upon the selection in the combo box.
      If comboPrintResolution.SelectedIndex <> -1 Then
        printDoc.DefaultPageSettings.PrinterResolution = _
        printDoc.PrinterSettings.PrinterResolutions.Item(comboPrintResolution.SelectedIndex)
      End If
    
      ' Print the document with the specified paper size and source.
      printDoc.Print()
    
    End Sub
    
     

    You may have to play with this code a bit (all taken from samples in Help) but it does work for me.

    james

    aka:Trucker

     


  • Setting page size in code