Question How to print tabpage silently

Grega

Active member
Joined
Jun 21, 2007
Messages
25
Programming Experience
1-3
I am not sure if this fits in some other forum thread but here goes...


I have an application in which I open larger number of files create a specific report (I fill in values in a usercontrol with 3 tabpages and some textboxes labels and graph on each of them) for each and would like to print this control silently (meaning never to open it for user to view)...

it works great if I open it and print then, but if I only fill the report and try to print this I just get a blank page. (I am printing in a pdf printer to save paper :) - for now), although here also if I only look at first tabpage and print all 3 of them (but never actually clicking on the rest) I only get a print of first tabpage and the second two pages are again blank.

The code I use to print:

VB.NET:
Public Class PrintMe

    Dim printdialog1 As New PrintDialog
    Dim WithEvents PrintDocument1 As New Printing.PrintDocument
    Private bmp As Bitmap
    Public tp() As TabPage
    'Public tc As TabControl
    Private tabpagecount As Integer = 0

    Public Sub Print() 'ByVal _ucreportpreview As ucReportPreview
      
        '_ucreportpreview.TpPS.DrawToBitmap(bmp, _ucreportpreview.TpPS.DisplayRectangle)
        With printdialog1
            'If PrintDocument1.DefaultPageSettings.PaperSize.Width - bmp.Width < 12 Then
            If PrintDocument1.DefaultPageSettings.PaperSize.Width - tp(tabpagecount).Width < 12 Then

                PrintDocument1.DefaultPageSettings.Margins.Left = Convert.ToInt32((3.0 / 25.4) * 100)
                PrintDocument1.DefaultPageSettings.Margins.Right = Convert.ToInt32((3.0 / 25.4) * 100)
            Else
                'PrintDocument1.DefaultPageSettings.Margins.Left = (PrintDocument1.DefaultPageSettings.PaperSize.Width - bmp.Width) / 2
                PrintDocument1.DefaultPageSettings.Margins.Left = (PrintDocument1.DefaultPageSettings.PaperSize.Width - tp(tabpagecount).Width) / 2
                PrintDocument1.DefaultPageSettings.Margins.Right = PrintDocument1.DefaultPageSettings.Margins.Left

            End If

            PrintDocument1.DefaultPageSettings.Margins.Top = Convert.ToInt32((3.0 / 25.4) * 100)
            'If PrintDocument1.DefaultPageSettings.PaperSize.Height - bmp.Height < 12 Then
            If PrintDocument1.DefaultPageSettings.PaperSize.Height - tp(tabpagecount).Height < 12 Then

                PrintDocument1.DefaultPageSettings.Margins.Bottom = Convert.ToInt32((3.0 / 25.4) * 100)
            Else
                'PrintDocument1.DefaultPageSettings.Margins.Bottom = PrintDocument1.DefaultPageSettings.PaperSize.Height - bmp.Height
                PrintDocument1.DefaultPageSettings.Margins.Bottom = PrintDocument1.DefaultPageSettings.PaperSize.Height - tp(tabpagecount).Height

            End If
            .Document = PrintDocument1
            If printdialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                PrintDocument1.Print()
            End If
        End With

    End Sub


    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
        bmp = New Bitmap(tp(tabpagecount).Width, tp(tabpagecount).Height)
        tp(tabpagecount).DrawToBitmap(bmp, tp(tabpagecount).DisplayRectangle)

        tabpagecount += 1
        e.Graphics.DrawImage(bmp, e.MarginBounds)

        If tabpagecount < tp.Length Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
            bmp = Nothing
        End If
    End Sub

End Class


Any help is greatly appreciated.

Regards,
Greg
 
Test project attached.

Thank you very much John,

Well I guess the problem is in my system or VS...It's not working. I had to create a new solution but I imported all your files (I am using VS2005).

Maybe this has to do something with me using VS2005.

I guess its time to upgrade?! :)

Well anyway thanks for everything. Here is the same project in VS2005 solution if someone else or you want to try.

Regards,
Grega
 

Attachments

  • Print Test.zip
    76.4 KB · Views: 37
Last edited:
I'm only using VB 2008, but all .Net libraries used in that code is same for VB 2005. Could there be differences in OS? I'm using Vista. I know there is difference in how Vista and newer caches painting for top-level windows, but I didn't think that would affect this.
 
I'm only using VB 2008, but all .Net libraries used in that code is same for VB 2005. Could there be differences in OS? I'm using Vista. I know there is difference in how Vista and newer caches painting for top-level windows, but I didn't think that would affect this.

I am using XP.

I don't know I guess only way to find out would be to reinstall my VS or try later with VS2008 but currently there isn't any time to do that.
I will try on my home computer later. And see if that works. (VS2005 and Vista)
 
Yup it worked on my home computer (vista, VS2005)....I guess its time to reinstall VS on my work comp.
I think that would indicate that this works on Vista and not XP, probably due to the difference in how these OS paints windows. It's still strange I think that there would be a difference for tabpages in a control.

VS is not reponsible here, neither is 2008 vs 2005 since as I explained the exact same .Net library versions are at play.
 
ah ok, well I though that maybe some file or something might be corrupt with VS2005 or maybe some aadon or plugin or power pack I installed in the past might have some effect on this.

so does this mean that even if I compile application on my home machine (Vista) it still wouldn't work on XP.

I just find it weird I haven't found any info of this on the net.

Grega
 
so does this mean that even if I compile application on my home machine (Vista) it still wouldn't work on XP.
If it is correct that XP (and earlier) is the cause, then yes, it will not work in any XP even if you compiled the assembly in a Vista machine.
 
Back
Top