PDA

View Full Version : چاپ rich text



don_alen
شنبه 20 تیر 1388, 18:58 عصر
سلام

آیا امکانش هست از یه rich text پیشنمایش چاپ گرفت یا از یه فایل rtf ؟؟؟؟ اگر امکانش هست توضیح بدین....

اگر بشه مستقیم هم چاپ گرفت بازم خوبه....

من خودم خواستم از کنترل های printing استفاده کنم ، ولی نتونستم

sinashahab
شنبه 20 تیر 1388, 23:06 عصر
سلام دوست عزیز
این هم کد ها :
این دو فضای نام ها رو اضافه کن :


using System.IO;
using System.Drawing.Printing;

این دو متغییر رو به صورت سراسری ایجاد کن :


private StreamReader objStreamToPrint;
private Font objPrintFont;

این متد رو تعریف کن :


private void prtPage(object sender, PrintPageEventArgs e)
{
StringFormat f = new StringFormat();
f.Alignment = StringAlignment.Near;

float sngLinesPerpage = 0;
float sngVerticalPosition = 0;
int intLineCount = 0;
float sngLeftMargin = e.MarginBounds.Left;
float sngTopMargin = e.MarginBounds.Top;
string strLine;

sngLinesPerpage = e.MarginBounds.Height /
objPrintFont.GetHeight(e.Graphics);

strLine = objStreamToPrint.ReadLine();
while ((intLineCount < sngLinesPerpage) &&
(strLine != null))
{

sngVerticalPosition = sngTopMargin +
(intLineCount * objPrintFont.GetHeight(e.Graphics));


e.Graphics.DrawString(strLine, objPrintFont,
Brushes.Black,e.MarginBounds.Right + 20,
sngVerticalPosition,
new StringFormat(f.FormatFlags = StringFormatFlags.DirectionRightToLeft));

intLineCount = intLineCount + 1;


if (intLineCount < sngLinesPerpage)
{
strLine = objStreamToPrint.ReadLine();
}
}

if (strLine != null)
{
e.HasMorePages = true;
}
else
{
e.HasMorePages = false;
}
this.Close();
}

بر روی دکمه هم دو بار کلیک کن تا event دکمه ایجاد و بعد بنویس :


PrintDocument objPrintDocument = new PrintDocument();

objPrintDocument.DocumentName = "Text File PrintDemo";

printDialog1.AllowPrintToFile = false;
printDialog1.AllowSelection = false;
printDialog1.AllowSomePages = false;


printDialog1.Document = objPrintDocument;

if (printDialog1.ShowDialog() == DialogResult.OK)
{


objStreamToPrint = new StreamReader("Temp");

objPrintFont = new Font("Tahoma", 12);


objPrintDocument.PrinterSettings = printDialog1.PrinterSettings;


objPrintDocument.PrintPage += new PrintPageEventHandler(prtPage);

objPrintDocument.Print();

objStreamToPrint.Close();
objStreamToPrint = null;
}

به همین سادگی . فقط باید به جای Temp آدرس فایل رو بنویسید .

تشکر یادت نره .

don_alen
دوشنبه 22 تیر 1388, 02:14 صبح
nokaram b molaaaaaaaaaaaaaa