Print RTF text that overflows one page

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi

Thanks to all of your help, I've got a working PrintRTF module that prints out RTF code, and it works well.

The next issue to overcome is to determine how to page-break text that is too long for one page

Is there some way to identify exactly how many characters I can print before I run out of room in the current rectangle that defines the output, and do to a new page. A VB.NET code sample with a loop to handle multiple pages would be great

Thanks

Neal Miller
 
* "=?Utf-8?B?TmVhbCBNaWxsZXI=?= said:
Thanks to all of your help, I've got a working PrintRTF module that prints out RTF code, and it works well.

Where did you get this code from?
 
Hi Herfried, here's the code I'm using

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 Sub
 
Hi, did anyone see my reply showing my PrintRTF code, and can anyone help me figure out how to do page breaks when the amount of RTF text exceeds the space remaining on the print rectangle?
 
Back
Top