Humble Seeker
Newcomer
- Joined
- Jun 18, 2003
- Messages
- 10
Right I've got a slightly sticky problem. I need to print out from the web browser control (wbc) with bakground colour. The problem starts that IE is usually set to not print with background colours off. is there a way I can tell the WBC to print out it's contents with the background colour on?
Here is my attempt so far, its all done in .net.
Imports System.Windows
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnPrint As System.Windows.Forms.Button
Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Friend WithEvents lblPageTitle As System.Windows.Forms.Label
Friend WithEvents optMinimize As System.Windows.Forms.Button
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser()
Me.btnExit = New System.Windows.Forms.Button()
Me.btnPrint = New System.Windows.Forms.Button()
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
Me.lblPageTitle = New System.Windows.Forms.Label()
Me.optMinimize = New System.Windows.Forms.Button()
Me.Splitter1 = New System.Windows.Forms.Splitter()
CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
Me.AxWebBrowser1.Enabled = True
Me.AxWebBrowser1.Location = New System.Drawing.Point(0, 16)
Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxWebBrowser1.Size = New System.Drawing.Size(848, 632)
Me.AxWebBrowser1.TabIndex = 0
Me.btnExit.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnExit.ForeColor = System.Drawing.Color.White
Me.btnExit.Location = New System.Drawing.Point(824, 0)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(24, 16)
Me.btnExit.TabIndex = 1
Me.btnExit.Text = "X"
Me.btnPrint.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.btnPrint.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnPrint.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnPrint.ForeColor = System.Drawing.Color.White
Me.btnPrint.Name = "btnPrint"
Me.btnPrint.Size = New System.Drawing.Size(64, 24)
Me.btnPrint.TabIndex = 2
Me.btnPrint.Text = "Print"
Me.lblPageTitle.BackColor = System.Drawing.Color.Transparent
Me.lblPageTitle.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblPageTitle.ForeColor = System.Drawing.Color.White
Me.lblPageTitle.Location = New System.Drawing.Point(80, 0)
Me.lblPageTitle.Name = "lblPageTitle"
Me.lblPageTitle.Size = New System.Drawing.Size(680, 16)
Me.lblPageTitle.TabIndex = 3
Me.optMinimize.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.optMinimize.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.optMinimize.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.optMinimize.ForeColor = System.Drawing.Color.White
Me.optMinimize.Location = New System.Drawing.Point(792, 0)
Me.optMinimize.Name = "optMinimize"
Me.optMinimize.Size = New System.Drawing.Size(24, 16)
Me.optMinimize.TabIndex = 4
Me.optMinimize.Text = "--"
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(3, 638)
Me.Splitter1.TabIndex = 5
Me.Splitter1.TabStop = False
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Maroon
Me.ClientSize = New System.Drawing.Size(848, 638)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Splitter1, Me.optMinimize, Me.lblPageTitle, Me.btnPrint, Me.btnExit, Me.AxWebBrowser1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://www.bbc.co.uk")
lblPageTitle.Text() = AxWebBrowser1.Name.ToString
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_SELECTALL, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
Print()
End Sub
Public Sub Print()
Try
Forms.SendKeys.SendWait("%{PRTSC}")
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
pd.DocumentName = "PrintScreen"
With pd.DefaultPageSettings
.Landscape = False
'-- Reduce margins to accomodate larger pictures
.Margins.Left -= .Margins.Left \ 3
.Margins.Top -= .Margins.Top \ 3
.Margins.Right -= .Margins.Right \ 3
.Margins.Bottom -= .Margins.Bottom \ 3
End With
'-- Print document
pd.Print()
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
Try
'-- Get PrintScreen bitmap from clipboard
Dim iData As IDataObject = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Bitmap) Then
Dim bm As Bitmap = CType(iData.GetData(DataFormats.Bitmap), Bitmap)
'-- Set size of destination rectangle (printer)
Dim rDest As New RectangleF(ev.MarginBounds.Left, ev.MarginBounds.Top, 0, 0)
If bm.Width < ev.MarginBounds.Width Then
'-- Set to bitmap size if bitmap is smaller than paper
rDest.Width = bm.Width
rDest.Height = bm.Height
Else
'-- Set to paper size if bitmap is larger than paper
'-- Scale height to retain proportions
rDest.Width = ev.MarginBounds.Width
rDest.Height = CType(rDest.Width * (bm.Height / bm.Width), Single)
End If
'-- Draw bitmap
ev.Graphics.DrawImage(bm, rDest, bm.GetBounds(GraphicsUnit.Pixel), GraphicsUnit.Pixel)
'-- Draw timestamp
Dim printFont As Font = New Font("Arial", 10, FontStyle.Regular)
ev.Graphics.DrawString("End of job", printFont, Brushes.Black, rDest.Left, rDest.Top + rDest.Height + printFont.GetHeight(ev.Graphics))
Else
'-- No bitmap, cancel printing
ev.Cancel = True
End If
Catch ex As Exception
'-- Cancel printing
ev.Cancel = True
Throw ex
Finally
'-- Print only on page
ev.HasMorePages = False
End Try
End Sub
Private Sub optMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optMinimize.Click
On Error Resume Next
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER)
End Sub
End Class
Long I know. As you can see I do have two forms of print on here already, on takes a screen shot of the form (giving me background colouring, but on part of the page) the second print out the page like IE (full, but no background colours)
Any suggestions?
Here is my attempt so far, its all done in .net.
Imports System.Windows
Imports System.Drawing
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
InitializeComponent()
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
Private components As System.ComponentModel.IContainer
Friend WithEvents AxWebBrowser1 As AxSHDocVw.AxWebBrowser
Friend WithEvents btnExit As System.Windows.Forms.Button
Friend WithEvents btnPrint As System.Windows.Forms.Button
Friend WithEvents PrintDocument1 As System.Drawing.Printing.PrintDocument
Friend WithEvents lblPageTitle As System.Windows.Forms.Label
Friend WithEvents optMinimize As System.Windows.Forms.Button
Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(Form1))
Me.AxWebBrowser1 = New AxSHDocVw.AxWebBrowser()
Me.btnExit = New System.Windows.Forms.Button()
Me.btnPrint = New System.Windows.Forms.Button()
Me.PrintDocument1 = New System.Drawing.Printing.PrintDocument()
Me.lblPageTitle = New System.Windows.Forms.Label()
Me.optMinimize = New System.Windows.Forms.Button()
Me.Splitter1 = New System.Windows.Forms.Splitter()
CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
Me.AxWebBrowser1.Enabled = True
Me.AxWebBrowser1.Location = New System.Drawing.Point(0, 16)
Me.AxWebBrowser1.OcxState = CType(resources.GetObject("AxWebBrowser1.OcxState"), System.Windows.Forms.AxHost.State)
Me.AxWebBrowser1.Size = New System.Drawing.Size(848, 632)
Me.AxWebBrowser1.TabIndex = 0
Me.btnExit.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.btnExit.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnExit.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnExit.ForeColor = System.Drawing.Color.White
Me.btnExit.Location = New System.Drawing.Point(824, 0)
Me.btnExit.Name = "btnExit"
Me.btnExit.Size = New System.Drawing.Size(24, 16)
Me.btnExit.TabIndex = 1
Me.btnExit.Text = "X"
Me.btnPrint.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.btnPrint.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.btnPrint.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.btnPrint.ForeColor = System.Drawing.Color.White
Me.btnPrint.Name = "btnPrint"
Me.btnPrint.Size = New System.Drawing.Size(64, 24)
Me.btnPrint.TabIndex = 2
Me.btnPrint.Text = "Print"
Me.lblPageTitle.BackColor = System.Drawing.Color.Transparent
Me.lblPageTitle.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.lblPageTitle.ForeColor = System.Drawing.Color.White
Me.lblPageTitle.Location = New System.Drawing.Point(80, 0)
Me.lblPageTitle.Name = "lblPageTitle"
Me.lblPageTitle.Size = New System.Drawing.Size(680, 16)
Me.lblPageTitle.TabIndex = 3
Me.optMinimize.BackColor = System.Drawing.Color.FromArgb(CType(192, Byte), CType(0, Byte), CType(0, Byte))
Me.optMinimize.FlatStyle = System.Windows.Forms.FlatStyle.Popup
Me.optMinimize.Font = New System.Drawing.Font("Microsoft Sans Serif", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.optMinimize.ForeColor = System.Drawing.Color.White
Me.optMinimize.Location = New System.Drawing.Point(792, 0)
Me.optMinimize.Name = "optMinimize"
Me.optMinimize.Size = New System.Drawing.Size(24, 16)
Me.optMinimize.TabIndex = 4
Me.optMinimize.Text = "--"
Me.Splitter1.Name = "Splitter1"
Me.Splitter1.Size = New System.Drawing.Size(3, 638)
Me.Splitter1.TabIndex = 5
Me.Splitter1.TabStop = False
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.Maroon
Me.ClientSize = New System.Drawing.Size(848, 638)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Splitter1, Me.optMinimize, Me.lblPageTitle, Me.btnPrint, Me.btnExit, Me.AxWebBrowser1})
Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None
Me.Name = "Form1"
Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
Me.Text = "Form1"
CType(Me.AxWebBrowser1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AxWebBrowser1.Navigate("http://www.bbc.co.uk")
lblPageTitle.Text() = AxWebBrowser1.Name.ToString
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_SELECTALL, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_COPY, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_DONTPROMPTUSER)
Print()
End Sub
Public Sub Print()
Try
Forms.SendKeys.SendWait("%{PRTSC}")
Dim pd As New Printing.PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
pd.DocumentName = "PrintScreen"
With pd.DefaultPageSettings
.Landscape = False
'-- Reduce margins to accomodate larger pictures
.Margins.Left -= .Margins.Left \ 3
.Margins.Top -= .Margins.Top \ 3
.Margins.Right -= .Margins.Right \ 3
.Margins.Bottom -= .Margins.Bottom \ 3
End With
'-- Print document
pd.Print()
Catch ex As Exception
Throw ex
End Try
End Sub
Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As Printing.PrintPageEventArgs)
Try
'-- Get PrintScreen bitmap from clipboard
Dim iData As IDataObject = Clipboard.GetDataObject
If iData.GetDataPresent(DataFormats.Bitmap) Then
Dim bm As Bitmap = CType(iData.GetData(DataFormats.Bitmap), Bitmap)
'-- Set size of destination rectangle (printer)
Dim rDest As New RectangleF(ev.MarginBounds.Left, ev.MarginBounds.Top, 0, 0)
If bm.Width < ev.MarginBounds.Width Then
'-- Set to bitmap size if bitmap is smaller than paper
rDest.Width = bm.Width
rDest.Height = bm.Height
Else
'-- Set to paper size if bitmap is larger than paper
'-- Scale height to retain proportions
rDest.Width = ev.MarginBounds.Width
rDest.Height = CType(rDest.Width * (bm.Height / bm.Width), Single)
End If
'-- Draw bitmap
ev.Graphics.DrawImage(bm, rDest, bm.GetBounds(GraphicsUnit.Pixel), GraphicsUnit.Pixel)
'-- Draw timestamp
Dim printFont As Font = New Font("Arial", 10, FontStyle.Regular)
ev.Graphics.DrawString("End of job", printFont, Brushes.Black, rDest.Left, rDest.Top + rDest.Height + printFont.GetHeight(ev.Graphics))
Else
'-- No bitmap, cancel printing
ev.Cancel = True
End If
Catch ex As Exception
'-- Cancel printing
ev.Cancel = True
Throw ex
Finally
'-- Print only on page
ev.HasMorePages = False
End Try
End Sub
Private Sub optMinimize_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optMinimize.Click
On Error Resume Next
AxWebBrowser1.ExecWB(SHDocVw.OLECMDID.OLECMDID_PRINT, SHDocVw.OLECMDEXECOPT.OLECMDEXECOPT_PROMPTUSER)
End Sub
End Class
Long I know. As you can see I do have two forms of print on here already, on takes a screen shot of the form (giving me background colouring, but on part of the page) the second print out the page like IE (full, but no background colours)
Any suggestions?