I can already print everything but my user wanted to have some special formatting (hanging indent) so I needed to redesign/replace my labels to a richtextbox. unfortunately the richtextbox I added isn't included in my printpreview.
below are my codes and my preview screenshot
Code:
Private PrintDoc1 As PrintDocument = New PrintDocument
Code:
Private Sub btnPrintPrevWith_Click(sender As Object, e As EventArgs) Handles btnPrintPrevWith.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
Code:
Private Sub PDoc_PrintPage(sender As Object, e As PrintPageEventArgs)
PrintToGraphics(e.Graphics, e.MarginBounds)
End Sub
Code:
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(PrintPanel.Width, PrintPanel.Height)
PrintPanel.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.Document
Graphics.DrawImage(Bitmap, target)
End Sub
before printing (print preview)
my print preview
pardon me as I'm still learning this printdocument.
Thanks in advance!