VS 2015 Can't see richtextbox in my print preview *using bitmap + printdocument*-VBForums
Results 1 to 2 of 2

Thread: Can't see richtextbox in my print preview *using bitmap + printdocument*

  1. #1

    Thread Starter
    Registered User
    Join Date
    Dec 2017
    Posts
    1

    Can't see richtextbox in my print preview *using bitmap + printdocument*

    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)
    Name:  Capture.jpg
Views: 124
Size:  50.5 KB

    my print preview
    Name:  Capture2.PNG
Views: 118
Size:  244.0 KB

    pardon me as I'm still learning this printdocument.
    Thanks in advance!

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,265

    Re: Can't see richtextbox in my print preview *using bitmap + printdocument*

    According to the documentation (which you should have already read):
    DrawToBitmap is not fully functional for the RichTextBox; only the border of a bitmap is drawn.
    I would assume that calling DrawToBitmap on a Panel, which you appear to be doing, would then call the same method on all its child controls. If one of those child controls is a RichTextBox and that method doesn't do anything more than draw a border for a RichTextbox then you won't get more than a border.

    If you search the MSDN site you'll find at least one article dedicated to a WYSIWYG RichTextBox. That may not be of use to you as is because you want to print more than just the RTB but I would expect that it could incorporated and/or adapted to work in your scenario.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width