Printform doesn't print entire form width
-
viernes, 06 de abril de 2012 21:24
I am trying to use the printform control to print one of the forms in a MDI document. For some reason It only seems to print a portion of the form width that is displayed on the screen. I have played with portrait and landscape page orientations with landscape printing more of the form but still not the entrie width. It seems like it is a screen to printer resolution issue but I can't figure out how to compress the form to fit the printer resolution. The code I found in another thread I have been playing with and a print preview screen shot. Any help would be appreciated.
With Me.PrintForm1 .PrintAction = Printing.PrintAction.PrintToPreview For i = 0 To .PrinterSettings.PaperSizes.Count - 1 If .PrinterSettings.PaperSizes(i).Kind = PaperKind.Letter Then Dim size As PaperSize = .PrinterSettings.PaperSizes(i) .PrinterSettings.DefaultPageSettings.PaperSize = size .PrinterSettings.DefaultPageSettings.Landscape = True '.PrinterSettings.PrintRange.AllPages(1) Exit For End If Next Dim MyMargins As New Margins With MyMargins .Left = 0 .Right = 0 .Top = 0 .Bottom = 0 End With .PrinterSettings.DefaultPageSettings.Margins = MyMargins .Print(Me, PowerPacks.Printing.PrintForm.PrintOption.CompatibleModeFullWindow) End With
Todas las respuestas
-
lunes, 09 de abril de 2012 5:25
Hi mustBob,
What do you mean by " one of the forms in a MDI document"? According to my experience, it seems that the printdocument doesn't provide the function to adjust itself to suit the size of papper.
Hope this helps.
No code, No fact.
-
lunes, 09 de abril de 2012 9:26Moderador
Hi,
I have no idea how to customize the control size to fit the printing paper size in the PrintForm in the power pack. But could you please the PrintPreviewDialog class instead? We can shrink the image of the form if the its size is larger than the normal A4 pager size.
Here are some sample codes for your references. I only handled the form's width is larger than the normal A4 paper width condition. You can edit the codes to meet your own requirement.
Private PrintDoc1 As PrintDocument = New PrintDocument Private PrintPreviewDialog1 As PrintPreviewDialog = New PrintPreviewDialog Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click PrintPreviewDialog1.Document = PrintDoc1 PrintDoc1.OriginAtMargins = False AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage PrintPreviewDialog1.ShowDialog() End Sub Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs) Dim bmp As Bitmap = New Bitmap(Me.Width, Me.Height) Me.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height)) If Me.Width > 820 Or Me.Height > 1100 Then Dim bmp2 As Bitmap = New Bitmap(bmp) Dim height As Integer = 820.0 * Me.Height / Me.Width Dim bmp3 As Bitmap = New Bitmap(bmp2, 820, height) e.Graphics.DrawImage(bmp3, 0, 0) Else e.Graphics.DrawImage(bmp, 0, 0) End If End SubGood day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Propuesto como respuesta Michael Sun [MSFT]Microsoft Employee, Moderator jueves, 12 de abril de 2012 6:04
- Marcado como respuesta Michael Sun [MSFT]Microsoft Employee, Moderator lunes, 16 de abril de 2012 1:42
-
lunes, 09 de abril de 2012 12:06
Hi mustBob,
What do you mean by " one of the forms in a MDI document"? According to my experience, it seems that the printdocument doesn't provide the function to adjust itself to suit the size of papper.
Hope this helps.
No code, No fact.
The form I am trying to print is one of the child forms in a MDI document. I think I have to abandon the PrintForm method as even if I can get it to print the entire formwidth it won't do what I need. The summary report form contains a mix of text and images and is scrollable. Initially it seemed the print form control would work but now I think print the printdocument method may provide a better solution. My problem will be since the form is scrollable I need method that is not just a screenshot. -
lunes, 09 de abril de 2012 12:21Moderador
Hi,
So how about the workaround I provided? If you have any questions, please feel free to let me know.
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
lunes, 09 de abril de 2012 12:27
Thanks Michael,
Your code should solve the form width issue but it will still be a screen shot. Since my form is scrollable I will need something that would allow me to print at least one and possibly two letter size pages. I have played with a method to print a scrollable rich text box but that only works for text. If I drop graphics or images into it they don't print.
Any ideas?
-
martes, 10 de abril de 2012 3:14Moderador
Hi,
Thanks for the following up! Could you please make your request be more specific? Do you want to print the forms in multiple pages? Or do you want to control the print image scrollable automatically?
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
jueves, 12 de abril de 2012 1:46Moderador
Hi,
Any update? If you need any assistance, please feel free to let me know.
Good day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
jueves, 12 de abril de 2012 2:43
Sorry for posting back sooner. I haven't been able to work on this project for a few days.
I tried your solution and it solves the form width issue. Now I just need to get the full form height. What I am thinking of trying now is to place all the controls in a panel and then just print the panel. Not sure if this will work though. Any thoughts.
-
jueves, 12 de abril de 2012 7:01Moderador
Hi,
I think it's fine to print the panel since we can trend the panel as a control too, like:
Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs)
Dim bmp As Bitmap = New Bitmap(Me.Panel1.Width, Me.Panel1.Height)
Me.Panel1.DrawToBitmap(bmp, New Rectangle(0, 0, bmp.Width, bmp.Height))
e.Graphics.DrawImage(bmp, 0, 0)
End SubIs it what you want?
Have a nice day!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Propuesto como respuesta Michael Sun [MSFT]Microsoft Employee, Moderator viernes, 13 de abril de 2012 1:41
- Marcado como respuesta MustBob viernes, 13 de abril de 2012 10:56
-
jueves, 12 de abril de 2012 11:16
Yep I tried that last night and it works. The only problem is it distorts the aspect ratio of the panel so it is compressed in height . On the bright side at least I can print the whole form now. I'll have to see if there is a way to correct the aspect ratio issue.
Thanks for all you help so far.
-
viernes, 13 de abril de 2012 1:41Moderador
You're welcome! If you need any assistance, please feel free to let me know.
Have a nice weekend!
Thanks
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
-
viernes, 13 de abril de 2012 11:07I solved the aspect ratio issue and have it almost printing perfectly now. Never thought printing would be so involved. I marked one of your posts as answer.
-
lunes, 16 de abril de 2012 1:42Moderador
Thanks for the update! By the way, if it is convenient, could you please share the solution you have to benefit more community members?
Have a nice day!
Michael Sun [MSFT]
MSDN Community Support | Feedback to us
- Editado Michael Sun [MSFT]Microsoft Employee, Moderator lunes, 16 de abril de 2012 1:43
-
lunes, 16 de abril de 2012 12:16
Here is the final solution. It has nothing to do with the VB Power Packs PrintForm control which is where I started. This control just would do what I needed so I ended up using the .printing namespace. The solution involves creating a form with three buttons, Page Setup, Print Preview and Print and a pane. All of the form controls you want to print are placed in the panel and the graphics print method is used to print everything. The graphics print routine scales the panel to fit the available print area (Letter, legal A4 whatever is selected).
Add reference to printing namespace
Imports System.Drawing.Printing
Define your print object
Private PrintDoc1 As PrintDocument = New PrintDocument
Page Setup Button Code
Private Sub btnPageSetup_Click(sender As System.Object, e As System.EventArgs) Handles btnPageSetup.Click With PageSetupDialog1 'Assign the document to use .Document = PrintDoc1 'Enable printer button .AllowPrinter = True .EnableMetric = True If .ShowDialog = Windows.Forms.DialogResult.OK Then ' Initialize the dialog's PrinterSettings property to hold user ' defined page settings. PageSetupDialog1.PageSettings = _ New System.Drawing.Printing.PageSettings ' Initialize dialog's PrinterSettings property to hold user ' set printer settings. .PrinterSettings = _ New System.Drawing.Printing.PrinterSettings End If End With End Sub
Preview Dialog and Print Buttons
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage PrintDoc1.Print() End Sub Private Sub btnPrintPreview_Click(sender As System.Object, e As System.EventArgs) Handles btnPrintPreview.Click PrintPreviewDialog1.Document = PrintDoc1 'Add a handler to direct print from Preview Dialog to graphics AddHandler PrintDoc1.PrintPage, AddressOf PDoc_PrintPage PrintPreviewDialog1.ShowDialog() End Sub
Print Handler and Graphics print method with scaling
Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs) PrintToGraphics(e.Graphics, e.MarginBounds) End Sub Public Sub PrintToGraphics(Graphics As Graphics, bounds As Rectangle) 'Print the control's view to a Graphics object. '<param name="graphics">Graphics object to draw on.</param> '<param name="bounds">Rectangle to print in.</param>
'Draw the control and contents to a bitmap Dim Bitmap As Bitmap = New Bitmap(pnlSetupSummary.Width, pnlSetupSummary.Height) pnlSetupSummary.DrawToBitmap(Bitmap, New Rectangle(0, 0, Bitmap.Width, Bitmap.Height))
'Assign Print Bounds to target rectangle Dim PrtWidth, PrtHeight, PrtLeft, PrtTop As Integer PrtWidth = bounds.Width PrtHeight = bounds.Height PrtLeft = bounds.Left PrtTop = bounds.Top Dim target As Rectangle = New Rectangle(PrtLeft, PrtTop, PrtWidth, PrtHeight) 'Scale bitmap to fit target Dim xScale As Double = Bitmap.Width / PrtWidth Dim yScale As Double = Bitmap.Height / PrtHeight If xScale < yScale Then target.Width = Int(xScale * target.Width / yScale) Else target.Height = Int(yScale * target.Height / xScale) End If 'Draw the bitmap Graphics.PageUnit = GraphicsUnit.Display Graphics.DrawImage(Bitmap, target) End Sub
- Marcado como respuesta Michael Sun [MSFT]Microsoft Employee, Moderator lunes, 16 de abril de 2012 12:30
-
lunes, 16 de abril de 2012 12:30Moderador
-
viernes, 03 de agosto de 2012 7:46
Good morning,
congratulations for this page.
I have a question.
i try to use the above code but it does not recognize "PDoc_Printpage". What i am doing wrong?
Best regards,
Sérgio
-
viernes, 03 de agosto de 2012 11:11
Does the top of you code look exactly like this?
Imports System.Drawing.Printing Public Class SetupPrint Private PrintDoc1 As PrintDocument = New PrintDocument

