PDA

View Full Version : سوال: پرینت اطلاعات فرم بصورت یک جدول



afravi
یک شنبه 22 آذر 1388, 12:08 عصر
سلام خدمت دوستان گرامی
من اطلاعات یک فرم رو می خوام به صورت یک جدول پرینت بگیرم آیا کرستال ریپورت این امکان رو داره یا نه اگر داره خواهش می کنم در این مورد کمکی به من بکنید که چطوری این را کار انجام بدم
با تشکر

FastCode
شنبه 28 آذر 1388, 20:35 عصر
کریستال اگه داشت اسمشو میگذاشتند الماس.
من برای این کار از این کد استفاده کردم.
یکمش نامفهومه چون اگر میخواستم همشو بفرستم 1000 خط میشد.

public sealed class DataGridViewPrinter
{
//Collection<DataGridViewPrintRow> rows;
private DataGridView dataGridView;
private Collection<DataGridViewColumn> columns;
private GridOptions options;
private System.Drawing.Printing.PrintDocument Document;
private object[][] Cache;
private int CurrentPage;
private int Width;
private float[] Widths;
private float[] dataGridView_RowHeights;
private float dataGridView_ColumnHeadersHeight;
private float MarginAndWidth;
private float WidthAndLeftMargin;
private float WidthAndRightMargin;
private float MarginLessWidth;
private float MarginLessHeight;
private float Y;
private int RowIndex;//Cell Graphic Temps
private object[] Temps;
public DataGridViewPrinter(DataGridView DataGridView, GridOptions Options)
{
int i;
options = Options;
dataGridView = DataGridView;
Document = new System.Drawing.Printing.PrintDocument();
columns = new Collection<DataGridViewColumn>();

Width = 0;
RowIndex = -1;
Document.DefaultPageSettings.Landscape = options.PrintOptions.Oriention == PrintOriention.Landscape;

MarginAndWidth = Document.DefaultPageSettings.Bounds.Width;

MarginLessWidth = MarginAndWidth - (options.PrintOptions.LeftMargin + options.PrintOptions.RightMargin);
WidthAndLeftMargin = MarginAndWidth - options.PrintOptions.RightMargin;
WidthAndRightMargin = MarginAndWidth - options.PrintOptions.LeftMargin;

MarginLessHeight = Document.DefaultPageSettings.Bounds.Height - (options.PrintOptions.TopMargin + options.PrintOptions.TopMargin);


i = 0;
dataGridView_ColumnHeadersHeight = DataGridView.ColumnHeadersHeight;
dataGridView_RowHeights = new float[DataGridView.RowCount];
Cache = new object[DataGridView.RowCount][];
foreach (System.Windows.Forms.DataGridViewRow Row in DataGridView.Rows) { dataGridView_RowHeights[Row.Index] = Row.Height; Cache[Row.Index] = new object[dataGridView.ColumnCount]; }
for (int rc = 0; rc != DataGridView.RowCount; rc++) for (int cc = 0; cc != DataGridView.ColumnCount; cc++) Cache[rc][cc] = dataGridView[cc, rc].Value;
foreach (System.Windows.Forms.DataGridViewColumn Col in DataGridView.Columns) { if (((ICollection<ColState>)options).GetAt(i).PrintOptions.Visible) columns.Add(Col); i++; }
Widths = new float[columns.Count];
Temps = new string[columns.Count];
i = 0;
DataGridViewColumn[] cols = new DataGridViewColumn[columns.Count];
for (i = 0; i != columns.Count; i++) cols[i] = columns[i];
columns.Clear(); i = 0;
for (i = 0; i != cols.GetUpperBound(0) + 1; i++) columns.Add(cols[i]);
foreach (System.Windows.Forms.DataGridViewColumn Col in columns) Width += ((ICollection<ColState>)options).GetAt(Col.DisplayIndex).PrintOptions.Wid th;
i = 0;
foreach (System.Windows.Forms.DataGridViewColumn Col in columns) { Widths[i] = MarginLessWidth * ((ICollection<ColState>)options).GetAt(Col.DisplayIndex).PrintOptions.Wid th / Width; i++; }
i = 0;
foreach (System.Windows.Forms.DataGridViewColumn Col in columns) if (Col.HeaderCell.Style.Font == null) Col.HeaderCell.Style.Font = DataGridView.ColumnHeadersDefaultCellStyle.Font;
i = 0;
}
private bool DrawNextPage(Graphics g)
{
CurrentPage++;
Y = options.PrintOptions.TopMargin;
DrawHeader(g);
if (CurrentPage == 1) DrawLabelRow(g, options.PrintOptions.FirstPageTopRows, false);
DrawLabelRow(g, options.PrintOptions.TopRows, false);
float BY = 0;
BY += DrawLabelRow(g, options.PrintOptions.BottomRows, true);
if (CurrentPage == 1) BY += DrawLabelRow(g, options.PrintOptions.FirstPageBottomRows, true);
bool bContinue = DrawRows(g, BY);
if (!bContinue) BY += DrawLabelRow(g, options.PrintOptions.LastPageBottomRows, false);
return bContinue;
}
private bool DrawRows(Graphics g, float BY)
{
if ((options.PrintOptions.HeaderPrintCount == ObjectPrintCount.Firstpage && CurrentPage == 1) || options.PrintOptions.HeaderPrintCount == ObjectPrintCount.EachPage)
{
for (int cc = 0; cc != columns.Count; cc++) { Temps[cc] = columns[cc].HeaderText; }
DrawRects(g, BY);
Y += 2;
}
do
{
RowIndex++;
if (RowIndex == Cache.GetUpperBound(0) + 1) return false;
Temps = Cache[RowIndex];
bool bcontinue = DrawRects(g, BY);
if (bcontinue == true) return bcontinue;
}
while (true);
}
private bool DrawRects(Graphics g, float BY)
{
float DY;
if (RowIndex == -1)
DY = dataGridView_ColumnHeadersHeight;
else
DY = (float)(dataGridView_RowHeights[RowIndex]);
float NY = Y + DY;
if (NY > this.MarginLessHeight + options.PrintOptions.TopMargin - BY) return true;
float X;
g.DrawLine(Pens.Black, options.PrintOptions.LeftMargin, Y, MarginLessWidth + options.PrintOptions.LeftMargin, Y);
g.DrawLine(Pens.Black, options.PrintOptions.LeftMargin, NY, MarginLessWidth + options.PrintOptions.LeftMargin, NY);
g.DrawLine(Pens.Black, options.PrintOptions.LeftMargin, Y, options.PrintOptions.LeftMargin, NY);


if (options.PrintOptions.RightToLeft == RightToLeft.Yes)
{
X = MarginLessWidth;
for (int cc = 0; cc != columns.Count - 1; cc++)
{
X -= Widths[cc];
g.DrawLine(Pens.Black, X, Y, X, NY);
}
}
else
{
X = options.PrintOptions.LeftMargin;
for (int cc = 0; cc != columns.Count; cc++)
{
g.DrawLine(Pens.Black, X, Y, X, NY);
X += Widths[cc];
}
}
if (options.PrintOptions.RightToLeft == RightToLeft.Yes)
{
X = MarginLessWidth + options.PrintOptions.LeftMargin;
StringFormat Format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.DirectionRightToLeft | StringFormatFlags.FitBlackBox);
if (RowIndex == -1)
for (int cc = 0; cc != columns.Count; cc++)
{
X -= Widths[cc];
g.DrawString(CStr(Temps[cc]), new Font("Courier New", 9, FontStyle.Bold), Brushes.Black, new RectangleF(X, (float)(Y + 2.5), Widths[cc], DY), Format);
if (cc == 0) X -= options.PrintOptions.LeftMargin;
}
else
for (int cc = 0; cc != columns.Count; cc++)
{
X -= Widths[cc];
g.DrawString(CStr(Temps[cc]), new Font("Courier New", 8, FontStyle.Bold), Brushes.Black, new RectangleF(X, (float)(Y + 2.5), Widths[cc], DY), Format);
if (cc == 0) X -= options.PrintOptions.LeftMargin;
}
}
else
{
X = options.PrintOptions.LeftMargin;
StringFormat Format = new StringFormat(StringFormatFlags.LineLimit | StringFormatFlags.FitBlackBox);
if (RowIndex == -1)
for (int cc = 0; cc != columns.Count; cc++)
{
g.DrawString(CStr(Temps[cc]), new Font("Courier New", 9, FontStyle.Bold), Brushes.Black, new RectangleF(X, (float)(Y + 2.5), Widths[cc], DY), Format);
X += Widths[cc];
}
else
for (int cc = 0; cc != columns.Count; cc++)
{
g.DrawString(CStr(Temps[cc]), new Font("Courier New", 8, FontStyle.Bold), Brushes.Black, new RectangleF(X, (float)(Y + 2.5), Widths[cc], DY), Format);
X += Widths[cc];
}
}
g.DrawLine(Pens.Black, MarginLessWidth + options.PrintOptions.LeftMargin, Y, MarginLessWidth + options.PrintOptions.LeftMargin, NY);
Y = NY;
return false;
}

private string CStr(object p)
{
if (p == null) return string.Empty;
if (p is DBNull) return string.Empty;
return p.ToString();
}
private void DrawHeader(Graphics g)
{
Font F = new Font("Courier New", 8, FontStyle.Regular);
Y += 1;
if (options.PrintOptions.PagingMode == PagingMode.English)
{
g.DrawString("Page : " + CurrentPage.ToString(), F, Brushes.Black, options.PrintOptions.LeftMargin, Y);
}
if (options.PrintOptions.PagingMode == PagingMode.Persian)
{
string T = CurrentPage.ToString() + " : صفحه";
float W = g.MeasureString(T, F).Width;
g.DrawString(T, F, Brushes.Black, W - options.PrintOptions.RightMargin, Y);
}
Y += 8;
Y += 1;
Y += 1;
Y += 1;
}
private float DrawLabelRow(Graphics g, Collection<DataGridViewPrintRow> Rows, bool BottomToTop)
{
float R = Y;
if (!BottomToTop)
{
foreach (DataGridViewPrintRow DGVPR in Rows)
{
Y += DGVPR.TopMargin;
if (DGVPR.BorderVisible)
{
g.DrawRectangle(new Pen(DGVPR.BorderColor), options.PrintOptions.LeftMargin + DGVPR.LeftMargin, Y, MarginLessWidth - (DGVPR.LeftMargin + DGVPR.RightMargin), DGVPR.Height);
}
foreach (DataGridViewPrintLabel DGVPL in DGVPR.Labels)
{
if (DGVPL.BorderVisible)
{
if (DGVPL.BottomMargin == 0)
{
StringFormat Format = new StringFormat(StringFormatFlags.NoWrap, 429);
g.DrawRectangle(new Pen(DGVPL.BorderColor, DGVPL.BorderWidth), options.PrintOptions.LeftMargin + DGVPR.LeftMargin + DGVPL.LeftMargin + 1, Y + DGVPL.TopMargin + 1, MarginLessWidth - (DGVPR.LeftMargin + DGVPR.RightMargin + DGVPL.LeftMargin + 1 + DGVPL.RightMargin + 1), DGVPR.Height - (DGVPL.TopMargin + 1 + DGVPL.BottomMargin + 1));
g.DrawString(DGVPL.Format, DGVPL.Font, new SolidBrush(DGVPL.Color), new PointF(options.PrintOptions.LeftMargin + DGVPR.LeftMargin + DGVPL.LeftMargin + 1, Y + DGVPL.TopMargin + 1), Format);
}
else
{
StringFormat Format = new StringFormat(StringFormatFlags.NoWrap, 429);
g.DrawRectangle(new Pen(DGVPL.BorderColor, DGVPL.BorderWidth), options.PrintOptions.LeftMargin + DGVPR.LeftMargin + DGVPL.LeftMargin + 1, Y + DGVPL.TopMargin + 1, MarginLessWidth - (DGVPR.LeftMargin + DGVPR.RightMargin + DGVPL.LeftMargin + 1 + DGVPL.RightMargin + 1), DGVPR.Height - (DGVPL.BottomMargin + 1 + DGVPL.TopMargin + 1));
g.DrawString(DGVPL.Format, DGVPL.Font, new SolidBrush(DGVPL.Color), new PointF(options.PrintOptions.LeftMargin + DGVPR.LeftMargin + DGVPL.LeftMargin + 1, Y + DGVPL.TopMargin + 1), Format);
}
}
}
Y += DGVPR.Height;
Y += DGVPR.BottomMargin;
Y += 10;
}
}
else
{

}
return Y - R;
}
private void MyPrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) { e.HasMorePages = DrawNextPage(e.Graphics); }
public void ShowDialog()
{
}
private void Do()
{
Document.PrinterSettings.Copies = options.PrintOptions.PrintCount;
if (Document.PrinterSettings.CanDuplex)
Document.PrinterSettings.Duplex = options.PrintOptions.Duplex;
Document.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(MyPr intDocument_PrintPage);
try
{
Document.Print();
}
catch { }
}
public void Print()
{
System.Threading.Thread t = new System.Threading.Thread(Do, 128);
t.Priority = System.Threading.ThreadPriority.BelowNormal;
t.Start();
}
public void ShowPrintPreview()
{
DataGridViewPrintPreview DGVPP = new DataGridViewPrintPreview(options, dataGridView);
DGVPP.Show();
}
public void ShowSystemPrintDialog()
{
}
}

FastCode
شنبه 28 آذر 1388, 20:38 عصر
هر سوالی داشتی بپرس.
cache متغیر برای ذخیره اطلاعات DataGridView.
DataGridViewPrintOptions یه کلاس که مشخصات پرینت داره.

h.gheidrlou
یک شنبه 07 تیر 1394, 13:48 عصر
سلام استاد
من یک سری اطلاعات از تکست های مختلف ریختم داخل یک ریچ تکست باکس ولی در نظر دارم که اطلاعات در داخل مستطیل های ترسیم شده گرافیکی ریخته بشه و بتونم از اطلاعاتم که داخل ریچ تکست ترسیم کردم چاپ بگیرم
خیلی وقت گذاشتم براش ترسیم انجام میشه چاپ نمیگیره یا اطلاعات چاپ میشه ولی بدون اشکال ترسیم شده و .....
لطفا راهنمایی کنید