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
Solved

Reusable Code for PrintDocument in vb.NET

Posted on 2011-09-22
4
1,090 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
parsing JSON help 1 20
What are the simplest ways to create a Merge Sort? 3 29
System.net.Sockets Error 5 26
Name Space error VS2015 1 24
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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

856 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