none
How to prevent LineFeed after finish printing to the Parallel Printer RRS feed

  • Question

  • I made code to print text lines to the parallel printer using PrintDocument class.

    Those text lines are just 5 lines.

    It prints well but it always scrolls(linefeed) fit to one page.

    I want to prevent from line feed.

    How could I do this?

    Here is the code which is a bit modified from the PrintDocument class's sample code.

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Runtime.InteropServices;
    using Microsoft.Win32.SafeHandles;
    using System.Drawing.Printing;
    
    namespace ParallelPortTest
    {
        public partial class Form1 : Form
        {
            FileStream filestream;
            string strTicket = "DOE / JOHN     MR                        " + Environment.NewLine +
                               "                                         " + Environment.NewLine +
                               "HEATHROW - LONDON  AA 1234  C 25MAR 21:00" + Environment.NewLine +
                               "FRANKFURT                                " + Environment.NewLine +
                               "                                      28G";
            private StreamReader streamToPrint;
            private Font printFont;
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                this.tbSendText.Text = strTicket;
    
            }
    
    
        private void bttnSpool_Click(object sender, EventArgs e)
        {
    
                try
                {
                    // convert string to stream
                    byte[] byteArray = Encoding.UTF8.GetBytes(this.tbSendText.Text);
                    //byte[] byteArray = Encoding.ASCII.GetBytes(contents);
                    MemoryStream stream = new MemoryStream(byteArray);
                    // convert stream to string
                    streamToPrint = new StreamReader(stream);
                    try
                    {
                        printFont = new Font("Lucida Console", 12);
                        PrintDocument pd = new PrintDocument();
                        pd.PrintPage += new PrintPageEventHandler
                           (this.pd_PrintPage);
                        pd.Print();
                    }
                    finally
                    {
                        streamToPrint.Close();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
        }
    
    // 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;
                float leftMargin = 10;
                float topMargin = 10;
                string line = null;
    
    
                // Calculate the number of lines per page.
                linesPerPage = Convert.ToSingle(this.tbSendText.Text.Split('\n').Length) + 1;
    
                // Print each line of the file.
                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++;
                }
                ev.HasMorePages = false;
            }
        }
    }


    • Edited by Jeff0803 Thursday, September 26, 2019 8:55 PM
    Thursday, September 26, 2019 8:06 PM

All replies

  • Hi Jeff0803,

    Thank you for posting here.

    If you want to prevent line feed, why you used Environment.NewLine to make it line feed? It confused me.

    So we only need to change the string to the following to solve line feed problem.

            string strTicket = "DOE / JOHN     MR       " + "HEATHROW - LONDON  AA 1234  C 25MAR 21:00"+ "FRANKFURT           "+ "         28G";

    Please feel free to let me know if this solution is not working or I misunderstand your meaning.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, September 27, 2019 1:58 AM
    Moderator
  • No I mean I don't want to have paper scroll down after print all lines.

    There are 5 text lines to print and those each lines should be as is.

    After print all lines(after print "28G"), paper should not scroll and stop.

    However currently paper scroll to fit to one page. I want to prevent this.


    • Edited by Jeff0803 Friday, September 27, 2019 4:04 PM
    Friday, September 27, 2019 1:08 PM
  • Hi Jeff,

    I've never used the PrintDocument class, so take this suggestion with a grain of salt. I wonder if there is some way to set the PaperSize dynamically (so that it will differ for whatever you're printing) … you could set the PaperSize to correspond to whatever you calculate to be the end of the lines of data that you're printing (or one line after that). That way, when it "scrolls to the end of the page", it would only "scroll" one line, not to the real end of the page.

    I have no idea if you can do that, I've Googled a few things but could not find anything about your particular problem. I looked at the PrintDocument documentation, and when I saw something about the PaperSize property, I thought maybe you could "fudge it" somehow using that. Worth a try anyway … I would have tried it myself, but I don't currently have a printer hooked up that I could test with.


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Jeff0803 Monday, September 30, 2019 8:04 PM
    • Unmarked as answer by Jeff0803 Monday, September 30, 2019 8:13 PM
    Friday, September 27, 2019 4:09 PM
    Moderator
  • The Windows printing model is page-oriented.  That's just fundamental.  You're not communicating with the printer, you're talking to the print subsystem and the print spooler.  The lines you write get wrapped up into a "document", and that document travels through the system as a unit.  Have you looked at the print options for your printer driver to see if it has a "continuous" page size option?

    If not, then I suggest you skip PrintDocument altogether, and just open the special file "LPT1".  You can write lines to that as if it were a file, and the raw bytes will be sent directly to the printer, without involving the driver or the print spooler.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Friday, September 27, 2019 4:46 PM
  • First I tried printing to "LPT1" using Win32API call.

    However the result was a little bit different from the source text like following.

    (Source)

    "DOE/JOHN MR                              "
    "                                         "
    "HEATHROW - LONDON  AA 1234  C 25MAR 21:00"
    "FRANKFURT                                "
    "                                      28G"

    (Result)

    "DOE/JOHN MR                              "
    "                                         "
    "HEATHROW - LONDON  AA 1234  C 25MAR 21:00"
    "FRANKFURT                                "
    "                    28GDOE/JOHN MR       "

    I couldn't figure out why DOE/JOHN MR is attached to the 28G.

    So I switched to printer spooler.

    Monday, September 30, 2019 8:13 PM
  • The only way that could happen is if you sent the "DOE/JOHN MR" again.  You didn't show us that code, so we can't comment on the reason.  Note that you certainly want to send CR and LF after your final string.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, October 1, 2019 3:58 AM