Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can I create a second page with it own content with printDocument C#

I am a PHP developer doing a C# project.
I am busy with a C# winform project.

On printing a document I need to add a page with different content than the first page.

To be clear.I need two pages, each page with its own content.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Currently it is printing 2 pages as expected but with the exact same content on both pages,
Here is an example of what I have currently.

    int currentpage = 0;
    int numofpages = 2;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
        
        float pageHeight = e.MarginBounds.Height;

        Bitmap bmp = Properties.Resources.someImage;
        Image newImage = bmp;
        e.Graphics.DrawImage(newImage, 20, 20);
        e.Graphics.DrawString("More content", new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 600, 350);

 currentpage++;

        if (currentpage < numofpages)
        {
       
            e.HasMorePages = true;
            
        Bitmap bmp = Properties.Resources.someOtherImage;
        Image newImage = bmp;
        e.Graphics.DrawImage(newImage, 20, 20);
        e.Graphics.DrawString("Other content", new Font("Verdana", 10, FontStyle.Bold), Brushes.Black, 600, 350);
        }

        else
        {
            e.HasMorePages = false;
            
        }
}

Is there a way to create a second page with its own content?

My only current option is to create a second function printDocument2_PrintPage_1 but it is not user-friendly for the end user.

>Solution :

As I said in a comment, it looks like you’re trying to render both page’s content during a single callback to the event handler. You should instead do something like:

int currentpage = 0;
int numofpages = 2;
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    currentpage++;

    if(currentPage==1)
    {
        Bitmap bmp = Properties.Resources.someImage;
        Image newImage = bmp;
        e.Graphics.DrawImage(newImage, 20, 20);
        e.Graphics.DrawString("More content", new Font("Verdana", 10, 
           FontStyle.Bold), Brushes.Black, 600, 350);
    }
    else if(currentPage == 2)
    {
        Bitmap bmp = Properties.Resources.someOtherImage;
        Image newImage = bmp;
        e.Graphics.DrawImage(newImage, 20, 20);
        e.Graphics.DrawString("Other content", new Font("Verdana", 10, 
             FontStyle.Bold), Brushes.Black, 600, 350);
    }
    e.HasMorePages = currentPage < numofpages;
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading