PrintPreview?

A

Adam Right

Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but after
preview was shown, when i press the "print" button, i cannot print the
document although there is some words on the preview. So, is there a bug in
the preview?

Thanks...
 
N

Nicholas Paldino [.NET/C# MVP]

Adam,

Without seeing your code, it's impossible to say. You might have a bug
in your code.

Can you post a complete example?
 
A

Adam Right

Hi,

Hi,

My code is on the below,

m_PrintDocument = new PrintDocument( );
m_PrintDocument.PrintPage += new PrintPageEventHandler(
m_PrintDocument_PrintPage );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( );
printPreviewDialog1.Document = m_PrintDocument;
Form m_Temp = ( Form ) printPreviewDialog1;
m_Temp.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog ( );

void m_PrintDocument_PrintPage( object sender, PrintPageEventArgs e )
{
Font printFont = new Font ( "Courier" , 10 , FontStyle.Regular );
String myString = "Trying";
e.Graphics.DrawString( myString, printFont, Brushes.Black, 0, 0, new
StringFormat( ) );
}

The printPreview dialog does not print my document although there are words
on the document.
Thanks..
Nicholas Paldino said:
Adam,

Without seeing your code, it's impossible to say. You might have a bug
in your code.

Can you post a complete example?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adam Right said:
Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but
after preview was shown, when i press the "print" button, i cannot print
the document although there is some words on the preview. So, is there a
bug in the preview?

Thanks...
 
A

Adam Right

Hi,

can you give your mail address. I want to send yo an attachment in which
there is a screen shot about this problem....

Adam Right said:
Hi,

Hi,

My code is on the below,

m_PrintDocument = new PrintDocument( );
m_PrintDocument.PrintPage += new PrintPageEventHandler(
m_PrintDocument_PrintPage );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( );
printPreviewDialog1.Document = m_PrintDocument;
Form m_Temp = ( Form ) printPreviewDialog1;
m_Temp.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog ( );

void m_PrintDocument_PrintPage( object sender, PrintPageEventArgs e )
{
Font printFont = new Font ( "Courier" , 10 , FontStyle.Regular );
String myString = "Trying";
e.Graphics.DrawString( myString, printFont, Brushes.Black, 0, 0, new
StringFormat( ) );
}

The printPreview dialog does not print my document although there are
words on the document.
Thanks..
Nicholas Paldino said:
Adam,

Without seeing your code, it's impossible to say. You might have a
bug in your code.

Can you post a complete example?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adam Right said:
Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but
after preview was shown, when i press the "print" button, i cannot print
the document although there is some words on the preview. So, is there a
bug in the preview?

Thanks...
 
N

Nicholas Paldino [.NET/C# MVP]

Adam,

You can post it here, and I will see it. No need to hide it from the
rest of the group.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adam Right said:
Hi,

can you give your mail address. I want to send yo an attachment in which
there is a screen shot about this problem....

Adam Right said:
Hi,

Hi,

My code is on the below,

m_PrintDocument = new PrintDocument( );
m_PrintDocument.PrintPage += new PrintPageEventHandler(
m_PrintDocument_PrintPage );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( );
printPreviewDialog1.Document = m_PrintDocument;
Form m_Temp = ( Form ) printPreviewDialog1;
m_Temp.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog ( );

void m_PrintDocument_PrintPage( object sender, PrintPageEventArgs e )
{
Font printFont = new Font ( "Courier" , 10 , FontStyle.Regular );
String myString = "Trying";
e.Graphics.DrawString( myString, printFont, Brushes.Black, 0, 0, new
StringFormat( ) );
}

The printPreview dialog does not print my document although there are
words on the document.
Thanks..
Nicholas Paldino said:
Adam,

Without seeing your code, it's impossible to say. You might have a
bug in your code.

Can you post a complete example?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but
after preview was shown, when i press the "print" button, i cannot
print the document although there is some words on the preview. So, is
there a bug in the preview?

Thanks...
 
B

Bruce Wood

Nicholas,

I prepared a response for Adam, telling him that he was using
PrintDocument incorrectly. Then I read the "about PrintDocument"
documentation for the .NET Framework 1.1, and found that he was
following their instructions exactly. This let to a question:

Do you know whether PrintPreviewDialog, when you choose "Print..." from
the menu, re-uses the original instance of the PrintDocument you pass
it, or instantiates a new PrintDocument for the printing process
itself?

I ask because I've always inherited from PrintDocument and overridden
OnPrintPage, rather than handling the PrintPage event of a "vanilla"
PrintDocument. The former approach has always worked for me. However,
if PrintPreviewDialog instantiates a _new_ PrintDocument instance
internally in order to print, the latter method (the method recommended
by MS in its documentation) _won't_ work.
 
A

Adam Right

Hi,

Is there a basic solution for this problem, i cannot understand complex
english sorry...
 
B

Bruce Wood

I have a potential solution, but it will have to wait until tomorrow,
when I will be back in the office and will have a printer with which to
test.

I want to write a little test program to discover whether my theory is
correct.
 
R

Ron Allen

Adam,
Since you are putting the text at 0,0 on the page you may be outside the
'hard margin' of the printer and thus not printing anything. Try it again
with 100, 100 which should be 1" down and 1" right from the page edge using
the standard coordinates. Alternately you could try setting OriginAtMargins
to true for the PrintDocument (I think).

Ron Allen
Adam Right said:
Hi,

Hi,

My code is on the below,

m_PrintDocument = new PrintDocument( );
m_PrintDocument.PrintPage += new PrintPageEventHandler(
m_PrintDocument_PrintPage );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( );
printPreviewDialog1.Document = m_PrintDocument;
Form m_Temp = ( Form ) printPreviewDialog1;
m_Temp.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog ( );

void m_PrintDocument_PrintPage( object sender, PrintPageEventArgs e )
{
Font printFont = new Font ( "Courier" , 10 , FontStyle.Regular );
String myString = "Trying";
e.Graphics.DrawString( myString, printFont, Brushes.Black, 0, 0, new
StringFormat( ) );
}

The printPreview dialog does not print my document although there are
words on the document.
Thanks..
Nicholas Paldino said:
Adam,

Without seeing your code, it's impossible to say. You might have a
bug in your code.

Can you post a complete example?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Adam Right said:
Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but
after preview was shown, when i press the "print" button, i cannot print
the document although there is some words on the preview. So, is there a
bug in the preview?

Thanks...
 
A

Adam Right

Hi Ron,

I have tried that but the printed document still is empty. I do not
understand that if there are some words on the preview, what is the reason
that it does not print that words? in Beta 2, i can print with the same
code. So i am wondering about Framework 2.0 in which there may be a bug in
print document. I do not know how can i handle that problem, because it is
very important for me and my customers. Thanks...

Ron Allen said:
Adam,
Since you are putting the text at 0,0 on the page you may be outside
the 'hard margin' of the printer and thus not printing anything. Try it
again with 100, 100 which should be 1" down and 1" right from the page
edge using the standard coordinates. Alternately you could try setting
OriginAtMargins to true for the PrintDocument (I think).

Ron Allen
Adam Right said:
Hi,

Hi,

My code is on the below,

m_PrintDocument = new PrintDocument( );
m_PrintDocument.PrintPage += new PrintPageEventHandler(
m_PrintDocument_PrintPage );
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog ( );
printPreviewDialog1.Document = m_PrintDocument;
Form m_Temp = ( Form ) printPreviewDialog1;
m_Temp.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog ( );

void m_PrintDocument_PrintPage( object sender, PrintPageEventArgs e )
{
Font printFont = new Font ( "Courier" , 10 , FontStyle.Regular );
String myString = "Trying";
e.Graphics.DrawString( myString, printFont, Brushes.Black, 0, 0, new
StringFormat( ) );
}

The printPreview dialog does not print my document although there are
words on the document.
Thanks..
Nicholas Paldino said:
Adam,

Without seeing your code, it's impossible to say. You might have a
bug in your code.

Can you post a complete example?

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,

I am using .Net Framework 2.0 final, and i use .Net printing library to
print a document. I use printPreview dialog to print the document but
after preview was shown, when i press the "print" button, i cannot
print the document although there is some words on the preview. So, is
there a bug in the preview?

Thanks...
 
B

Bruce Wood

I ran my little test using .NET 1.1, and I was wrong. Ron Allen's
solution is most likely the correct one.

In my case, your code, as written, works fine in print preview and when
printing from print preview. No problems at all in .NET 1.1.

I'm not using .NET 2.0, so I'll leave it to others to try to reproduce
this behaviour in 2.0.
 
R

Ron Allen

OK, I had to try this here. The following works for me here using VS2005
both in preview mode and in printer output (although I haven't checked for
any hard margin offsets). Note that I've used a stock StringFormat object
and disposed of the Font created. I'm using a Lexmark Optra printer but I
don't think that the printer should make a difference. All my variable are
local as well.

Ron Allen
----------------------------snippet---------------------------------------------------------
private void button1_Click(object sender, EventArgs e)
{
PrintDocument m_PrintDocument = new PrintDocument();
m_PrintDocument.PrintPage += new
PrintPageEventHandler(m_PrintDocument_PrintPage);
PrintPreviewDialog printPreviewDialog1 = new PrintPreviewDialog();
printPreviewDialog1.Document = m_PrintDocument;
printPreviewDialog1.WindowState = FormWindowState.Maximized;
printPreviewDialog1.ShowDialog();
printPreviewDialog1.Dispose();
}

void m_PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Font printFont = new Font("Courier", 10, FontStyle.Regular);
String myString = "Trying";
e.Graphics.DrawString(myString, printFont, Brushes.Black, 100, 100,
StringFormat.GenericDefault);
printFont.Dispose();
}

----------------------------snippet---------------------------------------------------------
 
Joined
Jul 27, 2006
Messages
1
Reaction score
0
I donot know if you still need it

Hi,

I have the same problem when I use the PrintPreviewDialog to print. I would thank Bruce Wood for the suggestion to write a new class to implement PrintDocument.

The class is as follow:
public class TextDocument : System.Drawing.Printing.PrintDocument

{

private string text;

StringReader myReader; //for print preview

Font font;

public TextDocument(string text, Font font)

{

this.text = text;

this.font = font;

this.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(document_PrintPage);

myReader =
new StringReader(text);

}

public void refresh()

{

//reset the stream; otherwise, print empty page.

myReader = new StringReader(text);

}

private void document_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs ev)

{

System.Drawing.Printing.
PrintDocument document = (System.Drawing.Printing.PrintDocument)sender;

float linesPerPage = 0;

float yPosition = 0;

int count = 0;

float leftMargin = ev.MarginBounds.Left;

float topMargin = ev.MarginBounds.Top;

string line = null;

Font printFont = this.font;

SolidBrush myBrush = new SolidBrush(Color.Black);

// Work out the number of lines per page, using the MarginBounds.

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

// Iterate over the string using the StringReader, printing each line.

while (count < linesPerPage && ((line = myReader.ReadLine()) != null))

{

// calculate the next line position based on

// the height of the font according to the printing device

yPosition = topMargin + (count * printFont.GetHeight(ev.Graphics));

// draw the next line in the rich edit control

ev.Graphics.DrawString(line, printFont, myBrush, leftMargin, yPosition, new StringFormat());

count++;

}

// If there are more lines, print another page.

if (line != null)

ev.HasMorePages =
true;

else

ev.HasMorePages = false;

myBrush.Dispose();

}

}


Then, the following code is used to printpreview a string from a textbox:

try

{

string strText = this.txtReport.Text;

doc =
new TextDocument(txtReport.Text, txtReport.Font);

printPreviewDialog1.Document = doc;

DialogResult rs = printPreviewDialog1.ShowDialog();

}

catch (Exception exp)

{

System.
Console.WriteLine(exp.Message.ToString());

}

When clicking the print button in the preview window, it still prints empty page. The reason is that myReader runs to the end of the document when preview the page. So we need to reset it after the printPreviewDialog1 activated:

void printPreviewDialog1_Activated(object sender, EventArgs e)

{

doc.refresh();

}

Way to go!
 

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