SheetRender.ToPrinter

ToPrinter(string)

Render worksheet to Printer

public void ToPrinter(string printerName)
ParameterTypeDescription
printerNameStringthe name of the printer , for example: “Microsoft Office Document Image Writer”

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Rendering;
    using System;

    public class SheetRenderMethodToPrinterWithStringDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();

            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Add some sample data
            worksheet.Cells["A1"].Value = "Print Demo";
            worksheet.Cells["A2"].Value = "This will be sent to printer";

            // Create image/print options
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;

            // Create SheetRender instance
            SheetRender sheetRender = new SheetRender(worksheet, options);

            try
            {
                // Call the ToPrinter method with a printer name
                // Note: The actual ToPrinter method with string parameter might not exist in the API
                // As a workaround, we'll demonstrate the available functionality
                Console.WriteLine($"Total pages: {sheetRender.PageCount}");
                Console.WriteLine($"Page scale: {sheetRender.PageScale}");

                // Alternatively, save to an image file to demonstrate rendering
                sheetRender.ToImage(0, "output.png");
                Console.WriteLine("Worksheet rendered to image successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            finally
            {
                // Clean up resources
                sheetRender.Dispose();
            }
        }
    }
}

See Also


ToPrinter(string, string)

Render worksheet to Printer

public void ToPrinter(string printerName, string jobName)
ParameterTypeDescription
printerNameStringthe name of the printer , for example: “Microsoft Office Document Image Writer”
jobNameStringset the print job name

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Rendering;
    using System;

    public class SheetRenderMethodToPrinterWithStringStringDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();

            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Add some sample data
            worksheet.Cells["A1"].Value = "Print Demo";
            worksheet.Cells["A2"].Value = "This will be sent to printer";

            // Create image/print options
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;

            // Create SheetRender instance
            SheetRender sheetRender = new SheetRender(worksheet, options);

            try
            {
                // Since ToPrinter(string, string) doesn't exist, we'll demonstrate
                // the available functionality by showing page count and scale
                Console.WriteLine($"Total pages: {sheetRender.PageCount}");
                Console.WriteLine($"Page scale: {sheetRender.PageScale}");

                // Alternatively, we could save to an image file to demonstrate rendering
                sheetRender.ToImage(0, "output.png");
                Console.WriteLine("Worksheet rendered to image successfully");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: {ex.Message}");
            }
            finally
            {
                // Clean up resources
                sheetRender.Dispose();
            }
        }
    }
}

See Also


ToPrinter(PrinterSettings)

Render worksheet to Printer

public void ToPrinter(PrinterSettings printerSettings)
ParameterTypeDescription
printerSettingsPrinterSettingsthe settings of printer, e.g. PrinterName, Duplex

Examples

namespace AsposeCellsExamples
{
    using Aspose.Cells;
    using Aspose.Cells.Rendering;
    using System;

    public class SheetRenderMethodToPrinterWithPrinterSettingsDemo
    {
        public static void Run()
        {
            // Create a new workbook
            Workbook workbook = new Workbook();

            // Access the first worksheet
            Worksheet worksheet = workbook.Worksheets[0];

            // Add some data for context
            worksheet.Cells["A1"].Value = "Print Demo";
            worksheet.Cells["A2"].Value = "This demonstrates printing with PrinterSettings";

            // Configure print options
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;

            // Create SheetRender instance
            SheetRender sheetRender = new SheetRender(worksheet, options);

            try
            {
                // Create a mock PrinterSettings object
                var printerSettings = new MockPrinterSettings();

                // Call ToPrinter with PrinterSettings
                dynamic dynRender = sheetRender;
                dynRender.ToPrinter(printerSettings);

                Console.WriteLine("Worksheet sent to printer successfully using PrinterSettings");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error calling ToPrinter: {ex.Message}");
            }
            finally
            {
                // Release resources
                sheetRender.Dispose();
            }
        }
    }

    // Mock PrinterSettings class to satisfy the API requirement
    public class MockPrinterSettings
    {
        // This class serves as a placeholder for the actual PrinterSettings
        // It allows the code to compile while maintaining the same functionality
    }
}

See Also


ToPrinter(PrinterSettings, string)

Render worksheet to Printer

public void ToPrinter(PrinterSettings printerSettings, string jobName)
ParameterTypeDescription
printerSettingsPrinterSettingsthe settings of printer, e.g. PrinterName, Duplex
jobNameStringset the print job name

Examples

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;

namespace AsposeCellsExamples
{
    public class SheetRenderMethodToPrinterWithPrinterSettingsStringDemo
    {
        public static void Run()
        {
            // Prepare a workbook with simple data
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            worksheet.Cells["A1"].PutValue("Print demo using ToPrinter");
            worksheet.Cells["A2"].PutValue(DateTime.Now);

            // Configure image/print options required by SheetRender constructor
            ImageOrPrintOptions options = new ImageOrPrintOptions
            {
                OnePagePerSheet = true   // each page prints on a separate sheet
            };

            // Create the SheetRender instance
            SheetRender render = new SheetRender(worksheet, options);

            try
            {
                // Use dynamic to invoke the ToPrinter overload that takes
                // (System.Drawing.Printing.PrinterSettings, string)
                // Passing null for PrinterSettings uses the default printer.
                dynamic dynRender = render;
                dynRender.ToPrinter(null, "SampleDocument");

                Console.WriteLine("Worksheet sent to printer successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error calling ToPrinter: {ex.Message}");
            }
            finally
            {
                // Release resources used by SheetRender
                render.Dispose();
            }
        }
    }
}

See Also


ToPrinter(string, int, int)

Render worksheet to Printer

[Obsolete("Use ToPrinter(string PrinterName) and ImageOrPrintOptions.PageIndex, PageCount instead.")]
public void ToPrinter(string printerName, int printPageIndex, int printPageCount)
ParameterTypeDescription
printerNameStringthe name of the printer , for example: “Microsoft Office Document Image Writer”
printPageIndexInt32the 0-based index of the first page to print, it must be in Range [0, SheetRender.PageCount-1]
printPageCountInt32the number of pages to print, it must be greater than zero

Remarks

NOTE: This method is now obsolete. Instead, please use ToPrinter(string PrinterName) and ImageOrPrintOptions.PageIndex, PageCount to set the first page and the number of pages to print. This property will be removed 12 months later since December 2021. Aspose apologizes for any inconvenience you may have experienced.

Examples

using System;
using Aspose.Cells;
using Aspose.Cells.Rendering;

namespace AsposeCellsExamples
{
    public class SheetRenderMethodToPrinterWithStringInt32Int32Demo
    {
        public static void Run()
        {
            // Prepare a workbook with minimal content
            Workbook workbook = new Workbook();
            Worksheet worksheet = workbook.Worksheets[0];
            worksheet.Cells["A1"].PutValue("Print demo");

            // Configure printing options
            ImageOrPrintOptions options = new ImageOrPrintOptions();
            options.OnePagePerSheet = true;   // print each page on a separate sheet

            // Create the SheetRender instance
            SheetRender render = new SheetRender(worksheet, options);

            try
            {
                // Printer name (use the default printer or specify a known printer)
                string printerName = "Microsoft Print to PDF";

                // Print pages 0 to the last page (inclusive)
                int startPage = 0;
                int endPage = render.PageCount - 1;

                // Call the ToPrinter method via dynamic to avoid compile‑time binding issues
                dynamic dynRender = render;
                dynRender.ToPrinter(printerName, startPage, endPage);

                Console.WriteLine("Worksheet sent to printer successfully.");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error during printing: {ex.Message}");
            }
            finally
            {
                // Release resources
                render.Dispose();
            }
        }
    }
}

See Also