sohrabi1383
جمعه 29 آذر 1392, 16:46 عصر
چگونه «من به مدرسه رفتم» در صفحه اول و
«او از مدرسه آمد» در صفحه دوم چاپ شود کدها را گذاشتم اما هر دو در صفحه اول چاپ می شود.
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.IO;
using System.Drawing.Printing;
namespace printer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string strFileName;
private StreamReader objStreamToPrint;
private Font objPrintFont;
private void button1_Click(object sender, EventArgs e)
{
PrintDocument objPrintDocument = new PrintDocument();
// Set the DocumentName property
objPrintDocument.DocumentName = "student";
// Set the PrintDialog properties
printDialog1.AllowPrintToFile = false;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
// Set the Document property for
// the objPrintDocument object
printDialog1.Document = objPrintDocument;
// Show the Print dialog
if (printDialog1.ShowDialog() == DialogResult.OK)
{
// If the user clicked on the OK button
// then set the StreamReader object to
// the file name in the strFileName variable
// objStreamToPrint = new StreamReader(strFileName);
// Set the printer font
// objPrintFont = new Font("Arial", 10);
// Set the PrinterSettings property of the
// objPrintDocument Object to the
// PrinterSettings property returned from the
// PrintDialog control
//objPrintDocument.PrinterSettings = printDialog1.PrinterSettings;
// Add an event handler for the PrintPage event of
// the objPrintDocument object
objPrintDocument.PrintPage += new PrintPageEventHandler(prtPage);
// Print the text file
objPrintDocument.Print();
// Clean up
//objStreamToPrint.Close();
objStreamToPrint = null;
}
}
private void prtPage(object sender, PrintPageEventArgs e)
{
string drawString = "من به مدرسه رفتم.";
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new System.Drawing.Font("Arail", 10);
RectangleF recAtZero = new RectangleF(0, 0, e.PageBounds.Width, e.PageBounds.Height*2);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero, drawFormat);
RectangleF recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height*2);
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
drawString = "او از مدرسه آمد.";
drawBrush = new SolidBrush(Color.Black);
drawFont = new System.Drawing.Font("Arail", 10);
RectangleF recAtZero2 = new RectangleF(0, 100, e.PageBounds.Width, e.PageBounds.Height*2 );
drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero2, drawFormat);
recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height*2);
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
}
}
}
«او از مدرسه آمد» در صفحه دوم چاپ شود کدها را گذاشتم اما هر دو در صفحه اول چاپ می شود.
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.IO;
using System.Drawing.Printing;
namespace printer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string strFileName;
private StreamReader objStreamToPrint;
private Font objPrintFont;
private void button1_Click(object sender, EventArgs e)
{
PrintDocument objPrintDocument = new PrintDocument();
// Set the DocumentName property
objPrintDocument.DocumentName = "student";
// Set the PrintDialog properties
printDialog1.AllowPrintToFile = false;
printDialog1.AllowSelection = true;
printDialog1.AllowSomePages = true;
// Set the Document property for
// the objPrintDocument object
printDialog1.Document = objPrintDocument;
// Show the Print dialog
if (printDialog1.ShowDialog() == DialogResult.OK)
{
// If the user clicked on the OK button
// then set the StreamReader object to
// the file name in the strFileName variable
// objStreamToPrint = new StreamReader(strFileName);
// Set the printer font
// objPrintFont = new Font("Arial", 10);
// Set the PrinterSettings property of the
// objPrintDocument Object to the
// PrinterSettings property returned from the
// PrintDialog control
//objPrintDocument.PrinterSettings = printDialog1.PrinterSettings;
// Add an event handler for the PrintPage event of
// the objPrintDocument object
objPrintDocument.PrintPage += new PrintPageEventHandler(prtPage);
// Print the text file
objPrintDocument.Print();
// Clean up
//objStreamToPrint.Close();
objStreamToPrint = null;
}
}
private void prtPage(object sender, PrintPageEventArgs e)
{
string drawString = "من به مدرسه رفتم.";
SolidBrush drawBrush = new SolidBrush(Color.Black);
Font drawFont = new System.Drawing.Font("Arail", 10);
RectangleF recAtZero = new RectangleF(0, 0, e.PageBounds.Width, e.PageBounds.Height*2);
StringFormat drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero, drawFormat);
RectangleF recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height*2);
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
drawString = "او از مدرسه آمد.";
drawBrush = new SolidBrush(Color.Black);
drawFont = new System.Drawing.Font("Arail", 10);
RectangleF recAtZero2 = new RectangleF(0, 100, e.PageBounds.Width, e.PageBounds.Height*2 );
drawFormat = new StringFormat();
drawFormat.FormatFlags = StringFormatFlags.DirectionRightToLeft;
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtZero2, drawFormat);
recAtGreaterThantZero = new RectangleF(300, 0, e.PageBounds.Width, e.PageBounds.Height*2);
e.Graphics.DrawString(drawString, drawFont, drawBrush, recAtGreaterThantZero, drawFormat);
}
}
}