C# と VB.NET の質問掲示板

わんくま同盟

ASP.NET、C++/CLI、Java 何でもどうぞ

C# と VB.NET の入門サイト


(過去ログ 111 を表示中)
■65585 / )  Re[3]: 印刷の余白について
□投稿者/ 魔界の仮面弁士 (163回)-(2013/03/09(Sat) 19:29:58)
No65577 (ひろ さん) に返信
> private void pageSetupDialog1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
> {
>    int ypos = e.MarginBounds.Top;
> 上記の e.MarginBounds.Top には、どこで設定した値が入るのでしょうか。

そもそも上記は実行されません。
PageSetupDialog には PrintPage イベントが無いからです。


> 余白設定とe.MarginBounds は違うのですか?

PrintDocument.DefaultPageSettings は、既定のページ設定を表します。
ページごとに異なる設定をしたいような場合には、
QueryPageSettings イベントで e.PageSettings を調整すれば、
PrintPage イベントの引数に反映されます。

UI 指定が必要な場合には PageSetupDialog を使うことも
できますが、今回はプログラムから値を指定したいのですよね?



■No65583 (ひろ さん) に返信
> では、余白を小さくして印刷領域を広くするには
> 同設定したらいいのでしょうか。

PageSettings クラスの Marings プロパティです。



private void button1_Click(object sender, EventArgs e)
{
    // ここの値を変えてみよう!
    printDocument1.DefaultPageSettings.Margins
        //= new Margins(100, 100, 100, 100);
         = new Margins(10, 10, 10, 10);

    // この値も変えてみよう!
    printDocument1.OriginAtMargins = true;
    
    printDocument1.Print();
}

private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
    using (Brush b = new SolidBrush(Color.FromArgb(128, Color.Red)))
    using (Pen p = new Pen(b, 80))
    {
        e.Graphics.DrawRectangle(p, e.PageBounds);
    }

    using (Brush b = new SolidBrush(Color.FromArgb(128, Color.Blue)))
    using (Pen p = new Pen(b, 80))
    {
        e.Graphics.DrawRectangle(p, e.MarginBounds);
    }

    using (Font f = new Font("MS Gothic", 12))
    {
        var draw = new Action<Point, Brush, string>((p, b, s) =>
        {
            e.Graphics.DrawString(s, f, b, p);
        });
        var pos = e.MarginBounds.Location;
        draw(pos, Brushes.Black,
            String.Format("PrintDocument.OriginAtMargins={0}", 
            this.printDocument1.OriginAtMargins));
        pos.Offset(0, 50);
        draw(pos, Brushes.DarkGreen,
            String.Format("PageSettings.HadMarginXY=({0}, {1})", 
            e.PageSettings.HardMarginX,
            e.PageSettings.HardMarginY));
        pos.Offset(0, 50);
        draw(pos, Brushes.DarkGreen,
            String.Format("PageSettings.Margins={0}",
            e.PageSettings.Margins.ToString()));
        pos.Offset(0, 50);
        draw(pos, Brushes.Red,
            String.Format("PageBounds={0}",
            e.PageBounds.ToString()));
        pos.Offset(0, 50);
        draw(pos, Brushes.Blue,
            String.Format("MarginBounds={0}",
            e.MarginBounds.ToString()));
    }
}


大抵のページプリンタには、印刷不可能領域が存在するため、上記では
DrawRectangle の際に、極太(幅=80)の Pen を利用しています。
(PDF プリンタや XPS プリンタだと、ハード余白が0だったりしますが)

極太線であるがため、PageBounds の線と MarginBounds の線とが
重なることがあるため、それぞれ、半透明赤と半透明青の線で
描画しています。(重なったところは紫になる予定)

なので、試すときにはカラープリンターの方が分かりやすいかも。

返信 編集キー/


管理者用

- Child Tree -