Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Reusable Code for PrintDocument in vb.NET

Posted on 2011-09-22
4
Medium Priority
?
1,157 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

715 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