Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8191751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:59:12+00:00 2026-06-07T03:59:12+00:00

I am attempting to print something using a C# Winforms application. I can’t seem

  • 0

I am attempting to print something using a C# Winforms application. I can’t seem to understand how multiple pages works. Let’s say I have the following code in my constructor:

private string _stringToPrint;
_stringToPrint = "";
for (int i = 0; i < 120; i++)
{
    _stringToPrint = _stringToPrint + "Line " + i.ToString() + Environment.NewLine;
}

Then I have this code on my button click event:

private void MnuFilePrintClick(object sender, EventArgs e)
{
    var pd = new PrintDocument();
    pd.PrintPage += pd_PrintPage;

    var z = new PrintPreviewDialog { Document = pd };

    z.ShowDialog(this);
}

void pd_PrintPage(object sender, PrintPageEventArgs e)
{
    Graphics g = e.Graphics;
    var font = new Font("Arial", 10f, FontStyle.Regular);

    g.DrawString(_stringToPrint, font, Brushes.Black, new PointF(10f, 10f));
}

Right now, when I run this code, it is giving me one page and after like 70 lines, it just runs off the paper. How would I print this string so that it prints enough for one page, then runs over to the second page, etc..?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:59:13+00:00Added an answer on June 7, 2026 at 3:59 am

    You could have a counter and set the amount of lines you want per page like so:

    private string[] _stringToPrint = new string[100]; // 100 is the amount of lines
    private int counter = 0;
    private int amtleft = _stringToPrint.Length;
    private int amtperpage = 40; // The amount of lines per page
    for (int i = 0; i < 120; i++)
    {
        _stringToPrint[i] ="Line " + i.ToString();
    }
    

    Then in pd_PrintPage:

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        int currentamt = (amtleft > 40)?40:amtleft;
        Graphics g = e.Graphics;
        var font = new Font("Arial", 10f, FontStyle.Regular);
        for(int x = counter; x < (currentamt+counter); x++)
        {
            g.DrawString(_stringToPrint[x], font, Brushes.Black, new PointF(10f, (float)x*10));
            // x*10 is just so the lines are printed downwards and not on top of each other
            // For example Line 2 would be printed below Line 1 etc
        }
        counter+=currentamt;
        amtleft-=currentamt;
        if(amtleft<0)
            e.HasMorePages = true;
        else
            e.HasMorePages = false;
        // If e.HasMorePages is set to true and the 'PrintPage' has finished, it will print another page, else it wont
    }
    

    I have had bad experiences with e.HasMorePages so this may not work.

    Let me know if this works and I hope it helps!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to print a JPEG file that I reference using a Uri
I'm attempting to combine a few queries and can't seem to nail it down.
I'm attempting to print a bitmap from an Espon receipt printer. The full error
When I print the result of the regex I am attempting to use to
Attempting to set up an automated texting service for customers, where people can text
I am attempting to make an app in which you can load an image
Using django-filebrowser and the FileBrowserField I am attempting to assign the directory parameter based
I am using XNA to build a project where I can draw graffiti on
I am attempting a multiple producer/consumer problem in C, but its not working as
Hi so i am using msVS++2010 and have been attempting to set up SFML

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.