472,108 Members | 1,463 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Printing a document with multiple pages (page breaks)

5
Hello!

I need to print two pages using PrintDocument, but there are some problems.

I've got something like that:
-button is pressed
-two pages need to be printed

The code is like that:

Expand|Select|Wrap|Line Numbers
  1. PrintDocument print_tab1 = new PrintDocument();
  2.  
  3. in main():
  4.         print_tab1.PrintPage += new PrintPageEventHandler(printTab1);
  5.  
  6.         private void print_tab1_but_Click(object sender, EventArgs e)
  7.         {
  8.  
  9.             PrintDialog pd = new PrintDialog();
  10.             pd.Document = print_tab1;
  11.             if (pd.ShowDialog() == DialogResult.OK)
  12.             {
  13.                 print_tab1.Print();
  14.             }
  15.         }
  16.  
  17.         private void printTab1(Object sender, PrintPageEventArgs e)
  18.         {
  19.             e.Graphics.DrawString("page 1", new Font("Times new roman", 14, FontStyle.Bold), Brushes.Black, 60, 10);
  20.             e.HasMorePages = true;
  21.             e.Graphics.DrawString("page 2", new Font("Times new roman", 14, FontStyle.Bold), Brushes.Black, 60, 10);
  22.             e.HasMorePages = false;
  23.         }
I've seen the following post: http://www.thescripts.com/forum/thread104989.html
But I can't figure out how to insert page break BEFORE the page actually ends and print the data that is FOMED IN THE SAME METHOD.

I understand that after printing a single page the method exits. But it doesn't when the page still has space to print on it. So I've got "page 1" and "page 2" on the same page.
And if I return from printTab1 explicitly, than how should I call it again and print ONLY the second page?

Would appreciate any help.
Jan 26 '07 #1
4 14120
evll
5
So? No one can help?
Jan 28 '07 #2
kenobewan
4,871 Expert 4TB
Here is an example that may help:
Printing in C#

Note the use of hasmorepages...
Jan 28 '07 #3
evll
5
As I stated in my first post I've seen a very similar example, where a block of text is printed. But what I need is two words example - one word on the first page and the other on the second. "while" is not suitable for this. For better understanding see my simple code snippet (which doesn't work, but you'll get the idea).
Jan 29 '07 #4
evll
5
Ok. Managed to find a solution all by myself.
In case someone has the same problem in teh future:
Expand|Select|Wrap|Line Numbers
  1. int page = 1;
  2. private void printTab1(Object sender, PrintPageEventArgs e)
  3. {
  4.   if (page == 1)
  5.   {
  6.     e.Graphics.DrawString("page1", new Font("Times new roman", 14, FontStyle.Bold), Brushes.Black, 60, 10);
  7.     e.HasMorePages = true;
  8.   }
  9.   if (page == 2)
  10.   {
  11.     e.Graphics.DrawString("page2", new Font("Times new roman", 14, FontStyle.Bold), Brushes.Black, 60, 10);
  12.     e.HasMorePages = false;
  13.   }
  14.   page++;
  15. }
Jan 29 '07 #5

Post your reply

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

Similar topics

4 posts views Thread by m.shidoshi | last post: by
6 posts views Thread by Chris Dunaway | last post: by
3 posts views Thread by Richard MSL | last post: by
reply views Thread by ben.agnoli | last post: by

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.