How to prevent LineFeed after finish printing to the Parallel Printer

  • Thread starter Thread starter Jeff0803
  • Start date Start date
J

Jeff0803

Guest
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 = "GRAHAM / DAVID MR " + Environment.NewLine +
" " + Environment.NewLine +
"HEATHROW - LONDON XS 1794 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)
{
/*
PrintDocument p = new PrintDocument();
p.PrintPage += delegate(object sender1, PrintPageEventArgs e1)
{
e1.Graphics.DrawString(strTicket, new Font("Times New Roman", 12), new SolidBrush(Color.Black), new RectangleF(0, 0, p.DefaultPageSettings.PrintableArea.Width, p.DefaultPageSettings.PrintableArea.Height));

};
try
{
p.Print();
}
catch (Exception ex)
{
throw new Exception("Exception Occured While Printing", ex);
}
*/
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;
}
}
}

Continue reading...
 
Back
Top