473,748 Members | 10,771 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_P rintPage 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 1761
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(strin g userInput)
{
streamToPrint = new StringReader(us erInput);

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

PrintDocument.P rintPage += new
PrintPageEventH andler(PrintDoc ument_PrintPage );

PrintDocument.P rint();

streamToPrint.C lose();
}
private void PrintDocument_P rintPage(object sender,
System.Drawing. Printing.PrintP ageEventArgs 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.GetHe ight(e.Graphics ) ;

while (count<linesPer Page && ((line=streamTo Print.ReadLine( ))!=null))
{
yPos = topMargin + (count * printFont.GetHe ight(e.Graphics ));
e.Graphics.Draw String (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.P rintPage += new
PrintPageEventH andler(PrintDoc ument_PrintPage );
PrintDocument.P rint();

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
PrintPageEventH andler(PrintDoc ument_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(st ring filePath)
{
using(StreamRea der sr = new StreamReader(fi lePath))
{
string line;

while ((line = sr.ReadLine()) != null)
{
if((line!=@"\n" || line!=@" \n") && fileContents!=" ")
{
fileContents = string.Concat(f ileContents, '\n', line);
}
else
{
fileContents = string.Concat(f ileContents, 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.IsWhiteSpa ce(line[i]);
i += 1;
}

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

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

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

System.Text.Str ingBuilder contents = new System.Text.Str ingBuilder();
....
if (!isBlankLine)
{
if (fileContents.L ength > 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.L ength > 0)
{
fileContents = string.Concat(f ileContents, "\n", line);
}
else
{
fileContents = line;
}

}

with

if (!isBlankLine)
{
if (fileContents.L ength > 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.Lengt h > 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
2299
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 some problems I encountered while using them. Please note that, the Regional and Language setting on my machine is using "Metric" measurement system (where the default is "US"). In this case, the measurement unit is "milimeters" and not "inches".
0
2127
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 some problems I encountered while using them. Please note that, the Regional and Language setting on my machine is using "Metric" measurement system (where the default is "US"). In this case, the measurement unit is "milimeters" and not "inches".
1
9590
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 error message "the data area passed to a system call is too small", and nothing else. With SSRS when I try to print the report from the IDE I get the same error, I can load the report up to the report server and view it with IE, then print it from...
5
3350
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 = "P:\2004worksheets\IIS_WS.doc" Set oWord = New Word.Application oWord.Documents.Add (strObjectPath) oWord.PrintOut
5
1975
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 Button1, a printdocument1, a printdialog1, a printpreviewdialog1 and the following code: Private Sub PrintDocument1_PrintPage(ByVal sender as System.Object,
1
5716
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 of a Microsoft book, "Visual Basic,Net Step by Step" in Chapter 18. All but the bottom two subroutines will open a text file, and then allow me to use the above controls, example 1. The bottom two subroutines will print a graphic file, example...
2
1831
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 graphics to printer every second. After 2 minutes it calls Printer.Enddoc and the result is printed out. Printing process is on background and I can use my program normally during this 2 minutes. Now in VB 2005 if I'm right, whole printing...
2
2539
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 locations. But After printing first page it increase paper 3cm vertically/Height so on second
8
5909
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 clients (Internet Explorer). My flash content was originally brought in via the “flash satay” method, but I have since used some server-side magic do deliver one <objecttag
18
11309
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 classes, for each of the checked rows in the GridView. This works fine in the Visual Studio 2005 development environment on localhost. But, when I move the page to the web server, I get the error "Settings to access printer...
0
8989
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9537
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6795
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.