printDocument class...Form Feed

B

Bhavin Parekh

Hi Guys,
Can anyone help on this? Basically i want to send form feed to print two
string data on different page but some how its always printing in one
page...I used following code which is available on msdn site as example...
I tried /f, \f, with and without double slash as well but none of them is to
be working in c#.

private void pd_PrintPage(object sender, PrintPageEventArgs ev)

{

float linesPerPage = 0;

float yPos = 0;

int count = 0;

float leftMargin = ev.MarginBounds.Left;

float topMargin = ev.MarginBounds.Top;

leftMargin = 0;

topMargin = 0;

String line = null;

// Calculate the number of lines per page.

linesPerPage = ev.MarginBounds.Height /

printFont.GetHeight(ev.Graphics);

// Iterate over the file, printing each line.

line = "line11111111111111111111111111111";

yPos =20;

ev.Graphics.DrawString(line, printFont, Brushes.Black,leftMargin, yPos, new
StringFormat());

ev.HasMorePages = true;

yPos = 30;

string line2 = "line222222222222222222222222222";

ev.Graphics.DrawString(line2, printFont, Brushes.Black, leftMargin, yPos,
new StringFormat());

ev.HasMorePages = false;

}



below is button event code...

printFont = new Font("Arial", 8);

PrintDocument pd = new PrintDocument();

pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

pd.Print();



I dont want to use button event code to repeat or use in a loop if i will be
printing thousand pages it will slower the performance...

All i m looking whats the way to send form feed or page break in a data so
it prints remaining data into next page...

Any help would be appreciated...

Thnx
 
B

Bhavin Parekh

Hi Pete,
Thanx for a reply. Yes I understand that i will need to use HasMorePages
property but somehow i m still not clear how to use exactly . For ex, i
tried below code to print line and line2 on different page and i want to use
print Document object initialized only once and print command only for
performance issue if i am printing pages in thousands with diff data.

public void Printing()
{

printFont = new Font("Arial", 8);

PrintDocument pd = new PrintDocument();

pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

pd.Print();

}

private void pd_PrintPage(object sender, PrintPageEventArgs ev)

{

float linesPerPage = 0;

float yPos = 0;

int count = 0;

float leftMargin = ev.MarginBounds.Left;

float topMargin = ev.MarginBounds.Top;

leftMargin = 0;

topMargin = 0;

String line = null;

linesPerPage = ev.MarginBounds.Height / printFont.GetHeight(ev.Graphics);

line = "line11111111111111111111111111111";

yPos =20;

ev.Graphics.DrawString(line, printFont, Brushes.Black,leftMargin, yPos, new
StringFormat());

ev.HasMorePages = true;

yPos = 30;

string line2 = "line222222222222222222222222222";

ev.Graphics.DrawString(line2, printFont, Brushes.Black, leftMargin, yPos,
new StringFormat());

ev.HasMorePages = false;

}

I want print line and line2 on different page...but somehow its not working.
Pls correct me if i have mis understood u.
Thnx

Peter Duniho said:
[...]
All i m looking whats the way to send form feed or page break in a data
so
it prints remaining data into next page...

The way you finish the page is return from the PrintPage event handler.
If you set the HasMorePages property of the PrintPageEventArgs argument to
"true", then the PrintPage event will be raised again, and you'll get to
print the next page.

You have to do all the pagination yourself. All .NET knows is that it
hands you a Graphics instance representing a single page and that your
event handler, before it returns, indicates whether it's the last page or
not by setting the HasMorePages property appropriately. Anything else is
up to you.

Pete
 
B

Bhavin Parekh

yes, you are right and you help me to clear my doubts..but still finding too
hard to implement in the code as repeat calls can be made by having
hasmorepages property through only.
for e.g., if i have 10 string line variable, and i want to print on 10
pages, i will need to remember status for each line if its printed or not.
Also still cant figure out how do i pass the diff data to print page event
everytime. i guess i will need to check status update for each line in
printPage Event.
 
C

Christophe Lephay

Bhavin Parekh said:
yes, you are right and you help me to clear my doubts..but still finding
too hard to implement in the code as repeat calls can be made by having
hasmorepages property through only.
for e.g., if i have 10 string line variable, and i want to print on 10
pages, i will need to remember status for each line if its printed or not.
Also still cant figure out how do i pass the diff data to print page event
everytime. i guess i will need to check status update for each line in
printPage Event.

You have to remember status for each line or you can use the proper
container : i'd suggest a Queue that you would fill in the BeginPrint event.

Queue<string> lines = new Queue<string>();

private void pd_BeginPrint(object sender,
System.Drawing.Printing.PrintEventArgs e)
{
lines.Enqueue("line1");
lines.Enqueue("line2");
}

private void pd_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
if (lines.Count > 0)
{
string current_line = lines.Dequeue();
e.Graphics.DrawString(current_line, SomeStuffHere...);
}
e.HasMorePages = lines.Count > 0;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top