Paybit
asked on
Printing in non standard page format from VB.net
Hi,
I'm trying to print a report (from MS reporting service) on a continous piece of paper.
I have a printer that can print continous paper an cut's the paper when he's done(at end of page).
So I'm using reporting services to generate my report (which is variable in length)
When I print from reporting services I get 2 A4 sized papers (test report is 1.5 A4 pages long)
When I export to excel I get an excel file. When I set my printers print length to 1.5A4 (+-45cm) (in the printer properties) I can print the excel on 1 page that is 45cm long.
So the goal is to print the report from vb.net code.
From code I can export the report to excel (using the reporting webservice)
And I can print the excel to the printer, bu I can't change the print length while printing the excel.
I can however change the print length using the printdocument from vb.net (see attached code)
But I can't combine the 2. So I'm unable to print the excel using printdocument.
And I can't manualy create the report using graphics in the printdocument.printpage event because the report contains barcodes and those are hard to draw (+ this is to much coding)
I've been searching to print the xls to an image and print the image in the printdocument.printpage event, but the xls to image (from code) is still a problem...
So any ideas? tia
I'm trying to print a report (from MS reporting service) on a continous piece of paper.
I have a printer that can print continous paper an cut's the paper when he's done(at end of page).
So I'm using reporting services to generate my report (which is variable in length)
When I print from reporting services I get 2 A4 sized papers (test report is 1.5 A4 pages long)
When I export to excel I get an excel file. When I set my printers print length to 1.5A4 (+-45cm) (in the printer properties) I can print the excel on 1 page that is 45cm long.
So the goal is to print the report from vb.net code.
From code I can export the report to excel (using the reporting webservice)
And I can print the excel to the printer, bu I can't change the print length while printing the excel.
I can however change the print length using the printdocument from vb.net (see attached code)
But I can't combine the 2. So I'm unable to print the excel using printdocument.
And I can't manualy create the report using graphics in the printdocument.printpage event because the report contains barcodes and those are hard to draw (+ this is to much coding)
I've been searching to print the xls to an image and print the image in the printdocument.printpage event, but the xls to image (from code) is still a problem...
So any ideas? tia
m_PrintDocument.PrinterSettings = printerSetting
Dim pk as New PaperSize("cstm",670,1850)
Dim intIdx As Integer
For intIdx = 0 To m_PrintDocument.PrinterSettings.PaperSizes.Count - 1 Step 1
If m_PrintDocument.PrinterSettings.PaperSizes(intIdx).Kind = Printing.PaperKind.Custom Then
pk.RawKind = m_PrintDocument.PrinterSettings.PaperSizes(intIdx).RawKind
m_PrintDocument.DefaultPageSettings.PaperSize = pk
End If
Next
m_PrintDocument.Print()
ASKER
I can export from Windows Reporting Services to Excel.
Im looking for:
Either a way to print the generated excel using the .net printdocument class (I need this class to set print length)
Or another way to get the report to the printer that enables me to set the print length.
I could manually draw the report (using graphics.drawstring) in the prindocument.printpage class, but this would be a lot of work, and hard to maintain the report, and it would be a thing that has to be done for all reports that go onto that printer...
Im looking for:
Either a way to print the generated excel using the .net printdocument class (I need this class to set print length)
Or another way to get the report to the printer that enables me to set the print length.
I could manually draw the report (using graphics.drawstring) in the prindocument.printpage class, but this would be a lot of work, and hard to maintain the report, and it would be a thing that has to be done for all reports that go onto that printer...
I have this code you can try....
'FORM 1
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
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
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(104, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(72, 24)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Read Excel"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(196, 12)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(72, 24)
Me.Button2.TabIndex = 1
Me.Button2.Text = "Exit"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(284, 264)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
PrintReports()
End Sub
Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
'MODULE
Imports System.IO
Imports Microsoft.Win32
Imports System.Drawing.Printing
Module modPrintExcel
Public Declare Unicode Function GetTickCount32 Lib "Kernel32" Alias "GetTickCount" () As Integer
Public sReader = "C:\Program Files\Microsoft Office\Office\Excel.exe" '<----change to your Excel Directory
Public Function InitializeDefaultPrinter()
' Set the specified printer to the default printer for this program. Return
' true if the printer was found.
Dim objprinter As PageSettings = New PageSettings
Dim strname = objprinter.PrinterSettings.PrinterName
InitializeDefaultPrinter = strname
End Function
Public Sub PrintReports()
Cursor.Current = Cursors.WaitCursor
Dim regKey As RegistryKey
Dim sPrinterDriver As String
Dim sPrinterPort As String
Dim myProcess As New Process
Dim sDefaultPrinter As String
Dim keyValue As String
'To get the Printer driver...
sDefaultPrinter = InitializeDefaultPrinter()
'If sDefaultPrinter.StartsWith("\") Then
' sDefaultPrinter = sDefaultPrinter.Substring(2)
'End If
keyValue = "Software\\Microsoft\\Windows NT\\CurrentVersion\\Print\\Printers\\" & sDefaultPrinter
regKey = Registry.LocalMachine.OpenSubKey(keyValue, False)
sPrinterDriver = regKey.GetValue("Printer Driver", 0)
sPrinterPort = regKey.GetValue("Port", 0)
regKey.Close()
Try
'THIS WILL PRINT ALL EXCELS IN DIRECTORY
Dim di As New DirectoryInfo("C:\OLD D DIR\Visual Studio Projects\SerProjects\Abajo\AW_Sheets\") '<----change to your Directory
Dim fi As FileInfo() = di.GetFiles()
Dim fiTemp As FileInfo
Dim i As Short
Dim j As Short = 20
'Run this EXE to print files sReader = "C:\Program Files\Microsoft Office\Office\Excel.exe"
Dim startInfo As New ProcessStartInfo(sReader)
startInfo.WindowStyle = ProcessWindowStyle.Hidden
startInfo.CreateNoWindow = True
startInfo.Verb = "Print"
'CHANGE IF YOU WANT TO PRINT ALL EXCEL FILES
For Each fiTemp In fi
If fiTemp.Name = "31199049.XLS" Then '<----- TEST FILE
startInfo.UseShellExecute = False
'startInfo.Arguments = "/t """ & fiTemp.FullName & """ """ & sDefaultPrinter & """ """ & sPrinterDriver & """ """ & sPrinterPort & """"
startInfo.Arguments = "/t """ & fiTemp.FullName & """"
myProcess.Start(startInfo)
End If
'
'Use delay to wait for printer to print files in 20 file units
'i += 1
'Select Case i
' Case j
' delay(2000) '2 mins.
' j += 30
'End Select
Next fiTemp
Catch Exp As Exception
MsgBox(Exp.Message & " in Print Report Procedure", MsgBoxStyle.Critical, "General Error")
End Try
Cursor.Current = Cursors.Default
End Sub
Public Sub delay(ByVal msecs As Integer)
Dim StartTime As Integer
StartTime = GetTickCount32()
While GetTickCount32() < StartTime + msecs
' do nothing
End While
End Sub
End Module
ASKER
That just prints xls's in a directory.
It doesn't change the printer settings...
It doesn't change the printer settings...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
1. a report to print to excel
2. a barcade print
Here is a simple barcode sample.
One FORM CLASS
one CLASS
you will need to download the free TTF barcode files from the internet.
Open in new window