Solved

Reusable Code for PrintDocument in vb.NET

Posted on 2011-09-22
4
1,067 Views
Last Modified: 2013-11-27
Gday,

I am writing a large amount of reports for my vb.net application, every report has and identical header and footer of each page. I dont want to re-write the code to produce these headers and footers for every report I create.

Is there a way with using the printdocument function in VB.Net to create a function or the like that can return a header and/or footer and insert it into the document? I have done some searching of google and EE to no avail.

Any help would be appricated!
0
Comment
Question by:pawnit
  • 2
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 36586166
Assuming that you use the PrintDocument-object and build your page in the PrintPage-event, you can create a seperate function that creates the header/footer for the page. You can then call that function/those functions from within the printpage-event to build the header and/or footer.
0
 

Author Comment

by:pawnit
ID: 36586195
cool i was hoping that was the case! I will be using the print document object and will build my pages in the printpage even. tho i don't quiet understand how i can return all the drawstring, drawimage & etc.. in a single function to be drawn onto the page.
0
 
LVL 18

Accepted Solution

by:
Dennis Aries earned 500 total points
ID: 36586221
You don't. You will pass the page-object as an argument to your function which will draw the desired items on the page and then returns. After that, you can continue your page-event drawing whatever you more want on that page.

A simple example:
Private Sub PrintHeader(ByRef e As System.Drawing.Printing.PrintPageEventArgs) 
    ' Draw title '
    e.Graphics.DrawString("Report Title"), New Font("Arial", 16), Brushes.Black, 95, 70) 
 
    ' Draw footer '
    e.Graphics.DrawImage(DirectCast(mResources.GetObject("footer_logo"), Drawing.Bitmap), 95, e.PageBounds.Height - 87) 
    Dim drawFont As New Font("Arial", 8.75) 
 
    e.Graphics.DrawString("Report Title", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 90) 
    e.Graphics.DrawString("Printed", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 76) 
    e.Graphics.DrawString("Printed By", drawFont, Brushes.Gray, 190, e.PageBounds.Height - 62) 
 
    ' Draw some grid lines to add structure to the footer information '
    e.Graphics.DrawLine(Pens.Gray, 246, e.PageBounds.Height - 90, 246, e.PageBounds.Height - 48) 
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 75, 550, e.PageBounds.Height - 75) 
    e.Graphics.DrawLine(Pens.Gray, 188, e.PageBounds.Height - 61, 550, e.PageBounds.Height - 61) 
 
    e.Graphics.DrawString("Report", drawFont, Brushes.Black, 250, e.PageBounds.Height - 90) 
    e.Graphics.DrawString(Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString, drawFont, Brushes.Black, 250, e.PageBounds.Height - 76) 
    e.Graphics.DrawString("Arkro IT", drawFont, Brushes.Black, 250, e.PageBounds.Height - 62) 
End Sub

Private Sub PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) 
    PrintHeader(e)

    ' Add your extra code for additional report-information here'
End Sub 

Open in new window

0
 

Author Closing Comment

by:pawnit
ID: 36586294
Thank you - I would never of thought to take that approach.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now