Solved

Reusable Code for PrintDocument in vb.NET

Posted on 2011-09-22
4
1,047 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you - I would never of thought to take that approach.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

8 Experts available now in Live!

Get 1:1 Help Now