Page skipping

Z

Zach

In a txt file I would like to insert a character to force a page skip,
char 12 doesn't work, any suggestion?

Many thanks.
Zach
 
A

Alberto Poblacion

Zach said:
In a txt file I would like to insert a character to force a page skip,
char 12 doesn't work, any suggestion?

That depends on the peripheral that interprets that file. If you are
pumping the file directly into a printer that supports character 12 as "form
feed", then it should work. If it doesn't, then either the printer does not
support that command, or the program that you wrote to send the bytes from
the file into the printer is doing something wrong and it is not sending
that character correctly.
 
Z

Zach

That depends on the peripheral that interprets that file. If you are
pumping the file directly into a printer that supports character 12 as
"form feed", then it should work. If it doesn't, then either the printer
does not support that command, or the program that you wrote to send the
bytes from the file into the printer is doing something wrong and it is
not sending that character correctly.
Is there anything in the code below that might furnish a clue at to why
char 12 will not force a page skip?

using System.ComponentModel;
using System.Drawing;
using System.Drawing.Printing;
using System.IO;

namespace InfraStructure
{
public class Print
{
private Font printFont;
private StreamReader streamToPrint;
public string file_path;
public static int reset;

public Print(string file_path)
{
this.file_path = file_path;
execute();
}

// 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 leftMargin = 10;
float topMargin = ev.MarginBounds.Top;
if (reset!=0)
topMargin = reset;
string line=null;

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

// Iterate over the file, No_Frills 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 execute()
{
try
{
streamToPrint = new StreamReader (file_path);
try
{
printFont = new Font("Courier New", 9);
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
// Print the document.
pd.Print();
}
finally
{
streamToPrint.Close() ;
}
}
catch
{
}
}
}
}
 
R

Random

Is there anything in the code below that might furnish a clue at to why
char 12 will not force a page skip?

If you're expecting a form feed character in the document the class is
reading in to skip a page, you'll need to handle that yourself in the
PrintPage handler.
 
A

Alberto Poblacion

Zach said:
Is there anything in the code below that might furnish a clue at to why
char 12 will not force a page skip?

[...]
ev.Graphics.DrawString (line, printFont, Brushes.Black,leftMargin, yPos,
new StringFormat());

Yes, you are not sending text to the printer; you are sending GRAPHICS.
If the text contains a char 12, it never gets sent to the printer; instead,
GDI+ tries to render a pixel-by-pixel representation of that character,
which most probably will result in a little square (unless your printFont
contains a symbol for char 12).

If you want to print with a PrintDocument object, and you want to skip to
the next page when the input file contains a char 12, you will have to
handle this situation in your code. Basically, you will have to scan the
"line" variable that you read from the file to see if it contains tha char
12. If it does, you have to exit the loop that is printing lines, and set
ev.HasMorePages=true. Then you exit the PrintPage event handler. The handler
will be fired again immediately (due to the ev.HasMorePages=true), and you
should use it to continue printing from the point where you exited the loop
due to the char 12. The new lines that you print will appear in a new page.
Note that this will take some programming work: you will need to maintain
some state variables outside of the event handler to keep track of what you
were doing and what you need to print next. It is not a simple matter of
just adding a single line of code and being done.

Also note that there *may* be an added difficculty in the part where you
scan the line for a char 12. Since you are reading through a StreamReader,
the characters get mapped through the default encoding. I don't know right
away if the char 12 will make it through the encoding or not. If it doesn't,
you may have to resort to reading the file directly with a FileStream
instead of a StreamReader.
 
Z

Zach

Zach said:
Is there anything in the code below that might furnish a clue at to
why char 12 will not force a page skip?

[...]
ev.Graphics.DrawString (line, printFont, Brushes.Black,leftMargin,
yPos, new StringFormat());

Yes, you are not sending text to the printer; you are sending GRAPHICS.
If the text contains a char 12, it never gets sent to the printer;
instead, GDI+ tries to render a pixel-by-pixel representation of that
character, which most probably will result in a little square (unless
your printFont contains a symbol for char 12).

If you want to print with a PrintDocument object, and you want to skip
to the next page when the input file contains a char 12, you will have
to handle this situation in your code. Basically, you will have to scan
the "line" variable that you read from the file to see if it contains
tha char 12. If it does, you have to exit the loop that is printing
lines, and set ev.HasMorePages=true. Then you exit the PrintPage event
handler. The handler will be fired again immediately (due to the
ev.HasMorePages=true), and you should use it to continue printing from
the point where you exited the loop due to the char 12. The new lines
that you print will appear in a new page.
Note that this will take some programming work: you will need to
maintain some state variables outside of the event handler to keep track
of what you were doing and what you need to print next. It is not a
simple matter of just adding a single line of code and being done.

Also note that there *may* be an added difficculty in the part where you
scan the line for a char 12. Since you are reading through a
StreamReader, the characters get mapped through the default encoding. I
don't know right away if the char 12 will make it through the encoding
or not. If it doesn't, you may have to resort to reading the file
directly with a FileStream instead of a StreamReader.
Thank you very much for your comprehensive response. After considering
your words, and the difficulties implied, I will go a different route.
What I am currently doing is creating a text file and inputting that
text file to the print object. The change I shall make to incorporate
page skipping, is to deliver separate text packages to the print object.
That would seem to get around the required more advanced coding of the
print object per se to do the skipping.

Many thanks,
Regards,
Zach
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top