PrintPreviewDialog Print Icon not printing

R

Randy

Hello all,
I'm trying to print using PrintPreviewDialog. What's happening is that the
PrintPreviewDialog shows the correct information to be printed, but when I
click the print icon, it prints a blank page. If I comment out the
printPreviewDialog1.ShowDialog();
and just do a
printDocument1.Print();
instead, it prints all the pages like a champ. Anyone know what I'm doing
wrong?
Thanks
 
R

Ron Allen

Randy,
As I said in my earlier reply you aren't resetting the start of the
print data stream (or wherever you get your print data from). You should
override the OnBeginPrint method of the PrintDocument and set your stream's
staring position back to the beginning there. If you have read through the
stream once already and don't reset the position when you press on the print
button on the PrintPreviewDialog you will already be at the end and thus
have nothing available to print.

Ron Allen
 
J

Justin Weinberg

Try this print document class. Made it with my new tool.

I did exactly what you said - showed a print dialogue and hit the print
button and it's ok. If you're a VB developer, let me know and I'll reexport
it in VB.

//Code starts here



// GDIPlus Architect PrintDocument Class Output
// www.mrgsoft.com
public class Untitled1 : System.Drawing.Printing.PrintDocument {

// Current page
private int currentPage = 1;

// Total pages
private int totalPages = 2;

private System.Drawing.SolidBrush brush1 = new
System.Drawing.SolidBrush(System.Drawing.Color.Black);

private System.Drawing.Font font1 = new System.Drawing.Font("Arial",
42.48F, ((System.Drawing.FontStyle)(0)), System.Drawing.GraphicsUnit.Point,
1);

private System.Drawing.StringFormat stringformat1 = new
System.Drawing.StringFormat(System.Drawing.StringFormat.GenericTypographic);

protected string Text3 = "Hello world";

private System.Drawing.RectangleF Text3Rect = new
System.Drawing.RectangleF(136, 128, 426, 88);

protected string Text5 = "page 1";

private System.Single Text5PointX = 130;

private System.Single Text5PointY = 222;

protected string Text6 = "page 2";

private System.Single Text6PointX = 130;

private System.Single Text6PointY = 222;

public Untitled1() {
this.InitializeGraphics();
}

private void InitializeGraphics() {
this.stringformat1.Alignment = System.Drawing.StringAlignment.Near;
}

protected override void
OnPrintPage(System.Drawing.Printing.PrintPageEventArgs e) {
System.Drawing.Graphics g = e.Graphics;
this.printGraphics(g);
if ((this.currentPage < this.totalPages)) {
e.HasMorePages = true;
this.currentPage = (this.currentPage + 1);
}
else {
e.HasMorePages = false;
}
}

// Required to dispose of created resources
private void DisposeGraphics() {
this.brush1.Dispose();
this.font1.Dispose();
this.stringformat1.Dispose();
}

protected override void Dispose(bool disposing) {
if (disposing) {
this.DisposeGraphics();
}
}

private void printGraphics(System.Drawing.Graphics g) {
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint =
System.Drawing.Text.TextRenderingHint.AntiAlias;
if ((this.currentPage == 1)) {
g.DrawString(this.Text3, this.font1, this.brush1,
this.Text3Rect, this.stringformat1);
g.DrawString(this.Text5, this.font1, this.brush1,
this.Text5PointX, this.Text5PointY);
}
if ((this.currentPage == 2)) {
g.DrawString(this.Text6, this.font1, this.brush1,
this.Text6PointX, this.Text6PointY);
}
}
}
 

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