By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,839 Members | 2,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,839 IT Pros & Developers. It's quick & easy.

Printing to Generic Printer

P: n/a
I have a small VB program that has a printing module...very simple....and
works great. However, If I try to print to a generic printer, I get the
following error: "The data area passed to a system call is too small".

I found the following article, that I assume is similar to my problem, which
is of little help:
http://support.microsoft.com/default...b;en-us;822779

Any suggestions?

Thanks. -Rob T.
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
* "Rob T" <RT*********@DONTwalchemSPAM.com> scripsit:
I have a small VB program that has a printing module...very simple....and
works great. However, If I try to print to a generic printer, I get the
following error: "The data area passed to a system call is too small".


Post the code you are using to print to the printer.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #2

P: n/a
It's the code taken right from the MSDN example. Try it...if you install a
generic printer (or print to a file as generic) it will fail.
;-)

Public Class PrintingExample
Private printFont As Font
Private streamToPrint As StreamReader
Private Shared filePath As String

Public Sub New()
Printing()
End Sub

' The PrintPage event is raised for each page to be printed.
Private Sub pd_PrintPage(sender As Object, ev As PrintPageEventArgs)
Dim linesPerPage 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 = Nothing

' Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics)

' Iterate over the file, printing each line.
While count < linesPerPage
line = streamToPrint.ReadLine()
If line Is Nothing Then
Exit While
End If
yPos = topMargin + count * printFont.GetHeight(ev.Graphics)
ev.Graphics.DrawString(line, printFont, Brushes.Black,
leftMargin, _
yPos, New StringFormat())
count += 1
End While

' If more lines exist, print another page.
If Not (line Is Nothing) Then
ev.HasMorePages = True
Else
ev.HasMorePages = False
End If
End Sub

' Print the file.
Public Sub Printing()
Try
streamToPrint = New StreamReader(filePath)
Try
printFont = New Font("Arial", 10)
Dim pd As New PrintDocument()
AddHandler pd.PrintPage, AddressOf pd_PrintPage
' Print the document.
pd.Print()
Finally
streamToPrint.Close()
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Printing

' This is the main entry point for the application.
Public Shared Sub Main()
Dim args() As String = System.Environment.GetCommandLineArgs()
Dim sampleName As String = args(0)
If args.Length <> 1 Then
Console.WriteLine("Usage: " & sampleName & " <file path>")
Return
End If
filePath = args(0)
End Sub
End Class

[C#]
public class PrintingExample
{
private Font printFont;
private StreamReader streamToPrint;
static string filePath;
public PrintingExample()
{
Printing();
}

// The PrintPage event is raised for each page to be printed.
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = ev.MarginBounds.Left;
float topMargin = ev.MarginBounds.Top;
String line=null;

// Calculate the number of lines per page.
linesPerPage = ev.MarginBounds.Height /
printFont.GetHeight(ev.Graphics) ;

// Iterate over the file, printing each line.
while (count < linesPerPage &&
((line=streamToPrint.ReadLine()) != null))
{
yPos = topMargin + (count * printFont.GetHeight(ev.Graphics));
ev.Graphics.DrawString (line, printFont, Brushes.Black,
leftMargin, yPos, new StringFormat());
count++;
}

// If more lines exist, print another page.
if (line != null)
ev.HasMorePages = true;
else
ev.HasMorePages = false;
}

// Print the file.
public void Printing()
{
try
{
streamToPrint = new StreamReader (filePath);
try
{
printFont = new Font("Arial", 10);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}

// This is the main entry point for the application.
public static void Main(string[] args)
{
string sampleName = Environment.GetCommandLineArgs()[0];
if(args.Length != 1)
{
Console.WriteLine("Usage: " + sampleName +" <file path>");
return;
}
filePath = args[0];
new PrintingExample();
}
}
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2j************@uni-berlin.de...
* "Rob T" <RT*********@DONTwalchemSPAM.com> scripsit:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the
following error: "The data area passed to a system call is too small".


Post the code you are using to print to the printer.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #3

P: n/a
* "Rob T" <RT*********@DONTwalchemSPAM.com> scripsit:
It's the code taken right from the MSDN example. Try it...if you install a
generic printer (or print to a file as generic) it will fail.


I remember I posted this link some days ago...

HOW TO: Send Raw Data to a Printer by Using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;322090>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 20 '05 #4

P: n/a
Thanks. This seems like a complex way to simply print some raw data. I'm
curious to find how Microsoft print their 'test page' in the printer
properties... I would have thought that they would have just stripped out
the graphics and printed the text......
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:2j************@uni-berlin.de...
* "Rob T" <RT*********@DONTwalchemSPAM.com> scripsit:
It's the code taken right from the MSDN example. Try it...if you install a generic printer (or print to a file as generic) it will fail.


I remember I posted this link some days ago...

HOW TO: Send Raw Data to a Printer by Using Visual Basic .NET
<URL:http://support.microsoft.com/?scid=kb;EN-US;322090>

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.