Print Word Document
-
i am trying to print word document in VB .net. it is printing the document but it is priting couple of lines of boxes in the beginning. any idea why is doing so. here is the code: Public Sub PrintDoc() Dim strPCLPrint As String strPCLPrint = Trim(lblPCLPrinter.Text.ToString) 'ByVal strDocFile As String 'pcl Print driver Try StreamToPrint = New StreamReader("C:\temp\6031044.doc") Try Dim pr As New PrinterSettings() pr.PrinterName = strPCLPrint MsgBox(pr.PrinterName) printFont = New Font("Arial", 10) Dim pd As PrintDocument = New PrintDocument() 'sets the PCL printer for doc printing AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage 'pd.CreateObjRef() pd.DocumentName = "C:\temp\6031044.doc" pd.Print() Catch ex As Exception MessageBox.Show(ex.ToString) Finally StreamToPrint.Close() End Try Catch ex As Exception MessageBox.Show("An error occurred printing the file - " + ex.Message) End Try End Sub Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Dim lpp As Single = 0 Dim yPos As Single = 0 Dim count As Integer = 0 Dim leftMargin As Single = ev.MarginBounds.Left Dim topMargin As Single = ev.MarginBounds.Top Dim line As String 'Work out the number of lines per page 'Use the MarginBounds on the event to do this lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) 'Now iterate over the file printing out each line 'NOTE WELL: This assumes that a single line is not wider than the page width 'Check count first so that we don't read line that we won't print line = StreamToPrint.ReadLine() While (count < lpp And line <> Nothing) yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)) 'Print Preview control will not work. ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _ yPos, New StringFormat()) count = count + 1 If (count < lpp) Then line = StreamToPrint.ReadLine() End If End While 'If we have m
-
i am trying to print word document in VB .net. it is printing the document but it is priting couple of lines of boxes in the beginning. any idea why is doing so. here is the code: Public Sub PrintDoc() Dim strPCLPrint As String strPCLPrint = Trim(lblPCLPrinter.Text.ToString) 'ByVal strDocFile As String 'pcl Print driver Try StreamToPrint = New StreamReader("C:\temp\6031044.doc") Try Dim pr As New PrinterSettings() pr.PrinterName = strPCLPrint MsgBox(pr.PrinterName) printFont = New Font("Arial", 10) Dim pd As PrintDocument = New PrintDocument() 'sets the PCL printer for doc printing AddHandler pd.PrintPage, AddressOf Me.pd_PrintPage 'pd.CreateObjRef() pd.DocumentName = "C:\temp\6031044.doc" pd.Print() Catch ex As Exception MessageBox.Show(ex.ToString) Finally StreamToPrint.Close() End Try Catch ex As Exception MessageBox.Show("An error occurred printing the file - " + ex.Message) End Try End Sub Private Sub pd_PrintPage(ByVal sender As Object, ByVal ev As System.Drawing.Printing.PrintPageEventArgs) Dim lpp As Single = 0 Dim yPos As Single = 0 Dim count As Integer = 0 Dim leftMargin As Single = ev.MarginBounds.Left Dim topMargin As Single = ev.MarginBounds.Top Dim line As String 'Work out the number of lines per page 'Use the MarginBounds on the event to do this lpp = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics) 'Now iterate over the file printing out each line 'NOTE WELL: This assumes that a single line is not wider than the page width 'Check count first so that we don't read line that we won't print line = StreamToPrint.ReadLine() While (count < lpp And line <> Nothing) yPos = topMargin + (count * printFont.GetHeight(ev.Graphics)) 'Print Preview control will not work. ev.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, _ yPos, New StringFormat()) count = count + 1 If (count < lpp) Then line = StreamToPrint.ReadLine() End If End While 'If we have m
There is a great example of printing Word Documents to a Fax Device or other specific printer on www.gotdotnet.com. The site is currently down or I would provide you with a direct link. When it is available again just search for SendMSWordDocumentViaFaxExample.