Printing RTF text in a hand-coded VB.NET report

G

Guest

Hi All, and thanks in advance

I'm using the following code to print out RTF data, and it works well. At a fixed position x pixels from the top of the page, I'm printing out various images, and that works well too.

What I don't know how to do now is to determine the pixel position of the bottom of the RTF text as printed out, and then start the printing of the images a few pixels down from the bottom of the RTF text

Here's the code to print out the RTF data

Public Sub PrintRTF(ByRef RTF_In As RichTextBox,
ByRef oPictureBoxIn As System.Windows.Forms.PictureBox,
ByRef e As System.Drawing.Printing.PrintPageEventArgs,
ByVal iPosX_In As Int32,
ByVal iPosY_In As Int32

'Source in: http://www.dotnet247.com/247reference/msgs/11/56581.asp
'Honors also due to Ken Tucker [MVP]" <[email protected]> for VB.NET conversion

Dim FormatRange1 As FORMATRANG
Dim rectDrawTo As Rec
Dim rectPage As Rec
Dim TextLength As Intege
Dim NextCharPosition As Intege
Dim r As Intege
Dim oBitmap As New System.Drawing.Bitmap(oPictureBoxIn.Width, oPictureBoxIn.Height
'Dim g As Graphics = Graphics.FromImage(oBitmap
'g.Clear(Color.White
Dim intScaleX As Single = 1440 / e.Graphics.DpiX ' g.Dpi
Dim intScaleY As Single = 1440 / e.Graphics.DpiY ' g.Dpi

' Start a print job to get a valid oPictureBoxIn.hD
'Dim hdc As IntPtr = g.GetHd
Dim hdc As IntPtr = e.Graphics.GetHd

Tr
With rectPag
.Left =
.Top =
.Right = oPictureBoxIn.ClientRectangle.Width 'Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsX(oPictureBoxIn.ClientRectangle.Width) ' oPictureBoxIn.ClientRectangle.Widt
.Bottom = oPictureBoxIn.ClientRectangle.Height 'Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsY(oPictureBoxIn.ClientRectangle.Height) ' oPictureBoxIn.ClientRectangle.Heigh
End Wit

' Set rect in which to print (relative to printable area
With rectDrawT
.Left = 1800 ' iPosX_In * intScaleX '288 ' Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsX(iPosX_In
.Top = 3600 ' iPosY_In * intScaleY '612 ' Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsY(iPosY_In
.Right = 10500 ' oPictureBoxIn.Width * intScaleX '1464 ' Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsX(oPictureBoxIn.ClientRectangle.Width
.Bottom = 9800 'oPictureBoxIn.Height * intScaleY '1008 ' Microsoft.VisualBasic.Compatibility.VB6.PixelsToTwipsY(oPictureBoxIn.ClientRectangle.Height
End Wit

' Set up the print instruction
L = 3
With FormatRange
.hdc = hdc.ToInt32 ' Use the same DC for measuring and renderin
.hdcTarget = hdc.ToInt32 ' Point at printer hD
.rc = rectDrawTo ' Indicate the area on page to draw t
.rectPage = rectPage ' Indicate entire size of pag
.charRange1.cpMin = 0 ' Indicate start of text throug
.charRange1.cpMax = -1 ' end of the tex
End Wit

' Get length of text in RT
TextLength = RTF_In.Text.Lengt

' Loop printing each page until don
D
' Print the page by sending EM_FORMATRANGE messag
NextCharPosition = SendMessage(RTF_In.Handle, EM_FORMATRANGE, True, FormatRange1
If NextCharPosition >= TextLength The
Exit D
End I
With FormatRange
.charRange1.cpMin = NextCharPosition ' Starting position for next page(
.hdc = hdc.ToInt3
.hdcTarget = hdc.ToInt3
End Wit
Loop Until NextCharPosition >= TextLengt

' Allow the RTF to free up memor
r = SendMessage(RTF_In.Handle, EM_FORMATRANGE, False, 0

'g.ReleaseHdc(hdc
e.Graphics.ReleaseHdc(hdc

Catch

Finall
End Tr

End Su
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top