|
using System;
using System.Collections.Generic;
using System.Text;
using BizDraw.Core ;
using System.Drawing.Printing ;
using System.Drawing ;
using System.Windows.Forms;
namespace BizDraw.Printing
{
[System.Runtime.InteropServices.ComVisible(true)]
public class DocumentPrintEngine
{
private Document _document;
private int _count = 0;
private int _pagesCount = 0;
private int _columns = 0;
private int _rows = 0;
private int currentPage;
private List<Cell> _cells;
internal PrintDocument printDocument;
private PrintFormat _format;
internal event EventHandler ParametersChanged;
public DocumentPrintEngine()
{
printDocument = new PrintDocument();
this.printDocument.BeginPrint += new PrintEventHandler(printDocument_BeginPrint);
this.printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
this.printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);
}
#region Properties
/// <summary>
/// Get/Set the document to print
/// </summary>
public Document Document
{
get
{
return _document;
}
set
{
_document = value;
}
}
/// <summary>
/// Get/Set the number of documents to print
/// </summary>
public int Count
{
get
{
return _count ;
}
set
{
if (_count != value )
{
_count = value;
Prepare();
}
}
}
/// <summary>
/// get the number of columns per page
/// </summary>
public int Columns
{
get { return _columns; }
}
/// <summary>
/// Get the number of rows per page
/// </summary>
public int Rows
{
get { return _rows; }
}
/// <summary>
/// Gets the number of documents to print per page
/// </summary>
public int DocumentsPerPage
{
get
{
return Columns * Rows ;
}
}
/// <summary>
/// Gets the number of pages to print
/// </summary>
public int PagesCount
{
get
{
return _pagesCount;
}
}
/// <summary>
/// Get/Set the printing format used to print the document
/// </summary>
public PrintFormat Format
{
get { return _format; }
set {
if (_format != value)
{
_format = value;
Prepare();
}
}
}
public List<Cell> Cells
{
get
{
return _cells;
}
}
#endregion
#region Methods
/// <summary>
/// Do calculations for the print operation
/// </summary>
internal void Prepare()
{
if (this.Document.IsDataBound())
{
this.Count = this.Document.DataSource.RowsCount;
}
_columns =Convert.ToInt32( Math.Floor((double)(this.Format.PrintableArea.Width + this.Format.ColumnsSepWidth) / (this.Format.ColumnsSepWidth + this.Document.Width)));
_rows = Convert.ToInt32( Math.Floor((double)(this.Format.PrintableArea.Height + this.Format.ColumnsSepWidth) / (this.Format.ColumnsSepWidth + this.Document.Height )));
_cells = new List<Cell>(this.Count );
_pagesCount = 0;
int z = 0;
while (z < this.Count)
{
for (int i = 0; i < _rows; i++)
{
for (int j = 0; j < _columns; j++)
{
if (z >= this.Count)
{
_pagesCount++;
goto end;
}
this.Cells.Add(new Cell(i, j));
z++;
}
}
_pagesCount++;
}
end:
if (ParametersChanged != null)
ParametersChanged(this, EventArgs.Empty);
}
/// <summary>
/// Prints the document using the current properties
/// </summary>
public void Print()
{
currentPage = 0;
this.printDocument.DefaultPageSettings = this.Format.PageSettings;
this.printDocument.Print();
}
/// <summary>
/// Show th printing setup dialog
/// </summary>
public void ShowDialog()
{
FrmPrintSetup frm = new FrmPrintSetup();
frm.PrintEngine = this;
frm.ShowDialog();
}
#endregion
#region Print events
void printDocument_BeginPrint(object sender, PrintEventArgs e)
{
Document.DesignMode = false;
}
void printDocument_EndPrint(object sender, PrintEventArgs e)
{
Document.DesignMode = true;
if (Document.IsDataBound())
Document.DataSource.EndInitialize();
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
int i = 0;
e.Graphics.ScaleTransform(100f / 96f, 100f / 96f);
for (i = DocumentsPerPage * currentPage; i < (currentPage + 1) * DocumentsPerPage; i++)
{
if (i >= this.Count)
{
e.HasMorePages = false;
return;
}
Cell cell = this.Cells[i];
double mmToPixel = 96 / 25.4d;
int xTranslation = Convert.ToInt32(cell.Column * (Document.Width + Format.ColumnsSepWidth) * mmToPixel + Format.PageSettings.Margins.Left );
int yTranslation = Convert.ToInt32((double)cell.Row * (double)(Document.Height + Format.RowsSepHeight ) * mmToPixel + (double)Format.PageSettings.Margins.Top);
System.Drawing.Drawing2D.GraphicsContainer container = e.Graphics.BeginContainer();
e.Graphics.TranslateTransform((float)xTranslation, (float)yTranslation);
Document.Items.Draw(e.Graphics);
e.Graphics.EndContainer(container);
if (this.Document.IsDataBound())
this.Document.DataSource.MoveNext();
}
currentPage++;
if (currentPage >= this.PagesCount)
e.HasMorePages = false;
else
e.HasMorePages = true;
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.
A list of licenses authors might use can be found here