Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2008

Tip: Looking for answers? Try searching our database.

Formatting PrintDocument

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ross Culver - 21 Feb 2008 19:58 GMT
Can someone post a link to more indepth samples of formatting a PrintDocument.  I've found some info for the ReportViewer, but not for the PrintDocument.

Specifically, I'm looking for a code example of how to underline text in the middle of a document; how to draw a horizontal line and how to draw a box around text.

My guess is that I'm going about this all wrong.  Below is a snippet of how I'm setting up the page.

Thanks,

Ross

dim line as string
line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.UCase(RTrim(Me.CityStateZipTextBox.Text))
line = line & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
Do While detCntr <= detCNT
line = line & Me.ShippingLogDetailDataGridView.Rows(detCntr - 1).Cells(4).Value
line = line & Microsoft.VisualBasic.Space(13)
Select Case RTrim(Me.ShippingLogDetailDataGridView.Rows(detCntr - 1).Cells(1).Value.ToString)
    Case "F"
        line = line & "STAINLESS FITTINGS & FLANGES" & Microsoft.VisualBasic.Space(36)
    Case "P", "T"
        line = line & "STAINLESS PIPE" & Microsoft.VisualBasic.Space(50)
    Case "V"
        line = line & "STAINLESS VALVES" & Microsoft.VisualBasic.Space(48)
    End Select

    detCntr = detCntr + 1
Loop
line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.Space(15) & "Customer PO #: " & Microsoft.VisualBasic.UCase(RTrim(Me.CustPOTextBox.Text)) & vbCrLf
line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.Space(15) & "Tag #: " & Microsoft.VisualBasic.UCase(RTrim(Me.CustomersCustomersPONOTextBox.Text)) & vbCrLf & vbCrLf
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Bob Powell [MVP] - 21 Feb 2008 21:20 GMT
The PrintDocument is nothing more than a vehicle for drawing images.

When each page is printed, your handler will be called to draw the page
contents using standard GDI+ drawing comands via the Graphics object
provided in the print page event arguments.

You will draw the artefacts yourself. For example you caould create a
font with an underline and draw the text you need i the middle of the
screen using Graphics.DrawString.

To draw a box around text, use MeasureString to get a rectangle size,
draw the rectangle, then draw the text.

For details on how to use GDI+, see my site.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Can someone post a link to more indepth samples of formatting a PrintDocument.  I've found some info for the ReportViewer, but not for the PrintDocument.
>
[quoted text clipped - 27 lines]
> yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
> ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Ross Culver - 21 Feb 2008 22:15 GMT
Bob,

I've perused through your site and still have two fundamental issues:
1) all of your examples are painting to a form, what's the correlation to a printdocument?
2) I can't get past this: //Make a small bitmap Bitmap bm=new Bitmap(this.ClientSize.Width/4,this.ClientSize.Height/4);
   I'm using VB instead of C+ and can't translate this part into anything meaningful:new Bitmap(this.ClientSize.Width/4,this.ClientSize.Height/4)

Here's what I have:

Dim bm As Bitmap
Dim g As Graphics = Graphics.FromImage(bm)

Ross

> The PrintDocument is nothing more than a vehicle for drawing images.
>
[quoted text clipped - 42 lines]
>> yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
>> ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Ross Culver - 21 Feb 2008 22:19 GMT
Also, if the printDocument uses an image, then why do we pass a string value to it in (ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()))?

Ross
 Can someone post a link to more indepth samples of formatting a PrintDocument.  I've found some info for the ReportViewer, but not for the PrintDocument.

 Specifically, I'm looking for a code example of how to underline text in the middle of a document; how to draw a horizontal line and how to draw a box around text.

 My guess is that I'm going about this all wrong.  Below is a snippet of how I'm setting up the page.

 Thanks,

 Ross

 dim line as string
 line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.UCase(RTrim(Me.CityStateZipTextBox.Text))
 line = line & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf
 Do While detCntr <= detCNT
  line = line & Me.ShippingLogDetailDataGridView.Rows(detCntr - 1).Cells(4).Value
  line = line & Microsoft.VisualBasic.Space(13)
  Select Case RTrim(Me.ShippingLogDetailDataGridView.Rows(detCntr - 1).Cells(1).Value.ToString)
      Case "F"
          line = line & "STAINLESS FITTINGS & FLANGES" & Microsoft.VisualBasic.Space(36)
      Case "P", "T"
          line = line & "STAINLESS PIPE" & Microsoft.VisualBasic.Space(50)
      Case "V"
          line = line & "STAINLESS VALVES" & Microsoft.VisualBasic.Space(48)
      End Select

      detCntr = detCntr + 1
 Loop
 line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.Space(15) & "Customer PO #: " & Microsoft.VisualBasic.UCase(RTrim(Me.CustPOTextBox.Text)) & vbCrLf
 line = line & Microsoft.VisualBasic.Space(Col1) & Microsoft.VisualBasic.Space(15) & "Tag #: " & Microsoft.VisualBasic.UCase(RTrim(Me.CustomersCustomersPONOTextBox.Text)) & vbCrLf & vbCrLf
 yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
 ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Bob Powell [MVP] - 25 Feb 2008 22:49 GMT
Hi Ross,
Whatever the type of graphical operation you do the final output is an
image rendered on some physical device or an image rendered in memory.

The printer drawing surface is a higher resolution but a direct
equivalent of what you see on any screen.

The PrintDocument controls the basic printer settings and the PrintPage
event is raised to enable you to generate the graphics that you'll see
on the printed paper. The operations you'll use to print are identical
to those used to draw on screen.

When you output text, you certainly need to output a string, but the
glyphs of the text characters are rendered as pixels on a bitmap whih is
subsequently sent to the printer hardware.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Also, if the printDocument uses an image, then why do we pass a string value to it in (ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()))?
>
[quoted text clipped - 30 lines]
>   yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
>   ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Bob Powell [MVP] - 25 Feb 2008 23:30 GMT
A quick code example of printing a line of text underlined in the middle
of a page...

Imports System.Drawing.Printing

Public Class Form1

    dim count as decimal

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

        Dim preview As New PrintPreviewDialog()

        Dim pd As New PrintDocument()

        preview.Document = pd

        count = Me.NumericUpDown1.Value

        AddHandler pd.PrintPage, AddressOf Me.PrintPageHandler

        preview.ShowDialog()

    End Sub

    Public Sub PrintPageHandler(ByVal sender As Object, ByVal e As
PrintPageEventArgs)

        Dim fn As New Font("Verdana", 36, FontStyle.Underline,
GraphicsUnit.Point)
        Dim s As String = "Hello World!!"

        e.Graphics.PageUnit = GraphicsUnit.Point

        Dim sz As SizeF = e.Graphics.MeasureString(s, fn)

        e.Graphics.DrawString(s, fn, Brushes.Black, e.PageBounds.Width
/ 100 * 72 / 2 - sz.Width / 2, e.PageBounds.Height / 100 * 72 / 2 -
sz.Height / 2)

        count -= 1

        If (count > 0) Then
            e.HasMorePages = True
        End If

    End Sub
End Class

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

> Also, if the printDocument uses an image, then why do we pass a string value to it in (ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat()))?
>
[quoted text clipped - 30 lines]
>   yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
>   ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, New StringFormat())
Ross Culver - 29 Feb 2008 17:36 GMT
Thanks, Bob.  I'll give it a try and let you know if I get it (understand
it).

Ross

>A quick code example of printing a line of text underlined in the middle of
>a page...
[quoted text clipped - 99 lines]
>>   ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin,
>> yPos, New StringFormat())

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.