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

  • Home
  • SEARCH
  • 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 962919
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:35:15+00:00 2026-05-16T01:35:15+00:00

Okay, before you spam me with StringFormat.Alignment = StringAlignment.Center … hear my whole issue:

  • 0

Okay, before you spam me with StringFormat.Alignment = StringAlignment.Center … hear my whole issue:

When I draw text with the following code, the string is centered in the PrintPreview, but NOT CENTERED on the actual paper when it prints. The whole page is off to the right just a little, thus some stuff shows as printing on the print preview, but falls off the paper (not just outside the margin range, but OFF the paper) when printed.

    private void button1_Click(object sender, EventArgs e)
    {
        PrintDocument pd = new PrintDocument();

        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);

        PrintPreviewDialog ppd = new PrintPreviewDialog();

        ((Form)ppd).WindowState = FormWindowState.Maximized;

        ppd.Document = pd;
        ppd.ShowDialog();
    }

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        for (int y = 100; y < 400; y += 25)
        {
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;

            e.Graphics.DrawRectangle(Pens.Black, new Rectangle(5, y, 840, 25));
        }

        e.HasMorePages = false;
    }

Any thoughts as to why it’s off? This should be trivial, but it isn’t.

EDIT: I’ve found that it’s not just text… It’s printing EVERYTHING off just a little. I’ve updated the code above to provide a better example of the issue. Just drop this in a form with a button on it.

EDIT 2: With the answer given, I’ve modified the code and this now works. I’m providing the final code for those that may want to see it. I have to recognize whether i’m seeing this in the PrintPreview dialog or on paper, so I have a IsPreview flag to handle this.

public partial class Form1 : Form
{
    bool IsPreview = true;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        IsPreview = true;
        PrintDocument pd = new PrintDocument();

        pd.EndPrint += new PrintEventHandler(pd_EndPrint);
        pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);


        PrintPreviewDialog ppd = new PrintPreviewDialog();

        ((Form)ppd).WindowState = FormWindowState.Maximized;

        ppd.Document = pd;
        ppd.ShowDialog();
    }

    void pd_EndPrint(object sender, PrintEventArgs e)
    {
        IsPreview = false;
    }

    void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
        Rectangle b3 = e.PageBounds;

        if (IsPreview)
        {
            e.Graphics.TranslateTransform(e.PageSettings.HardMarginX, e.PageSettings.HardMarginY);
        }

        b3.Width -= (int)e.PageSettings.HardMarginX * 2;
        b3.Height -= (int)e.PageSettings.HardMarginY * 3;

        int y = b3.Y;
        int x=0;
        while ((y + 25) < b3.Bottom)
        {
            x++;
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Center;

            Rectangle R = new Rectangle(b3.X, y, b3.Width, 25);

            e.Graphics.DrawRectangle(Pens.Black, R);
            e.Graphics.DrawString(x.ToString(), this.Font, Brushes.Black, b3.X + 5, y + 5);

            y += 25;
        }
        // draw the last little bit
        e.Graphics.DrawRectangle(Pens.Black, new Rectangle(b3.X, y, b3.Width, b3.Height - y));

        e.HasMorePages = false;
    }
}

  • 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-05-16T01:35:16+00:00Added an answer on May 16, 2026 at 1:35 am

    Being off towards the right tends to be explainable by the value of the PageSettings.HardMarginX property. A value produced by the printer driver. Printer drivers are however typically not very good at guessing what the actual paper route through the printer might look like. That’s mechanical, pinch rollers, tray alignment and whatnot. Software and Mechanical engineers don’t have lunch together often enough.

    But a software engineer can almost always fix a mechanical engineer’s problem. You’ll need an Options dialog to allow the user to fix the mechanical engineer’s problem. Use the value in e.Graphics.TranslateTransform call.

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

Sidebar

Related Questions

The following is okay: try { Console.WriteLine("Before"); yield return 1; Console.WriteLine("After"); } finally {
Okay, before you guys go nuts -- this is just a small site, temporary
Should you always close a DataReader before closing the connection, or is it okay
Okay, I have the following create action #posts_controller, nested resource under discussions def create
Okay, a before; Array ( 'home' => array('order' => 1), 'about' => array(), 'folio'
Okay, i posted a question before to fix my live validation contact form. Now
I'm trying to get my head round this (okay, tbh cramming a night before
Okay I was wondering when should I sanitize my code, when I add store
Okay i've seen this done somewhere before where you have a function that takes
Okay, a short breif of my app before i explain the problem. My app

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.