C# 中printDocument打印、預覽、打印機設置和打印屬性的方法


private void Form1_Load(object sender, System.EventArgs e)
{
//獲取或設置一個值,該值指示是否發送到文件或端口
printDocument1.PrinterSettings.PrintToFile = true;
//設置打印時橫向還是縱向
printDocument1.DefaultPageSettings.Landscape = true;
}
private void fileOpenMenuItem_Click(object sender, System.EventArgs e)
{
OpenFile();
}
private void OpenFile()
{
openFileDialog1.Filter = "Text Files (*.txt)|*.txt";//打開文本的類型
//獲取文件對話框的初始目錄(StartupPath)獲得bin文件下的文件
openFileDialog1.InitialDirectory = System.Windows.Forms.Application.StartupPath;
DialogResult userResponse = openFileDialog1.ShowDialog();
//MessageBox.Show(userResponse.ToString());
if (userResponse==DialogResult.OK)
{
filePath = openFileDialog1.FileName.ToString();//轉換文件路徑
}
}

private void MyPrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) //充分利用e {

int topMargin = printDocument1.DefaultPageSettings.Margins.Top;//上邊距

int leftMargin = printDocument1.DefaultPageSettings.Margins.Left;//左邊距

float linesPerPage = 0;//頁面行號

float verticalPosition = 0;//繪制字符串的縱向位置

float horizontalPosition=leftMargin;//左邊距

string textLine = null;//行字符串

currentLine = 0;//行計數器

// float Xline=0;

//int line=0;

// Calculate the number of lines per page.

linesPerPage = e.MarginBounds.Height / myFont.GetHeight(e.Graphics);

// Xline=e.MarginBounds.Width/myFont.GetHeight();

// for each text line that will fit on the page, read a new line from the document

while (currentLine < linesPerPage )

{

textLine = streamToPrint.ReadLine();

if(textLine == null) { break;

}

// 求出已經打印的范圍

verticalPosition = topMargin + currentLine * myFont.GetHeight(e.Graphics);

// 設置頁面的屬性

e.Graphics.DrawString(textLine, myFont, myBrush, horizontalPosition, verticalPosition);

// 增加行數

currentLine ++;

}

// If more lines of text exist in the file, print another page.

if (textLine != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
}
private void printPreviewButton_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint = new StreamReader(filePath);
try
{
PrintPreview();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void runtimeDialogButton_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint = new StreamReader(filePath);
try
{
RuntimeDialog();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void printPreviewControlButton_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint = new StreamReader(filePath);
try
{
PrintPreviewControl();
}
finally
{
streamToPrint.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}

}
private void RuntimeDialog()
{
PrintPreviewDialog pPDlg;
pPDlg = new PrintPreviewDialog();
pPDlg.Document = pDoc;
pPDlg.WindowState = FormWindowState.Maximized;
pPDlg.PrintPreviewControl.Columns = 2;
pPDlg.ShowDialog();
pPDlg.Dispose();
}
private void PrintPreviewControl()
{
Form formPreview = new Form();
PrintPreviewControl previewControl = new PrintPreviewControl();
previewControl.Document = printDocument1;
previewControl.StartPage = 2;
formPreview.WindowState = FormWindowState.Maximized;
formPreview.Controls.Add(previewControl);
formPreview.Controls[0].Dock = DockStyle.Fill;
formPreview.ShowDialog();
formPreview.Dispose();
}

private void PrintPreview()
{

//設置頁面的預覽的頁碼
//設置顯示頁面顯示的大小(也就是原頁面的倍數)

printPreviewDialog1.PrintPreviewControl.StartPage = 0;
printPreviewDialog1.PrintPreviewControl.Zoom =1.0;
//設置或返回窗口狀態,即該窗口是最小化、正常大小還是其他狀態。
printPreviewDialog1.WindowState = FormWindowState.Maximized;
//設置和獲取需要預覽的文檔
//將窗體顯示為指定者的模式對話框
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.ShowDialog();
}
private void PrintDoc()
{
printDialog1.Document = printDocument1;
DialogResult userResPonse= printDialog1.ShowDialog();
if(userResPonse==DialogResult.OK)
{
printDocument1.Print();
}
}

//獲取打印機的設置和打印的屬性

private void button1_Click(object sender, System.EventArgs e)
{
try
{
streamToPrint=new StreamReader(filePath);
try
{
PrintDoc();
}
catch{}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
streamToPrint.Close();
}
}
}

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM