Solved

Reusable Code for PrintDocument in vb.NET

Posted on 2011-09-22
4
1,107 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

735 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