Printing to Generic Printer

Discussion in 'Microsoft VB .NET' started by Rob T, Jun 15, 2004.

  1. Rob T

    Rob T Guest

    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.aspx?scid=kb;en-us;822779

    Any suggestions?

    Thanks. -Rob T.
     
    Rob T, Jun 15, 2004
    #1
    1. Advertisements

  2. * "Rob T" <> 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/>
     
    Herfried K. Wagner [MVP], Jun 15, 2004
    #2
    1. Advertisements

  3. Rob T

    Rob T Guest

    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]" <> wrote in message
    news:...
    > * "Rob T" <> 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/>
     
    Rob T, Jun 15, 2004
    #3
  4. * "Rob T" <> 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/>
     
    Herfried K. Wagner [MVP], Jun 15, 2004
    #4
  5. Rob T

    Rob T Guest

    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]" <> wrote in message
    news:...
    > * "Rob T" <> 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/>
     
    Rob T, Jun 15, 2004
    #5
  6. Rob T

    Guest

    hi if you found solution then why you question other, why you west your time you study that article then solved your problem

    thnks

    On Tuesday, June 15, 2004 9:38:29 PM UTC+5:30, Rob T wrote:
    > 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.aspx?scid=kb;en-us;822779
    >
    > Any suggestions?
    >
    > Thanks. -Rob T.
     
    , Jun 27, 2012
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dennis

    invoke Stored Procedures through a generic class

    dennis, Sep 4, 2003, in forum: Microsoft VB .NET
    Replies:
    1
    Views:
    190
    Jay B. Harlow [MVP - Outlook]
    Sep 4, 2003
  2. Richard Brown

    Generic Control Validation

    Richard Brown, Sep 6, 2003, in forum: Microsoft VB .NET
    Replies:
    5
    Views:
    190
    Tom Spink
    Sep 7, 2003
  3. Woody Splawn

    How make this code more generic?

    Woody Splawn, Oct 13, 2003, in forum: Microsoft VB .NET
    Replies:
    13
    Views:
    265
    Peter Huang [MSFT]
    Oct 16, 2003
  4. Woody Splawn

    Generic trim code

    Woody Splawn, Jan 7, 2004, in forum: Microsoft VB .NET
    Replies:
    8
    Views:
    198
    Peter Huang
    Jan 9, 2004
  5. JackRazz
    Replies:
    8
    Views:
    556
    Bill McCarthy
    Mar 30, 2004
Loading...

Share This Page