Hey!

I'm doning my thesis work right now and I'm programming in C# (for the first time). I want to print multiple pages (2 or 3) but I don't know how. I've searched and found some info but I think my problem is a bit different. I'm not printing from a textbox, txt-file or datebase. I'm "designing" the printed page by hand (or code) since I wanna print some pictures, lines, rectangles and different variables on specific locations. I've managed to design the first page but I wanna do another one that looks similar but with different images and variables.
Does anyone have a clue of how I should do it??

Here's the code for the "handmade" first page:

void printDoc_PrintPage(object sender, PrintPageEventArgs e)
        {
            string customerInfo1 = customerBox1.Text.ToString();
            string customerInfo2 = customerBox2.Text.ToString();
            string customerInfo3 = customerBox3.Text.ToString();
            string customerInfo4 = customerBox4.Text.ToString();
            string customerInfo5 = customerBox5.Text.ToString();
            string customerInfo6 = customerBox6.Text.ToString();

            Image photo = springDraw.springBitmap();

            Pen myPen = new Pen(Brushes.Black, 1);

            //  Lesjöfors logotype
            e.Graphics.DrawImage(Image.FromFile("C:\\Andreas\\GSCP3\\GSCP3\\logotype_large.gif"), 170, 20);

            //  Picture of the lid
            e.Graphics.DrawImage(photo, 60, 153, 670, 670);

            //  Drawing of the rectangle
            e.Graphics.DrawRectangle(myPen, 45, 150, 700, 880);

            // Horizontal Line Under Lid Picture
            e.Graphics.DrawLine(myPen, 45, 823, 745, 823);

            //  Spring Info
            e.Graphics.DrawString("PROPOSAL", verdana10Font, Brushes.Black, 75, 825);

            //  Line under PROPOSAL
            e.Graphics.DrawLine(myPen, 77, 840, 155, 840);

            e.Graphics.DrawString("Gas Spring Dimension", verdana10Font, Brushes.Black, 75, 845);
                e.Graphics.DrawString(chosenSpring, verdana10Font, Brushes.Black, 280, 845);

            e.Graphics.DrawString("Catalogue Number", verdana10Font, Brushes.Black, 75, 865);
                e.Graphics.DrawString("xxx", verdana10Font, Brushes.Black, 280, 865);

            e.Graphics.DrawString("End Fitting, Tube", verdana10Font, Brushes.Black, 75, 885);
                e.Graphics.DrawString("xxx", verdana10Font, Brushes.Black, 280, 885);

            e.Graphics.DrawString("End Fitting, Rod", verdana10Font, Brushes.Black, 75, 905);
                e.Graphics.DrawString("xxx", verdana10Font, Brushes.Black, 280, 905);

            e.Graphics.DrawString("Mounting Point, Fixed [mm]", verdana10Font, Brushes.Black, 75, 925);
                e.Graphics.DrawString("X= " + springFixX.ToString() + ", Y= " + springFixY.ToString(), verdana10Font, Brushes.Black, 280, 925);

            e.Graphics.DrawString("Mounting Point, Lid [mm]", verdana10Font, Brushes.Black, 75, 945);
            e.Graphics.DrawString("X= " + springLidClosedX.ToString() + ", Y= " + springLidClosedY.ToString(), verdana10Font, Brushes.Black, 280, 945);

            e.Graphics.DrawString("Length Of Lid [mm]", verdana10Font, Brushes.Black, 75, 965);
                e.Graphics.DrawString(lidLengthInt.ToString(), verdana10Font, Brushes.Black, 280, 965);

            e.Graphics.DrawString("Weight Of Lid [kg]", verdana10Font, Brushes.Black, 75, 985);
                e.Graphics.DrawString(weight.ToString(), verdana10Font, Brushes.Black, 280, 985);

            e.Graphics.DrawString("Needed Forcein Pos. 2 [N]", verdana10Font, Brushes.Black, 75, 1005);
                e.Graphics.DrawString(maxForceInt.ToString(), verdana10Font, Brushes.Black, 280, 1005);

            // Vertical Line Left Of Customer Info
            e.Graphics.DrawLine(myPen, 465, 823, 465, 1030);

            //  Customer Info
            e.Graphics.DrawString("CUSTOMER INFO", verdana10Font, Brushes.Black, 470, 825);

            //  Line under PROPOSAL
            e.Graphics.DrawLine(myPen, 473, 840, 595, 840);

            e.Graphics.DrawString("Company", verdana10Font, Brushes.Black, 470, 845);
            e.Graphics.DrawString("Name", verdana10Font, Brushes.Black, 470, 865);
            e.Graphics.DrawString("Project", verdana10Font, Brushes.Black, 470, 885);
            e.Graphics.DrawString("Fax", verdana10Font, Brushes.Black, 470, 905);

            //  Designer Info
            e.Graphics.DrawString("Designed By", verdana10Font, Brushes.Black, 470, 965);
            e.Graphics.DrawString("Tel. Nr.", verdana10Font, Brushes.Black, 470, 985);
            e.Graphics.DrawString("Mail", verdana10Font, Brushes.Black, 470, 1005);

            //  Line
            e.Graphics.DrawLine(myPen, 45, 1035, 745, 1035);

            //  Lesjöfors Address
            e.Graphics.DrawString("Postal Address", verdana9Font, Brushes.Black, 45, 1040);
                e.Graphics.DrawString("Telephone", verdana9Font, Brushes.Black, 330, 1040);
                    e.Graphics.DrawString("Telefax", verdana9Font, Brushes.Black, 530, 1040);

            e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 45, 1055);

            e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 45, 1070);
                e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 330, 1070);
                    e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 530, 1070);

            e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 45, 1085);
                e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 330, 1085);
                    e.Graphics.DrawString("xxx", verdana9Font, Brushes.Black, 530, 1085);
        }

Thanks in advance!

Andreas

Edited 3 Years Ago by mike_2000_17: Fixed formatting

this is a tricky one to get your head around. But basically you call the print page method at the end of the printpage method. And use a variable or counter somewhere to make sure you don't get stuck in an infinite loop.

in pseudo code... I'm typing this from memory but this is sort of the right lines. I'll try and get back here in a bit with a more detailed solution.

int x = 0;

printpage(){
string[] stufftoprint = {hello, hello 1, hello2};
while( x<stufftoprint.count){

do graphics stuff with stufftoprint[x]
x++;
call printpage;
}

}

TA DA! I'm back.

Ok so I wasn't 100% right but i was close

the printing method automatically keeps calling itself as long as e.hasmorepages = true.

So set e.hasmorepages to true somewhere in the loop. (the loop in this case being the actual method)

You have to keep the page number somewhere outside the method. Then each time round you go "I'm on page...2" so what should i print if i'm on page 2. etc.

Hope i' not too skitish i've been up for about 27 hours now. This is actually my break.

private void printDocument1_PrintPage1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        if (checkBox4.Checked)
        {
            Graphics g = e.Graphics;
            String message = System.Environment.UserName;
            Font messageFont = new Font("Arial",
                     24, System.Drawing.GraphicsUnit.Point);
            Font instructionFont = new Font("Arial",
                     11, System.Drawing.GraphicsUnit.Point);
            Font choiceFont = new Font("Arial",
                     8, System.Drawing.GraphicsUnit.Point);
            Code39 mycode39 = null;

            float x = 0;

            float y = 80;

            int pagecount = 0;


            g.DrawString("Order Number: " + label1.Text, messageFont, Brushes.Black, 20, 20);
            mycode39 = new Code39("*" + label1.Text + "*");
            //g.DrawImage(mycode39.Paint(), 540, 20, 200, 60);

            g.DrawString(textBox6.Text, instructionFont, Brushes.Black, 25, 52);

            g.DrawRectangle(new Pen(Color.Black), 38, 78, 702, 806);

            //there is some very very complicated code here to print out 6 images and some imformation about each in a specific way.  But the point is att the end of the printing bit.  

            if (imagecount < panel1.Controls.Count)
            {
                e.HasMorePages = true;  <- this get set to false each time round.  so set it back to true to print another page
            }
            else
            {
                finaliseorder();
            }
        }
        else
        {
            finaliseorder();
        }
    }

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Okey, thank you for your quick replies! I'm a noob at programming so I have to look inte this a bit to fully understand how to use it.

Thanks again!

I didn't really understand how to to it like you suggested (I'm a noob). Isn't it possible to do something like this, like just creating another one of these:

void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
    //  Design code
}

(I've renamed them to printDoc1 & 2 in the code below) and just add it to:

private void print_Click(object sender, EventArgs e)
{
    printDoc.PrintPage += new PrintPageEventHandler(printDoc1);
    printDoc.PrintPage += new PrintPageEventHandler(printDoc2);
    printDoc.Print();
}

PrintDocument printDoc = new PrintDocument();

private void printPreview_Click(object sender, EventArgs e)
{
    printDoc.PrintPage += new PrintPageEventHandler(printDoc1);
    printDoc.PrintPage += new PrintPageEventHandler(printDoc2);
    PrintPreviewDialog dlg = new PrintPreviewDialog();
    dlg.Document = printDoc;
    dlg.ShowDialog();
}

I know that this code doesn't work since I've tried, but is it possible to do it in a similar way???

//Andreas

This article has been dead for over six months. Start a new discussion instead.