Question
my Question is about GTP.NET component.
how to print multiple page on c# code?
Answer
.NET comes with a very good PrintDocument class. Use this when you want to print the Gantt.
You might wonder why we just did not wrap all this up in the componen?t The answer is flexibility. You will probably want to add a title and a page number or something we never could anticipate, and you can do all of this in the PrintPage event.
The sample below shows how the printing mechanism is splitting the print to many pages over the Y-axis (grid rows). To split the print to several pages over the X-Axis (different pages show different time), you should execute the print operation two (or many) times with different values set on the datescaler.
Like this:
private void Print_Click(object sender, System.EventArgs e)< ?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
{
try
{
// Assumes the default printer.
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
pd.BeginPrint += new PrintEventHandler(this.pd_BeginPrint);
pd.EndPrint += new PrintEventHandler(this.pd_EndPrint);
printPreviewDialog1.Document=pd;
printPreviewDialog1.ShowDialog(this);
}
catch(Exception ex)
{
MessageBox.Show(“An error occurred while printing”, ex.ToString());
}
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
bool hasMorePages=false;
Rectangle r=Rectangle.FromLTRB(20,20,ev.MarginBounds.Right-20,ev.MarginBounds.Bottom-100);
gantt1.PrintPage(ev.Graphics,r,100,50,ref hasMorePages);
ev.HasMorePages=hasMorePages;
}
public void pd_BeginPrint(object sender, PrintEventArgs e)
{
gantt1.PrintInit(null);
}
public void pd_EndPrint(object sender,PrintEventArgs e)
{
gantt1.PrintEnd();
}
private void PrintPreview_Click(object sender, System.EventArgs e)
{
try
{
PrintDocument pd = new PrintDocument();
pd.PrinterSettings.FromPage = 5; // You can start from a given page
pd.BeginPrint += new PrintEventHandler(this.pd_BeginPrint); // Called first
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage); // called for each page
pd.EndPrint += new PrintEventHandler(this.pd_EndPrint); // called last
firstInx = true; // In this sample I want to print two pages along the x-axis with different time shown and grid supressed on the latter
printPreviewDialog1.Document = pd;
printPreviewDialog1.ShowDialog(this); // And show it in a preview
}
catch (Exception ex)
{
MessageBox.Show(“An error occurred while printing”, ex.ToString());
}
}
private DateTime start, stop;
private bool firstInx;
public void pd_BeginPrint(object sender, PrintEventArgs e)
{
gantt1.PrintInit(null); // Prepare for print
}
private void pd_PrintPage(object sender, PrintPageEventArgs ev)
{
bool hasMorePages = false;
Rectangle r = Rectangle.FromLTRB(20, 20, ev.MarginBounds.Right – 20, ev.MarginBounds.Bottom – 100);
// In this print I choose to divide the time axel into two pages.
// Then I must call PrintPage once with SuppressGrid=false and doNotStepGridWillPrintTheSameRowsOneMoreTime=true
// and once with SuppressGrid=true and doNotStepGridWillPrintTheSameRowsOneMoreTime=false
// In the second call I change the DateScaler intervall to reflect another area in time, then I set it back
// after the print to prepare for the first page in time (x) direction
if (firstInx /*This is the first page in X-direction*/)
{
gantt1.PrintPage(ev.Graphics, r, 100, 50, false, ref hasMorePages, true /* IMPORTANT FOR MULTI X PRINT: PrintTheSameBatchOfRowsNextTime=true*/);
start = gantt1.DateScaler.StartTime;
stop = gantt1.DateScaler.StopTime;
firstInx = false; // Signals that the next print will be the same
}
else
{
// This is the second page in X-direction
gantt1.DateScaler.TimeSpanSet(stop, stop.Add(stop.Subtract(start)));
gantt1.PrintPage(ev.Graphics, r, 100, 50, true, ref hasMorePages, false);
gantt1.DateScaler.TimeSpanSet(start, stop);
firstInx = true;
}
ev.HasMorePages = hasMorePages; // When this is false the print document will not all PrintPage anymore
}
public void pd_EndPrint(object sender, PrintEventArgs e)
{
gantt1.PrintEnd(); // clean up after print
}