473,387 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Printing problem

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
8 1734
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
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
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
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
> 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
0
by: Programatix | last post by:
Hi, I am working on the PrintDocument, PrintDialog, PageSetupDialog and PrintPreviewControl components of Visual Studio .NET 2003. My developement machine is running Windows XP. There are...
1
by: DCraig | last post by:
I'm having problems printing to a line printer from both Crystal Reports and SQL Server reporting services using dotnet. When I try and print a report from an application with Crystal I get the...
5
by: Stefania Scott | last post by:
I am trying to print a word document from Access. The code I've written works well in my computer but does not in the one were it is needed. Here the piece of code: 'doc path strObjectPath =...
5
by: C-Services Holland b.v. | last post by:
Hi all, I've run into a problem trying to print from vb.net (2002) in Windows 98. To test it I've setup a single form with a button and the following code: 'the form has a button called...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
2
by: Teemu | last post by:
I have an application created with VB6 and now I'm converting it to VB 2005. Conversion is not so simple because printing is so much different. In my app I have Timer-component, which is creates...
2
by: Sukh | last post by:
Hi I am stuck with a problem Can anyone help me out from this... I am printing a report on pre-printed continue paper using dot-matrix printer using vb.net. Data is printing on all the...
8
by: Neo Geshel | last post by:
Greetings. BACKGROUND: My sites are pure XHTML 1.1 with CSS 2.1 for markup. My pages are delivered as application/xhtml+xml for all non-MS web clients, and as text/xml for all MS web...
18
by: Brett | last post by:
I have an ASP.NET page that displays work orders in a GridView. In that GridView is a checkbox column. When the user clicks a "Print" button, I create a report, using the .NET Framework printing...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.