REPORTING WITH PRINTDOCUMENT IN .NET

Due to flexibility in creating reports with some tools in the .NET framework , using PrintDocument() class in creating reports is fading out. But sometimes you don’t have a choice but to use it. For instance creating reports without any predefined data source or generating reports where needed columns are determined at runtime.

In this post we will go through creating a report using the PrintDocument. Our datasoure will also come from a listview which contains a list of file types and their sizes from an indexed drive. When using the report document everything is drawn to the report interface, thus you draw a string, an image,a line. the difficulty in here also is, you yourself determine at runtime where your text, lines and images should be positioned

To start with, first drag a PrintPreviewDialog control on your form. The control is named “PrintPreview1”.

image

Then go to the code view and create  your procedure below.

Private Sub detaildisksreport_PrintPage(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        Dim linesPerPage As Single = 0
        Dim yPos As Single = 0
        Static count As Integer = 0
        Dim x, y, c, i As Integer
        Dim leftMargin As Single = e.MarginBounds.Left
        Dim topMargin As Single = e.MarginBounds.Top

        ‘declaring fonts to use in your reports
        Dim backFont As New Font("Arial", 10, FontStyle.Italic)
        Dim HeaderFont As New Font("Arial", 20, FontStyle.Bold)
        Dim normfont As New Font("Times New Roman", 18, FontStyle.Regular)

        ‘ Calculate the number of lines per page.
        linesPerPage = e.MarginBounds.Height / normfont.GetHeight(e.Graphics)

        ‘x determines the horizontal positions whiles y determines the vertical position

        ‘ Drawing an image on top of the report
        Dim b As Rectangle
        Dim pic As New Rectangle(40, 5, pic1.Width + 40, pic1.Height + 40)
        e.Graphics.DrawImage(pic1.Image, pic)

        ‘drawing a line around the image
        b = New Rectangle(40, 5, pic1.Width + 40, pic1.Height + 40)
        e.Graphics.DrawRectangle(Pens.Black, b)

        linesPerPage = e.MarginBounds.Height / normfont.GetHeight(e.Graphics)
        linesPerPage = linesPerPage – 5

        ‘ Drawing our report titles
        x = 190
        y = pic1.Height + 70

        e.Graphics.DrawString("DETAIL DRIVE REPORT", HeaderFont, Brushes.Black, x, y)

        y += 40
        x = 25
        e.Graphics.DrawString(" FOLDERS ON DRIVE :  " & Me.cmbCat.SelectedItem.ToString.ToUpper, HeaderFont, Brushes.Black, x, y)
        y += 40
        x = 10
        e.Graphics.DrawString("REPORT GENERATED ON " & Now.ToLongDateString.ToUpper, HeaderFont, Brushes.Black, x, y)
        y += 10
        x = 5

        e.Graphics.DrawLine(Pens.Black, 5, y + 30, 820, y + 30)
        x = 10
        y += 60
        c = y

        e.Graphics.DrawString("FOLDER NAME", HeaderFont, Brushes.Blue, x, y)
        x += 260

        e.Graphics.DrawString("NUMBER OF FILES", HeaderFont, Brushes.Blue, x, y)
        x += 330

        e.Graphics.DrawString("FOLDER SIZE", HeaderFont, Brushes.Blue, x, y)

        e.Graphics.DrawLine(Pens.Black, 5, y + 30, 820, y + 30)

        x = 33
        y = c + 40

        i = 0
        ‘adding the content to the report
        For i = count To Me.LsvCDs.Items.Count – 1

            Dim len As Integer
            If LsvCDs.Items.Item(i).SubItems(0).Text.Length > 25 Then
                len = 25
            Else
                len = LsvCDs.Items.Item(i).SubItems(0).Text.Length
            End If

            e.Graphics.DrawImage(Me.SmallImages.Images(0), 15, y + 2)

            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(0).Text.Substring(0, len), normfont, Brushes.Blue, x, y)
            x += 400
            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(1).Text, normfont, Brushes.Blue, x, y)
            x += 220
            e.Graphics.DrawString(Me.LsvCDs.Items.Item(i).SubItems(2).Text, normfont, Brushes.Blue, x, y)

            ‘setting the y value to point to the next line
            ‘reseting the x value to the margin
            y += 30
            x = 33

            ‘checking to see if the current page is full
            If y + normfont.Height > e.PageSettings.PaperSize.Height Then
                count = i + 1
                e.HasMorePages = True
                Return

            End If
        Next

        count = 0
        e.HasMorePages = False
    End Sub

Also add the code below to a button click event and you are good to go.

   1: Dim pd As New PrintDocument()

   2: 'adding the print method created to the printdocument printpage event

   3: AddHandler pd.PrintPage, AddressOf disksreport_PrintPage

   4:  

   5: 'passing the print doucument to the PrintPreviewDialog

   6: PrintPreview1.Document = pd

   7: PrintPreview1.ShowDialog()

Below are sample interfaces, using printDocument to generate reports

image

image

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s