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

Time:10-25

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.

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.

CodePudding user response:

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;
}
  • Related