目次

印刷時に座標をミリ単位(mm)で指定する - C#

印刷時に描画のX,Y座標をmmで指定するコードを紹介します。

UI

下図のUIを作成します。フォームにButtonを1つ配置します。

コード

以下のコードを記述します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;


namespace PrintGraphicsUnit
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      PrintDocument pd = new PrintDocument();
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      pd.Print();
    }

    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
      int pwidth = e.PageSettings.PaperSize.Width;
      int pheight = e.PageSettings.PaperSize.Height;

      double pwm = (double)pwidth * 0.254f;
      double phm = (double)pheight * 0.254f;


      e.Graphics.PageUnit = GraphicsUnit.Millimeter;
      Pen p = new Pen(Color.Blue);
      p.Width = 1;
      e.Graphics.DrawRectangle(p, new Rectangle(10,10,(int)pwm-20,(int)phm-20));
    }
  }
}

解説

印刷の詳細についてはこちらの記事を参照して下さい。

double pwm = (double)pwidth * 0.254f;
double phm = (double)pheight * 0.254f;
用紙の幅と高さを取得します。取得した値に0.254をかけることで、1/100インチ単位をミリ単位に直します。

e.Graphics.PageUnit = GraphicsUnit.Millimeter;
にて、PageUnitプロパティを変更することにより、座標の単位を変更できます。今回は"GraphicsUnit.Millimeter"を指定し、単位をミリメートル(mm)に変更します。

e.Graphics.DrawRectangle(p, new Rectangle(10,10,(int)pwm-20,(int)phm-20));
矩形を印刷します。用紙の10mm内側に矩形を印刷します。

実行結果

プロジェクトを実行します。下図のウィンドウが表示されます。


button1をクリックすると印刷が始まります。Adobe PDFで印刷した結果が下図になります。用紙の内側10mmに矩形が印刷できています。

補足、注意事項

単位をミリメートルに変更した場合、座標値を整数で指定するメソッドの場合、ミリメートル単位より小さな単位で印刷位置の変更がができません。ペン幅(Pen.Width)もミリ単位になり、1mmの太さの線で描画されます。細かく印刷位置を変更する場合はデフォルトの1/100インチの単位とし、ミリを1/100インチ単位に変換すると細かい位置の調整ができます。

コード例

1/100インチの単位でmmで描画位置を指定する場合は、以下のコードを用います。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace PrintGraphicsUnit
{
  public partial class FormMain : Form
  {
    public FormMain()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      PrintDocument pd = new PrintDocument();
      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage);
      pd.Print();
    }

    private void pd_PrintPage(object sender, PrintPageEventArgs e)
    {
      int pwidth = e.PageSettings.PaperSize.Width;
      int pheight = e.PageSettings.PaperSize.Height;

      Pen p = new Pen(Color.Blue);
      p.Width = 1;
      double xm = 5 * 3.9370f;
      double ym = 10 * 3.9370f;
      Rectangle r = new Rectangle(
        (int)xm, (int)ym, (int)(pwidth - xm * 2), (int)(pheight - ym * 2));
      e.Graphics.DrawRectangle(p, r); 
    }
  }
}

解説

ミリに3.970をかけることで、ミリメートルを1/100インチの値に変換できます。

実行結果

用紙の左右5mm内側、上下10mm内側に矩形を印刷できています。また、PageUnit を GraphicsUnit.Millimeterに設定した場合よりも、ペンの幅が細いことがわかります。



著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
掲載日: 2012-05-30
iPentec all rights reserverd.