Skip to main content Skip to footer

Spread Windows Forms and Printing

Spread Windows Forms supports printing to a PDF file or previewing the output before printing. This allows you to check the data and layout before printing, which can save paper. This example saves the sheets to a PDF file. C#

private void SpreadWin1_Load(object sender, EventArgs e)  
        {  
            fpSpread1.Sheets.Count = 3;  
            fpSpread1.Sheets[0].RowCount = 10;  
            fpSpread1.Sheets[0].ColumnCount = 10;  
            fpSpread1.Sheets[1].RowCount = 10;  
            fpSpread1.Sheets[1].ColumnCount = 10;  
            fpSpread1.Sheets[2].RowCount = 10;  
            fpSpread1.Sheets[2].ColumnCount = 10;  
            fpSpread1.Sheets[0].SetClip(0, 0, 3, 3, "Sunday\\tMonday\\tTuesday\\r\\nWednesday\\tThursday\\tFriday\\r\\nSaturday\\tSunday\\tMonday");  
            fpSpread1.Sheets[1].SetClip(0, 0, 3, 3, "January\\tFebruary\\tMarch\\r\\nApril\\tMay\\tJune\\r\\nJuly\\tAugust\\tSeptember");  
            fpSpread1.Sheets[2].SetClip(0, 0, 3, 3, "2012\\t2013\\t2014\\r\\n2015\\t2016\\t2017\\r\\n2018\\t2019\\t2020");  
        }  

        private void button1_Click(object sender, EventArgs e)  
        {  
            // Define the printer settings  
            FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo();  
            printset.Header = "Print Job for GrapeCity Inc.";              
            printset.PdfWriteTo = FarPoint.Win.Spread.PdfWriteTo.File;  
            printset.PdfWriteMode = FarPoint.Win.Spread.PdfWriteMode.Append;              
            printset.PrintToPdf = true;  
            printset.PdfFileName = "C:\\\results.pdf";  
            // Assign the printer settings to the sheet and print to PDF  
            fpSpread1.Sheets[0].PrintInfo = printset;  
            fpSpread1.Sheets[1].PrintInfo = printset;  
            fpSpread1.Sheets[2].PrintInfo = printset;  
            fpSpread1.PrintSheet(-1);  
        }

VB

Private Sub SpreadWin1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        FpSpread1.Sheets.Count = 3  
        FpSpread1.Sheets(0).RowCount = 10  
        FpSpread1.Sheets(0).ColumnCount = 10  
        FpSpread1.Sheets(1).RowCount = 10  
        FpSpread1.Sheets(1).ColumnCount = 10  
        FpSpread1.Sheets(2).RowCount = 10  
        FpSpread1.Sheets(2).ColumnCount = 10  
        FpSpread1.Sheets(0).SetClip(0, 0, 3, 3, "Sunday" + vbTab + "Monday" + vbTab + "Tuesday" + vbCrLf + "Wednesday" + vbTab + "Thursday" + vbTab + "Friday" + vbCrLf + "Saturday" + vbTab + "Sunday" + vbTab + "Monday")  
        FpSpread1.Sheets(1).SetClip(0, 0, 3, 3, "January" + vbTab + "February" + vbTab + "March" + vbCrLf + "April" + vbTab + "May" + vbTab + "June" + vbCrLf + "July" + vbTab + "August" + vbTab + "September")  
        FpSpread1.Sheets(2).SetClip(0, 0, 3, 3, "2012" + vbTab + "2013" + vbTab + "2014" + vbCrLf + "2015" + vbTab + "2016" + vbTab + "2017" + vbCrLf + "2018" + vbTab + "2019" + vbTab + "2020")  
    End Sub  

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        ' Define the printer settings  
        Dim printset As New FarPoint.Win.Spread.PrintInfo()  
        printset.Header = "Print Job for GrapeCity Inc."  
        printset.PdfWriteTo = FarPoint.Win.Spread.PdfWriteTo.File  
        printset.PdfWriteMode = FarPoint.Win.Spread.PdfWriteMode.Append  
        printset.PrintToPdf = True  
        printset.PdfFileName = "C:\\results.pdf"  
        ' Assign the printer settings to the sheet And print to PDF  
        FpSpread1.Sheets(0).PrintInfo = printset  
        FpSpread1.Sheets(1).PrintInfo = printset  
        FpSpread1.Sheets(2).PrintInfo = printset  
        FpSpread1.PrintSheet(-1)  
    End Sub  

This example uses the Preview property in the PrintInfo class to preview a sheet before printing. SpreadWinPreview Preview Notes and Shapes C#

private void SpreadWin1_Load(object sender, EventArgs e)  
        {  
            fpSpread1.TextTipPolicy = FarPoint.Win.Spread.TextTipPolicy.Fixed;  
            fpSpread1.Sheets[0].Cells[1, 1].NoteStyle = FarPoint.Win.Spread.NoteStyle.StickyNote;  
            fpSpread1.Sheets[0].Cells[1, 1].Note = "Printing";  
            fpSpread1.Sheets[0].Cells[5, 1].Text = "Print Shape and Note";  
            fpSpread1.AllowCellOverflow = true;  

            FarPoint.Win.Spread.DrawingSpace.ArrowShape arrow = new FarPoint.Win.Spread.DrawingSpace.ArrowShape();  
            arrow.BackColor = Color.Red;  
            arrow.ForeColor = Color.DarkRed;  
            arrow.SetBounds(0, 20, 100, 50);  
            fpSpread1.Sheets[0].AddShape(arrow);  
        }  

        private void button1_Click(object sender, EventArgs e)  
        {  
            FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo();              
            printset.PrintNotes = FarPoint.Win.Spread.PrintNotes.AsDisplayed;  
            printset.PrintShapes = true;  
            printset.Preview = true;  
            fpSpread1.Sheets[0].PrintInfo = printset;  
            fpSpread1.PrintSheet(0);  
        }  

VB

Private Sub SpreadWin1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        FpSpread1.TextTipPolicy = FarPoint.Win.Spread.TextTipPolicy.Fixed  
        FpSpread1.Sheets(0).Cells(1, 1).NoteStyle = FarPoint.Win.Spread.NoteStyle.StickyNote  
        FpSpread1.Sheets(0).Cells(1, 1).Note = "Printing"  
        FpSpread1.Sheets(0).Cells(5, 1).Text = "Print Shape and Note"  
        FpSpread1.AllowCellOverflow = True  

        Dim arrow = New FarPoint.Win.Spread.DrawingSpace.ArrowShape()  
        arrow.BackColor = Color.Red  
        arrow.ForeColor = Color.DarkRed  
        arrow.SetBounds(0, 20, 100, 50)  
        FpSpread1.Sheets(0).AddShape(arrow)  
    End Sub  

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Dim printset As New FarPoint.Win.Spread.PrintInfo()  
        printset.PrintNotes = FarPoint.Win.Spread.PrintNotes.AsDisplayed  
        printset.PrintShapes = True  
        printset.Preview = True  
        FpSpread1.Sheets(0).PrintInfo = printset  
        FpSpread1.PrintSheet(0)  
    End Sub

This example creates headers and footers for the printed sheet. SpreadWinpp Preview with Header C#

private void SpreadWin1_Load(object sender, EventArgs e)  
{  
//Add sample data  
string conStr = "Provider=Microsoft.JET.OLEDB.4.0;data source= C:\\\Program Files (x86)\\\GrapeCity\\\Spread Studio 8\\\Common\\\nwind.mdb";  
string sqlStr = "Select CompanyName, ContactName, ContactTitle, Country from Customers";  
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(conStr);  
DataSet ds = new DataSet();  
System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter(sqlStr, conn);  
da.Fill(ds);  
fpSpread1.ActiveSheet.DataAutoSizeColumns = true;  
fpSpread1.ActiveSheet.DataMember = "Patients";  
fpSpread1.ActiveSheet.DataSource = ds;  
}  

private void button1_Click(object sender, EventArgs e)  
{  
// Create PrintInfo object and set properties.  
FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo();  
printset.Colors = new Color[] { Color.Green, Color.Yellow, Color.Gold, Color.Indigo, Color.Brown };  
printset.Header = "/fn\\"Book Antiqua\\" /fz\\"14\\" Print job for GrapeCity./n ";  
printset.Footer = "/r/cl\\"4\\"This is page /p of /pc";  
printset.Preview = true;  
// Set the PrintInfo property for the first sheet.  
fpSpread1.Sheets[0].PrintInfo = printset;  
// Print the sheet.  
fpSpread1.PrintSheet(0);  
}

VB

Private Sub SpreadWin1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
Dim conStr As String = "Provider=Microsoft.JET.OLEDB.4.0;data source= C:\\Program Files (x86)\\GrapeCity\\Spread Studio 8\\Common\\nwind.mdb"  
Dim sqlStr As String = "Select CompanyName, ContactName, ContactTitle, Country from Customers"  
Dim conn As New System.Data.OleDb.OleDbConnection(conStr)  
Dim ds As DataSet = New DataSet()  
Dim da As New System.Data.OleDb.OleDbDataAdapter(sqlStr, conn)  
da.Fill(ds)  
FpSpread1.ActiveSheet.DataAutoSizeColumns = True  
FpSpread1.ActiveSheet.DataMember = "Patients"  
FpSpread1.ActiveSheet.DataSource = ds  
End Sub  

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
' Create PrintInfo object and set properties.  
Dim printset As New FarPoint.Win.Spread.PrintInfo()  
printset.Colors = New Color() {Color.Green, Color.Yellow, Color.Gold, Color.Indigo, Color.Brown}  
printset.Header = "/fn""Book Antiqua"" /fz""14"" Print job for GrapeCity./n "  
printset.Footer = "/g""1""/r/cl""4""This is page /p of /pc"  
' Set the PrintInfo property for the first sheet.  
printset.Preview = True  
FpSpread1.Sheets(0).PrintInfo = printset  
' Print the sheet.  
FpSpread1.PrintSheet(0)  
End Sub  

You can also use a print dialog control to preview the printing. You can use a print preview control in the PrintPreviewShowing event or with the SetPrintPreview method. This example uses a print preview control in the Spread PrintPreviewShowing event to preview the data. C#

private void button1_Click(object sender, EventArgs e)  
        {  
            FarPoint.Win.Spread.PrintInfo printset = new FarPoint.Win.Spread.PrintInfo();  
            printset.Preview = true;  
            printset.ShowPrintDialog = true;  
            fpSpread1.Sheets[0].PrintInfo = printset;  
            fpSpread1.PrintSheet(0);              
        }  

        private void fpSpread1_PrintPreviewShowing(object sender, FarPoint.Win.Spread.PrintPreviewShowingEventArgs e)  
        {  
            e.Cancel = true;  
            PrintPreviewDialog printPreviewDialog1  = new PrintPreviewDialog();            
            printPreviewDialog1.Document = e.PreviewDialog.Document;              
            printPreviewDialog1.ShowDialog();  
        }

VB

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Dim printset As New FarPoint.Win.Spread.PrintInfo()  
        printset.Preview = True  
        printset.ShowPrintDialog = True  
        FpSpread1.Sheets(0).PrintInfo = printset  
        FpSpread1.PrintSheet(0)  
    End Sub  

    Private Sub FpSpread1_PrintPreviewShowing(sender As Object, e As FarPoint.Win.Spread.PrintPreviewShowingEventArgs) Handles FpSpread1.PrintPreviewShowing  
        e.Cancel = True  
        Dim printPreviewDialog1 As New PrintPreviewDialog()  
        printPreviewDialog1.Document = e.PreviewDialog.Document  
        printPreviewDialog1.ShowDialog()  
    End Sub

This example uses the SetPrintPreview method and sets properties for the print preview control. spreadwinsetpreview Custom Preview Settings C#

private void Form1_Load(object sender, EventArgs e)  
        {  
            fpSpread1.Sheets.Count = 3;  
            fpSpread1.Sheets[0].RowCount = 10;  
            fpSpread1.Sheets[0].ColumnCount = 10;  
            fpSpread1.Sheets[1].RowCount = 10;  
            fpSpread1.Sheets[1].ColumnCount = 10;  
            fpSpread1.Sheets[2].RowCount = 10;  
            fpSpread1.Sheets[2].ColumnCount = 10;  
            fpSpread1.Sheets[0].SetClip(0, 0, 3, 3, "Sunday\\tMonday\\tTuesday\\r\\nWednesday\\tThursday\\tFriday\\r\\nSaturday\\tSunday\\tMonday");  
            fpSpread1.Sheets[1].SetClip(0, 0, 3, 3, "January\\tFebruary\\tMarch\\r\\nApril\\tMay\\tJune\\r\\nJuly\\tAugust\\tSeptember");  
            fpSpread1.Sheets[2].SetClip(0, 0, 3, 3, "2012\\t2013\\t2014\\r\\n2015\\t2016\\t2017\\r\\n2018\\t2019\\t2020");  
         }  

        private void button1_Click(object sender, EventArgs e)  
        {              
            PrintPreviewDialog pi = new PrintPreviewDialog();  
            pi.BackColor = Color.Cyan;  
            pi.ForeColor = Color.Red;  
            pi.Text = "My preview dialog";    
            fpSpread1.SetPrintPreview(pi);  
            fpSpread1.ActiveSheet.PrintInfo.Preview = true;  
            fpSpread1.ActiveSheet.PrintInfo.ZoomFactor = 0.75f;  
            fpSpread1.PrintSheet(fpSpread1.ActiveSheet);  
        }

VB

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load  
        FpSpread1.Sheets.Count = 3  
        FpSpread1.Sheets(0).RowCount = 10  
        FpSpread1.Sheets(0).ColumnCount = 10  
        FpSpread1.Sheets(1).RowCount = 10  
        FpSpread1.Sheets(1).ColumnCount = 10  
        FpSpread1.Sheets(2).RowCount = 10  
        FpSpread1.Sheets(2).ColumnCount = 10  
        FpSpread1.Sheets(0).SetClip(0, 0, 3, 3, "Sunday" + vbTab + "Monday" + vbTab + "Tuesday" + vbCrLf + "Wednesday" + vbTab + "Thursday" + vbTab + "Friday" + vbCrLf + "Saturday" + vbTab + "Sunday" + vbTab + "Monday")  
        FpSpread1.Sheets(1).SetClip(0, 0, 3, 3, "January" + vbTab + "February" + vbTab + "March" + vbCrLf + "April" + vbTab + "May" + vbTab + "June" + vbCrLf + "July" + vbTab + "August" + vbTab + "September")  
        FpSpread1.Sheets(2).SetClip(0, 0, 3, 3, "2012" + vbTab + "2013" + vbTab + "2014" + vbCrLf + "2015" + vbTab + "2016" + vbTab + "2017" + vbCrLf + "2018" + vbTab + "2019" + vbTab + "2020")  
    End Sub  

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
        Dim pi As New PrintPreviewDialog()  
        pi.BackColor = Color.Cyan  
        pi.ForeColor = Color.Red  
        pi.Text = "My preview dialog"  
        FpSpread1.SetPrintPreview(pi)  
        FpSpread1.ActiveSheet.PrintInfo.Preview = True  
        FpSpread1.ActiveSheet.PrintInfo.ZoomFactor = 0.75F  
        FpSpread1.PrintSheet(FpSpread1.ActiveSheet)  
    End Sub

MESCIUS inc.

comments powered by Disqus