How to print a Rich Text Box or BMP to a VB.NET PrintDocument

Posted on 2003-03-25
Medium Priority
Last Modified: 2012-05-04
I am trying to generate a report using VB.NET.

I can easily use Drawstring to place text on the Print Document.

But I am not sure how to define a square where a rich text box can be located on the print document and printed

Question by:ajtech
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 28

Expert Comment

ID: 8202582
That will print form or picture box

'label, picture and 4 buttons

Option Explicit On
Option Strict On

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

#Region "Variables"
    Private printWholeForm As Boolean = False
    Private memImage As Bitmap
#End Region
#Region "Base Events"
    Private Sub print_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printGraphic.Click, PrintForm.Click
        Dim buttonID As Button = CType(sender, Button)

        If buttonID.Name = "printForm" Then
            printWholeForm = True
            Call BuildFormImage()
        End If
        PrintDialog1.Document = PrintDocument1
        If PrintDialog1.ShowDialog = DialogResult.OK Then
        End If
    End Sub

    Private Sub printPreview_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles printPreviewGraphics.Click, printPreviewForm.Click
        Dim buttonID As Button = CType(sender, Button)
        Dim PrintPreviewID As New PrintPreviewDialog()

        PrintPreviewID.Document = PrintDocument1
        If buttonID.Name = "printPreviewForm" Then
            printWholeForm = True
            Call BuildFormImage()
        End If
        If PrintPreviewID.ShowDialog = DialogResult.OK Then
        End If
    End Sub

    Private Sub PrintDocument1_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        Dim formGraphics As Graphics = e.Graphics

        If printWholeForm = False Then
            ' Build each part of the picture
            With formGraphics
                .DrawString(Label1.Text, Label1.Font, New SolidBrush(Label1.ForeColor), 50, 50)
                .DrawImage(PictureBox1.Image, 100, 100)
            End With
            formGraphics.DrawImage(memImage, 0, 0)
            printWholeForm = False
        End If
        e.HasMorePages = False
    End Sub
#End Region
#Region "Implementation Methods"
    Private Sub BuildFormImage()
        Dim graphicID As Graphics = Me.CreateGraphics
        Dim sizeID As Size = Me.Size
        Const SRCCOPY As Integer = &HCC0020

        memImage = New Bitmap(sizeID.Width, sizeID.Height, graphicID)
        Dim memGraphic As Graphics = graphicID.FromImage(memImage)
        Dim deviceContext1 As IntPtr = graphicID.GetHdc
        Dim deviceContext2 As IntPtr = memGraphic.GetHdc

        BitBlt(deviceContext2, 0, 0, Me.ClientRectangle.Width, Me.ClientRectangle.Height, deviceContext1, 0, 0, SRCCOPY)
    End Sub

    Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal hdcDest As IntPtr, ByVal _
     nXDest As Integer, ByVal nYDest As Integer, ByVal nWidth As Integer, ByVal nHeight _
     As Integer, ByVal hdcSrc As _
     IntPtr, ByVal nXSrc As Integer, _
     ByVal nYSrc As Integer, ByVal _
     dwRop As System.Int32) As Boolean
#End Region

End Class


Author Comment

ID: 8203850
Ok that is fine, but that is not what I wanted.

I want to print a Rich Text Box
LVL 28

Expert Comment

ID: 8203898
DO you want to print content of RTB?
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.


Author Comment

ID: 8204278

I want to create a rectangle somewhere on the printdocument and print the properly formatted rich text contents

LVL 28

Accepted Solution

iboutchkine earned 2000 total points
ID: 8204296
That is the easiest code to print RTB

                rtb.SaveFile("c:\temp.rtf") 'save file to disk
                Dim o As Object
                o = GetObject("c:\temp.rtf")
                o = Nothing
            Catch ex As Exception
            End Try

also you can check
LVL 96

Expert Comment

by:Bob Learned
ID: 9725001
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered by iboutchkine

Please leave any comments here within the next seven days.


EE Cleanup Volunteer

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question