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

Printing problem

P: n/a
I have a text file that I would like to print. I load the contents of
this text file into a string, call it strData. I then pass strData
into my print method, which creates it as a StreamReader. The print
method creates an instance of the PrintDocument_PrintPage method, which
I got from the following website:

http://msdn.microsoft.com/library/de...tPageTopic.asp

My problem is that when the document actually prints out, instead of
printing two pages, it will only print one page, with multiple lines of
text printed on top of each other. Can anyone see where my error is?

Apr 3 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Could you post your code, including the method you copied from the page
to which you linked?

There are several possible sources for the problem you described. It
all depends upon the code.

Apr 4 '06 #2

P: n/a
public void PrintData(string userInput)
{
streamToPrint = new StringReader(userInput);

printFont = new Font("Times New Roman", 10);

PrintDocument.PrintPage += new
PrintPageEventHandler(PrintDocument_PrintPage);

PrintDocument.Print();

streamToPrint.Close();
}
private void PrintDocument_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
float linesPerPage = 0;
float yPos = 0;
int count = 0;
float leftMargin = e.MarginBounds.Left;
float topMargin = e.MarginBounds.Top;
string line = null;

linesPerPage = e.MarginBounds.Height /
printFont.GetHeight(e.Graphics) ;

while (count<linesPerPage && ((line=streamToPrint.ReadLine())!=null))
{
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
e.Graphics.DrawString (line, printFont, Brushes.Black, leftMargin,
yPos, new StringFormat());
count++;
}

if (line!=null)
e.HasMorePages = true;
else
e.HasMorePages = false;
}

Apr 4 '06 #3

P: n/a
I tested your code (with one modification) and it works fine for me.

I think it depends upon how you get the user input into the userInput
string. Can you post the code that does that? I simply initialized it
as follows:

string userInput = "The quick brown fox\r\njumped over the
lazy\r\ndog.";

and I got three lines of output.

Well, I got three lines of output only after I corrected something in
the code you posted, which didn't compile. The part where you say:

PrintDocument.PrintPage += new
PrintPageEventHandler(PrintDocument_PrintPage);
PrintDocument.Print();

doesn't compile, because PrintDocument is the class name, not an
instance, and the PrintPage event and the Print method are instance
members, not static members, so I changed this to:

PrintDocument pd = new PrintDocument();
pd.PrintPage += new
PrintPageEventHandler(PrintDocument_PrintPage);
pd.Print();

but that was to get it to compile at all. Once I did that, it produced
correct output.

So, my conclusion is that somehow when you load the text into the
userInput string, the line termination characters are being eliminated.
So, please post the code that loads that information into the string.

Apr 4 '06 #4

P: n/a
I have the following method load the text file.

private void LoadDataFile(string filePath)
{
using(StreamReader sr = new StreamReader(filePath))
{
string line;

while ((line = sr.ReadLine()) != null)
{
if((line!=@"\n" || line!=@" \n") && fileContents!="")
{
fileContents = string.Concat(fileContents, '\n', line);
}
else
{
fileContents = string.Concat(fileContents, line);
}
}
}
}

Suppose that the text file loaded has 85 lines. Then I don't have a
problem printing the first 56 lines, but lines 57-85 then reprint over
lines 1-28. Could it be due to the fact that I don't have the \r
character returns in my string?

Apr 4 '06 #5

P: n/a
> I don't have a problem printing the first 56 lines, but lines 57-85 then reprint over lines 1-28.

Ahh. You didn't say that before. :-)

I tried my little test again with many more lines and I got two pages
of output. I also tried with with just \n instead of \r\n and I still
got two pages of output.

The only change I made was the one I stated in my previous post.

I do notice, however, that there is something wrong with your test:

if((line!=@"\n" || line!=@" \n") && fileContents!="")

This has the following problems. First, since @"\n" and @" \n" are not
equal, the test (line != @"\n" || line != " \n") will always be true.
It can be false only when line equals BOTH @"\n" AND @" \n", which is
impossible. So, effectively the test becomes

if (fileContents != "")

In addition, I think that you don't want @"\n", but rather "\n". The
former compares against a backslash and a normal character n. The
latter against a newline character.

Finally, what you probably want to ask is whether the line is blank. I
would prefer to do this rather than what you've done:

bool isBlankLine = true;
int i = 0;
while (i < line.Length && isBlankLine)
{
isBlankLine = Char.IsWhiteSpace(line[i]);
i += 1;
}

then I think that what you want to do is this (I may be wrong):

if (!isBlankLine)
{
if (fileContents.Length > 0)
{
fileContents = string.Concat(fileContents, "\n", line);
}
else
{
fileContents = line;
}
}

Or you could do it better using a System.Text.StringBuilder:

System.Text.StringBuilder contents = new System.Text.StringBuilder();
....
if (!isBlankLine)
{
if (fileContents.Length > 0)
{
contents.Append("\n");
}
contents.Append(line);
}

Anyway, I don't see how any of this would produce your problem, since
your code works for me, even with these difficulties.

Apr 4 '06 #6

P: n/a
Thanks for the help. I'm not for sure, but I think that my problem was
I wasn't creating a new instance of the PrintDocument class, I was just
using the PrintDocument that I added to my main form. Also, thanks for
correcting my if statement. I think I initially meant it to be an &&
instead of an ||. Also, I'll look into using the StringBuilder, but am
a little unclear as to why it's better, any particular reasons?

Apr 4 '06 #7

P: n/a
Also, a question about your StringBuilder code. If I replaced

if (!isBlankLine)
{
if (fileContents.Length > 0)
{
fileContents = string.Concat(fileContents, "\n", line);
}
else
{
fileContents = line;
}

}

with

if (!isBlankLine)
{
if (fileContents.Length > 0)
{
contents.Append("\n");
}
contents.Append(line);

}

wouldn't it always add a "\n" because the fileContents string never
gets appended to, or added onto?

Apr 4 '06 #8

P: n/a
Yes it would. I should have written:

if (!isBlankLine)
{
if (contents.Length > 0)
{
contents.Append("\n");
}
contents.Append(line);
}

instead. As for why StringBuilder, I'm a little rushed right now, so
I'll cheat and let Jon Skeet explain: :-)

http://www.yoda.arachsys.com/csharp/stringbuilder.html

Apr 5 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.