본문 바로가기

Language/C#

[C#] PrintDocument 이용한 다중 페이지 출력

출처: http://tennesseewaltz.tistory.com/11

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Drawing.Printing;

namespace _CS__프린트2
{
public partial class Form1 : Form
{
int PageNo = 0;
int pageStartRow = 0;
int endRow = 50;

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

PageNo = 0;
pageStartRow = 0;
PrintDocument doc = new PrintDocument();
PrintPreviewDialog dlg = new PrintPreviewDialog();
dlg.Document = doc;
doc.PrintPage += new PrintPageEventHandler(doc_PrintPage);
dlg.ShowDialog();

}


void doc_PrintPage(object sender, PrintPageEventArgs e)
{
int nBotton = e.PageBounds.Bottom - 30;
Graphics g = e.Graphics;
Font PF = new Font("굴림", 11);
int line = 0;
for (int nRow = pageStartRow; nRow < endRow; nRow++)
{
line++;
int y = line * 30;
g.DrawString(nRow.ToString(), PF, Brushes.Black, 100, y);
if (y > (nBotton - 50))
{
pageStartRow = nRow + 1;
e.HasMorePages = true;
DrawFooter(e.Graphics, nBotton);
return;
}
else
{
e.HasMorePages = false;
}
}
DrawFooter(e.Graphics, nBotton);
}

private void DrawFooter(Graphics g, int nBottom)
{
Font PF = new Font("굴림", 20);

PageNo++;
string sPageNo = PageNo.ToString() + "Page ";
g.DrawLine(Pens.Red, 100, nBottom, 700, nBottom);
g.DrawString(sPageNo, PF, Brushes.Black, 300, nBottom);
}


}
}

--------------------------------------------------------------------------------------------------

/////////////////////////////////
//유용한 함수 추가
/////////////////////////////////

private void Set_Values(Graphics g, int iType, int _x, int _y, int iWidth, int iHeight, string sValue)
{
string sFont01 = "맑은 고딕";
string sFont02 = "Microsoft Sans Serif";
string sFontBarcode = "Code39(2:3)";

sFont01 = "Nsimsun";

Font m_Font40 = new Font(sFont01, 40);
Font m_Font20 = new Font(sFont01, 20);
Font m_Font16 = new Font(sFont01, 16);
Font m_Font10 = new Font(sFont01, 10);
Font m_Font08 = new Font(sFont01, 8);
Font m_Font06 = new Font(sFont01, 6);

Font m_Font30 = new Font(sFont02, 30);

Font m_Font_Bar = new Font(sFontBarcode, 24);

StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;

// Create pen.
Pen BlackPen = new Pen(Color.Black, 1);
Pen WhitePen = new Pen(Color.White, 1);

Rectangle rect = new Rectangle(_x, _y, iWidth, iHeight);

if (iType == 0)
{
// MAIN
g.FillRectangle(Brushes.Black, rect);
g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font10, Brushes.White, rect, sf);
}
else if (iType == 1)
{
g.FillRectangle(Brushes.WhiteSmoke, rect);
g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font06, Brushes.Black, rect, sf);
}
else if (iType == 2)
{
g.FillRectangle(Brushes.Black, rect);
g.DrawRectangle(WhitePen, rect);
g.DrawString(sValue, m_Font06, Brushes.White, rect, sf);
}
else if (iType == 3)
{
//g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font40, Brushes.Black, rect, sf);
}
else if (iType == 4)
{
g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font16, Brushes.Black, rect, sf);
}
else if (iType == 5)
{
//g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font10, Brushes.Black, rect, sf);
}
else if (iType == 6)
{
g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font_Bar, Brushes.Black, rect, sf);
}
else if (iType == 99)
{
// 일반 TEXT 입력
g.DrawRectangle(BlackPen, rect);
g.DrawString(sValue, m_Font06, Brushes.Black, rect, sf);
}

}

private void Set_Image(Graphics g, int _x, int _y, string sPath)
{
try
{
// @"C://Test1.jpg"
Image tempImage = Image.FromFile(sPath);

g.DrawImage(tempImage, _x, _y);
}
catch (System.IO.FileNotFoundException ex)
{
if (ex.FileName == null)
{
//MessageBox.Show("FILE NOT FOUND!!! > " + sPath);
txtMsg.Text = "FILE NOT FOUND!!! > " + sPath;
TOTxt("PrintProcess" + " > " + txtMsg.Text);
}
}

}



'Language > C#' 카테고리의 다른 글

ToString(IFormatProvider provider) 형식  (0) 2016.02.04
WPF 예제 샘플  (0) 2016.02.04
VisualStudio 단축키  (0) 2016.02.04
MyBatis.NET CRUD(1) - mySql DB연동  (0) 2016.02.04
MyBatis.NET 기본 및 환경 설정  (0) 2016.02.04