I am having an issue with printing a text file that has multiple pages. The file is approx 19 pages but when I call printpage it will print page one and two on the same page with the text overlapping. this happens for page 3 and 4, 5 and 6 etc.
here is my code for when the print button is pressed
StreamReader sr is a global variable -
-
streamFilename = "PullSummary.txt";
-
sr = new StreamReader(streamFilename);
-
-
pd.DocumentName = streamFilename;
-
-
pd.PrinterSettings.PrinterName = "\\\\mintoms02\\Canon5000i";
-
pd.PrinterSettings.DefaultPageSettings.Margins.Top = 10;
-
pd.PrinterSettings.DefaultPageSettings.Margins.Bottom = 10;
-
pd.PrinterSettings.DefaultPageSettings.Margins.Right = 10;
-
pd.PrinterSettings.DefaultPageSettings.Margins.Left = 10;
-
//pd.PrinterSettings.
-
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
-
pd.Print();
-
Here is my PrintPage Event Handler -
private void pd_PrintPage(object sender, PrintPageEventArgs e)
-
{
-
float linesPerPage = 0;
-
float yPos = 0;
-
int count = 0;
-
float leftMargin = 10;
-
float topMargin = 10;
-
string line = null;
-
// Calculate the number of lines per page.
-
linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
-
-
line = sr.ReadLine();
-
// Print each line of the file.
-
while (count < linesPerPage-1 && (line != null))
-
{
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
-
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
-
count++;
-
line = sr.ReadLine();
-
}
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
-
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
-
-
// Check if any more pages left for printing
-
if (line != null)
-
{
-
e.HasMorePages = true;
-
}
-
else
-
{
-
e.HasMorePages = false;
-
sr.Close();
-
}
-
-
}
-
If I step through the code a dialog box "Printing Page 1" pops up as soon as the PrintPage event is raised. It will then go through sr correctly and the hasmorepages will be set to true. The second time through the dialog box with still say "Printing Page 1". It is not until the third time through that the dialog box will change to say "Printing Page 2". I have tried different printers with the same result.
Any ideas would be greatly appreciated.
8 8795
I *think* this is the problem - linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
Try getting the height of a capital M. That will be a full height character for a correct height of each line.
A lot of your calculations are based on getting the height of e.Graphics. May I suggest you set the height value to a variable at one point at the start of your routine, then use this variable throughout.
This way you can test by hardcoding values until you know approximately what value works. Then you can keep tweeking your calculation. - string line = null;
-
//int LineHeight = 5; // Nope, too small
-
// int LineHeight = 15; // Still too small
-
// int LineHeight = 75; // Way too big
-
// int LineHeight = 35; // Getting closer
-
int LineHeight = 30; // Not bad. This is what my formula need to get close to
-
-
LineHeight = printFont.GetHeight('M');
-
if (UserWantsDoubleSpace) LineHeight *=2;
-
-
// Calculate the number of lines per page.
-
linesPerPage = e.PageBounds.Height / LineHeight;
-
No this is not the issue. linesPerPage is getting the correct number of lines (69). Even if I hardcode linesPerPage to be 20, I get 20 lines of 2 pages of text overlapped on one page.
Right. But if your measurement of line HEIGHT is wrong, then your spacing calculation is wrong. - yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
-
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
Here again you are trying to calculate the new line's position off of GetHeight(e.Graphics)
If your line height is 2 instead of 20 then you will calculate a new yPos that overlaps the old yPos - which sounds likes the behavior you are getting.
Sorry about the misunderstanding. I tried other lineheingts with no success.
I double checked the line height and printFont.GetHeight(e.Graphics) does return the correct line height. I can see the first line being added to the printing page all the way to the 69th line. The function then sets e.HasMorePages to true and returns to print a new page. The issue is here, for some reason even though PrintPage gets called again it's not telling the printer that we are on a new page. The second page then overlaps perfectly ontop of the first, I can also tell by physically looking at the page. The third page will start on a new page but the fourth will overlap the third. The overlapping happens on all even pages.
I've also tried many different printers with no success.
Thank you very much for the prompt suggesstions so far.
I completely misunderstood the issue. Hard to visualize. I thought you were having line spacing problems where line2 was overlapping line1... not page2 overlapping page1
Could it be this - wwhile (count < linesPerPage-1 && (line != null))
-
{
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
-
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
-
count++;
-
line = sr.ReadLine();
-
}
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
Do you really need to ignore null lines? Do blank lines that are just C/R get counted as null? Are you overlapping by exactly the number of blank lines (between paragraphs)? I'm just thinking the counter only gets incremented on non-blank lines instead of all lines.
I completely misunderstood the issue. Hard to visualize. I thought you were having line spacing problems where line2 was overlapping line1... not page2 overlapping page1
Could it be this - wwhile (count < linesPerPage-1 && (line != null))
-
{
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
-
e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
-
count++;
-
line = sr.ReadLine();
-
}
-
yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
Do you really need to ignore null lines? Do blank lines that are just C/R get counted as null? Are you overlapping by exactly the number of blank lines (between paragraphs)? I'm just thinking the counter only gets incremented on non-blank lines instead of all lines.
There are no blank lines in the text file. It is a dumb of data from a database that I do before printing. here is a small portion of it. -
--------------------2010-04-27 Kanban A5D3--------------------
-
Pull #3 A5D3: AA 16 3 of 6 -- 2010-04-28 10:30 AM
-
Pull #6 A5D3: AA 16 4 of 6 -- 2010-04-28 10:30 AM
-
Pull #9 A5D3: AA 16 5 of 6 -- 2010-04-28 10:30 AM
-
Pull #12 A5D3: AA 16 6 of 6 -- 2010-04-28 10:30 AM
-
Pull #15 A5D3: AA 17 1 of 3 -- 2010-04-28 10:30 AM
-
Pull #18 A5D3: AA 17 2 of 3 -- 2010-04-28 10:30 AM
-
Pull #21 A5D3: AA 17 3 of 3 -- 2010-04-28 10:30 AM
-
Pull #24 A5D3: AA 18 1 of 6 -- 2010-04-28 10:30 AM
-
Pull #27 A5D3: AA 18 2 of 6 -- 2010-04-28 10:30 AM
-
Pull #30 A5D3: AA 18 3 of 6 -- 2010-04-28 10:30 AM
-
Pull #33 A5D3: AA 18 4 of 6 -- 2010-04-28 10:30 AM
-
Pull #36 A5D3: AA 18 5 of 6 -- 2010-04-28 10:30 AM
-
Pull #39 A5D3: AA 18 6 of 6 -- 2010-04-28 10:30 AM
-
Pull #42 A5D3: AA 19 1 of 6 -- 2010-04-28 10:30 AM
-
Pull #45 A5D3: AA 19 2 of 6 -- 2010-04-28 10:30 AM
-
Pull #48 A5D3: AA 19 3 of 6 -- 2010-04-28 10:30 AM
-
Pull #51 A5D3: AA 19 4 of 6 -- 2010-04-28 10:30 AM
-
Pull #54 A5D3: AA 19 5 of 6 -- 2010-04-28 10:30 AM
-
Pull #57 A5D3: AA 19 6 of 6 -- 2010-04-28 10:30 AM
-
Pull #60 A5D3: AA 20 1 of 6 -- 2010-04-28 10:30 AM
-
Pull #63 A5D3: AA 20 2 of 6 -- 2010-04-28 10:30 AM
-
Pull #85 A5D3: AA 20 3 of 6 -- 2010-04-28 10:30 AM
-
Pull #88 A5D3: AA 20 4 of 6 -- 2010-04-28 10:30 AM
-
Pull #91 A5D3: AA 20 5 of 6 -- 2010-04-28 10:30 AM
-
Pull #94 A5D3: AA 20 6 of 6 -- 2010-04-28 10:30 AM
-
--------------------2010-04-27 Kanban A718--------------------
-
Pull #93 A718: AA 13 1 of 1 -- 2010-04-28 10:30 AM
-
Pull #95 A718: AA 16 1 of 1 -- 2010-04-28 10:30 AM
-
Pull #96 A718: AA 19 1 of 1 -- 2010-04-28 10:30 AM
-
--------------------2010-04-27 Kanban C577--------------------
-
Pull #15 C577: 1R D14 1 of 1 -- 2010-04-27 4:50 PM
-
Pull #63 C577: 1R D01 1 of 1 -- 2010-04-28 8:00 AM
-
Pull #95 C577: 1R D11 1 of 1 -- 2010-04-28 1:50 PM
-
--------------------2010-04-27 Kanban C598--------------------
-
Pull #2 C598: 2A D10 4 of 5 -- 2010-04-27 11:45 AM
-
Pull #5 C598: 2A D10 5 of 5 -- 2010-04-27 11:45 AM
-
Pull #9 C598: 2A D14 1 of 6 -- 2010-04-27 2:10 PM
-
Pull #11 C598: 2A D14 2 of 6 -- 2010-04-27 2:10 PM
-
Pull #13 C598: 2A D14 3 of 6 -- 2010-04-27 2:10 PM
I'm out of idea then. Sorry
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Zentil |
last post by:
While printing reports Spooling of print document size get increased into
larger size ie the document size is 260kb but on spooling it increased to
268mb. but this is happening in some systems...
|
by: Marco Martin |
last post by:
Hi,
I have a control that prints itself. The control has a method
Control.Print(strdocName, printEnum.FitToPageWidth). I need to have better
control on how it prints itself. I'm assuming that...
|
by: abcbook |
last post by:
Can anyone explain a get around for this .js file I’m trying to create or tell me if there is one or what am I missing.
I Do Not what to put the form Directly on the HTML Page. This .js file will...
|
by: Seemaraj |
last post by:
I would like to print the data that is displayed in the datagrid of my for, i dont know how to use the print document control ,print dialog controls
please do provide me the solution
Is there any...
|
by: nikhil17 |
last post by:
hi friends pls help me out if any one know abt the print document component in .net ...
i want to adjust the page size and its margins through code .... pls reply .
|
by: vidhyapriya |
last post by:
how to change backcolor of the print document in vb.net windows appln.......
|
by: Greg Esres |
last post by:
I have some text lines to print that are much longer than the width of
the paper, maybe as much as 6 times. For a given page, I'd like
everything that doesn't fit to print on a second page, and...
|
by: thirunavukarasukm |
last post by:
Hai....
To print Html page using PrintDialog and Print Document
i am created one windows appication..
the windows application have many pages
in my booking page i have two buttons one...
|
by: yoyoy harage |
last post by:
I want user to be able to click print button and it will print document in web page.
I try to use window.print(); but it prints everything including transparent font color. I want to print document...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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...
| |