Why doesn't PrintPreview match print?

Well, before spamming with StringFormat.Alignment = StringAlignment.Center ... hear my whole problem:

When I draw text with the following code, the line is centered in PrintPreview, but NOT CENTERED on the actual paper when printing. The whole page is a little to the right, so some things are displayed as a printout on the print preview, but paper drops out when printing (not only outside the range of the field, but also turns off the paper).

    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 on why this is? This should be trivial, but it is not.

EDIT: I found that this is not just text ... It just prints ALL. I updated the code above to provide a better example of the problem. Just leave it in the form with the button on it.

2: , , , . , . , PrintPreview , IsPreview.

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

>

+3
2

, , PageSettings.HardMarginX. , . , , , , . , , . - .

- -. "", . e.Graphics.TranslateTransform.

+6

, , , . , , .. , winforms, , .

0

Source: https://habr.com/ru/post/1756650/


All Articles