[]
        
(Showing Draft Content)

Print

DsExcel.NET allows you to output a workbook or worksheet to a printer using the PrintOut method provided by IWorkbook and IWorksheet interface respectively. This method accepts object of PrintOutOptions class as a parameter. In the background, PrintOut method uses DsPdf to print workbook to a printer. Hence, you need to reference DS.Documents.Imaging.Windows to implement the feature.

Note:

  • The PrintOut method works only on the Windows environment.

  • To print in other environments, you can export the document to PDF and then take a print of the converted document. For information on exporting a spreadsheet to PDF, see Export to PDF.

Print Options

DsExcel.NET provides PrintOutOptions class that contains various properties and events to set printing options such as printer name, number of copies, double-sided printing, first and last page to be printed, and selecting the printer paper source (input tray), etc. The following table lists all available properties:

Property

Description

ActivePrinter

Name of the target printer. Must match the printer name exactly as registered in the system.

Copies

Number of copies to print.

Collate

When printing multiple copies, true collates pages (1-2-3, 1-2-3); false groups them (1-1, 2-2, 3-3).

Duplex

Double-sided printing mode. Only effective when the printer supports duplex printing.

From

Index of the first page to print (1-based).

To

Index of the last page to print (1-based).

PaperSourceName

By default, when printing a workbook or worksheet, DsExcel .NET uses the printer's driver-default paper source(input tray). If your printer has multiple trays loaded with different paper types or sizes, you can use the PaperSourceName property of PrintOutOptions to specify which tray to use. The name is matched case-insensitively against the paper sources reported by the target printer driver.

  • If PaperSourceName is null (the default), the printer uses its default paper source.

  • If the specified name is not found for the target printer, an ArgumentException is thrown.

PrintBackgroundPicture

Indicates whether the worksheet's background image will be printed on the page.

PrintTransparentCell

Indicates whether the transparency of the cell's background color is preserved when printing.

ShrinkToFitSettings

Provides settings for performing "shrink to fit" on wrapped text during printing.

To print a document with specified printing options, you need to create an instance of the PrintOutOptions class, define the options for that instance and pass that instance while calling the PrintOut method.

The following example demonstrates how to configure print options to match your printer's capabilities and your printing requirements.

// Create a workbook and set data.
var workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

object[,] data = new object[,]
{
    { "Name",    "City",       "Birthday",                 "Eye color", "Weight", "Height" },
    { "Richard", "New York",   new DateTime(1968, 6, 8),   "Blue",      67,       165 },
    { "Nia",     "New York",   new DateTime(1972, 7, 3),   "Brown",     62,       134 },
    { "Jared",   "New York",   new DateTime(1964, 3, 2),   "Hazel",     72,       180 },
    { "Natalie", "Washington", new DateTime(1972, 8, 8),   "Blue",      66,       163 },
    { "Damon",   "Washington", new DateTime(1986, 2, 2),   "Hazel",     76,       176 },
    { "Angela",  "Washington", new DateTime(1993, 2, 15),  "Brown",     68,       145 }
};
worksheet.Range["A1:F7"].Value = data;
worksheet.Range["A:F"].ColumnWidth = 12;
worksheet.Tables.Add(worksheet.Range["A1:F7"], true);

var options = new PrintOutOptions();

// Set the name of the target printer.
options.ActivePrinter = "Gestetner MP C3503 PCL 6";

// Print 3 copies, collated (1-2-3, 1-2-3, 1-2-3).
options.Copies = 3;
options.Collate = true;

//  Print on double sides vertically.
options.Duplex = Duplex.Vertical;

// Print only pages 1 through 4.
options.From = 1;
options.To = 4;

// Use Tray 2 as the paper source.
// Use the standard .NET System.Drawing.Printing.PrinterSettings API to query available tray names for your printer.
options.PaperSourceName = "Tray 2";

// Print the workbook.
workbook.PrintOut(options);

// Save the workbook.
workbook.Save("printworkbook.xlsx");

Note:

Before setting PaperSourceName, you can query the available paper source names for a specific printer using the standard .NET System.Drawing.Printing.PrinterSettings API:

var settings = new System.Drawing.Printing.PrinterSettings();
settings.PrinterName = "Gestetner MP C3503 PCL 6";
Console.WriteLine("Available paper sources:");
foreach (System.Drawing.Printing.PaperSource ps in settings.PaperSources)
{
    Console.WriteLine($"  {ps.SourceName} (Kind: {ps.Kind})");
}