473,407 Members | 2,306 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,407 software developers and data experts.

Print Document PrintPage problem

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

Expand|Select|Wrap|Line Numbers
  1.  
  2.             streamFilename = "PullSummary.txt";
  3.             sr = new StreamReader(streamFilename);
  4.  
  5.             pd.DocumentName = streamFilename;
  6.  
  7.             pd.PrinterSettings.PrinterName = "\\\\mintoms02\\Canon5000i";
  8.             pd.PrinterSettings.DefaultPageSettings.Margins.Top = 10;
  9.             pd.PrinterSettings.DefaultPageSettings.Margins.Bottom = 10;
  10.             pd.PrinterSettings.DefaultPageSettings.Margins.Right = 10;
  11.             pd.PrinterSettings.DefaultPageSettings.Margins.Left = 10;
  12.             //pd.PrinterSettings.
  13.             pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
  14.             pd.Print();
  15.  
Here is my PrintPage Event Handler

Expand|Select|Wrap|Line Numbers
  1. private void pd_PrintPage(object sender, PrintPageEventArgs e)
  2.         {
  3.             float linesPerPage = 0;
  4.             float yPos = 0;
  5.             int count = 0;
  6.             float leftMargin = 10;
  7.             float topMargin = 10;
  8.             string line = null;
  9.             // Calculate the number of lines per page.
  10.             linesPerPage = e.PageBounds.Height / printFont.GetHeight(e.Graphics);
  11.  
  12.             line = sr.ReadLine();
  13.             // Print each line of the file.
  14.             while (count < linesPerPage-1 && (line != null))
  15.             {
  16.                 yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
  17.                 e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
  18.                 count++;
  19.                 line = sr.ReadLine();
  20.             }
  21.             yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
  22.             e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
  23.  
  24.             // Check if any more pages left for printing
  25.             if (line != null)
  26.             {
  27.                 e.HasMorePages = true;
  28.             }
  29.             else
  30.             {
  31.                 e.HasMorePages = false;
  32.                 sr.Close();
  33.             }
  34.  
  35.         }
  36.  
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.
Apr 28 '10 #1
8 8795
tlhintoq
3,525 Expert 2GB
I *think* this is the problem
Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. string line = null;
  2. //int LineHeight = 5; // Nope, too small
  3. // int LineHeight = 15; // Still too small
  4. // int LineHeight = 75; // Way too big
  5. // int LineHeight = 35; // Getting closer
  6. int LineHeight = 30; // Not bad.  This is what my formula need to get close to
  7.  
  8. LineHeight = printFont.GetHeight('M');
  9. if (UserWantsDoubleSpace) LineHeight *=2;
  10.  
  11.             // Calculate the number of lines per page.
  12.             linesPerPage = e.PageBounds.Height / LineHeight;
  13.  
Apr 28 '10 #2
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.
Apr 28 '10 #3
tlhintoq
3,525 Expert 2GB
Right. But if your measurement of line HEIGHT is wrong, then your spacing calculation is wrong.

Expand|Select|Wrap|Line Numbers
  1. yPos = topMargin + (count * printFont.GetHeight(e.Graphics));
  2.             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.
Apr 28 '10 #4
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.
Apr 28 '10 #5
tlhintoq
3,525 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. wwhile (count < linesPerPage-1 && (line != null))
  2.             {
  3.                 yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
  4.                 e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
  5.                 count++;
  6.                 line = sr.ReadLine();
  7.             }
  8.             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.
Apr 28 '10 #6
tlhintoq
3,525 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. wwhile (count < linesPerPage-1 && (line != null))
  2.             {
  3.                 yPos = topMargin + (count *  printFont.GetHeight(e.Graphics));
  4.                 e.Graphics.DrawString(line, printFont, Brushes.Black, leftMargin, yPos, new StringFormat());
  5.                 count++;
  6.                 line = sr.ReadLine();
  7.             }
  8.             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.
Apr 28 '10 #7
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.

Expand|Select|Wrap|Line Numbers
  1. --------------------2010-04-27 Kanban A5D3--------------------
  2. Pull #3 A5D3: AA 16  3 of 6 -- 2010-04-28 10:30 AM
  3. Pull #6 A5D3: AA 16  4 of 6 -- 2010-04-28 10:30 AM
  4. Pull #9 A5D3: AA 16  5 of 6 -- 2010-04-28 10:30 AM
  5. Pull #12 A5D3: AA 16  6 of 6 -- 2010-04-28 10:30 AM
  6. Pull #15 A5D3: AA 17  1 of 3 -- 2010-04-28 10:30 AM
  7. Pull #18 A5D3: AA 17  2 of 3 -- 2010-04-28 10:30 AM
  8. Pull #21 A5D3: AA 17  3 of 3 -- 2010-04-28 10:30 AM
  9. Pull #24 A5D3: AA 18  1 of 6 -- 2010-04-28 10:30 AM
  10. Pull #27 A5D3: AA 18  2 of 6 -- 2010-04-28 10:30 AM
  11. Pull #30 A5D3: AA 18  3 of 6 -- 2010-04-28 10:30 AM
  12. Pull #33 A5D3: AA 18  4 of 6 -- 2010-04-28 10:30 AM
  13. Pull #36 A5D3: AA 18  5 of 6 -- 2010-04-28 10:30 AM
  14. Pull #39 A5D3: AA 18  6 of 6 -- 2010-04-28 10:30 AM
  15. Pull #42 A5D3: AA 19  1 of 6 -- 2010-04-28 10:30 AM
  16. Pull #45 A5D3: AA 19  2 of 6 -- 2010-04-28 10:30 AM
  17. Pull #48 A5D3: AA 19  3 of 6 -- 2010-04-28 10:30 AM
  18. Pull #51 A5D3: AA 19  4 of 6 -- 2010-04-28 10:30 AM
  19. Pull #54 A5D3: AA 19  5 of 6 -- 2010-04-28 10:30 AM
  20. Pull #57 A5D3: AA 19  6 of 6 -- 2010-04-28 10:30 AM
  21. Pull #60 A5D3: AA 20  1 of 6 -- 2010-04-28 10:30 AM
  22. Pull #63 A5D3: AA 20  2 of 6 -- 2010-04-28 10:30 AM
  23. Pull #85 A5D3: AA 20  3 of 6 -- 2010-04-28 10:30 AM
  24. Pull #88 A5D3: AA 20  4 of 6 -- 2010-04-28 10:30 AM
  25. Pull #91 A5D3: AA 20  5 of 6 -- 2010-04-28 10:30 AM
  26. Pull #94 A5D3: AA 20  6 of 6 -- 2010-04-28 10:30 AM
  27. --------------------2010-04-27 Kanban A718--------------------
  28. Pull #93 A718: AA 13  1 of 1 -- 2010-04-28 10:30 AM
  29. Pull #95 A718: AA 16  1 of 1 -- 2010-04-28 10:30 AM
  30. Pull #96 A718: AA 19  1 of 1 -- 2010-04-28 10:30 AM
  31. --------------------2010-04-27 Kanban C577--------------------
  32. Pull #15 C577: 1R D14 1 of 1 -- 2010-04-27 4:50 PM
  33. Pull #63 C577: 1R D01 1 of 1 -- 2010-04-28 8:00 AM
  34. Pull #95 C577: 1R D11 1 of 1 -- 2010-04-28 1:50 PM
  35. --------------------2010-04-27 Kanban C598--------------------
  36. Pull #2 C598: 2A D10 4 of 5 -- 2010-04-27 11:45 AM
  37. Pull #5 C598: 2A D10 5 of 5 -- 2010-04-27 11:45 AM
  38. Pull #9 C598: 2A D14 1 of 6 -- 2010-04-27 2:10 PM
  39. Pull #11 C598: 2A D14 2 of 6 -- 2010-04-27 2:10 PM
  40. Pull #13 C598: 2A D14 3 of 6 -- 2010-04-27 2:10 PM
Apr 28 '10 #8
tlhintoq
3,525 Expert 2GB
I'm out of idea then. Sorry
Apr 28 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

0
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...
0
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...
1
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...
1
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...
1
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 .
0
by: vidhyapriya | last post by:
how to change backcolor of the print document in vb.net windows appln.......
6
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...
0
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...
2
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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
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
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,...
0
tracyyun
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...
0
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...

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.