DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

Excel Print Setup (Code Example)

IronXL allows Print Setup to be programmatically defined in any Excel document. This gives granular control over how the document will be printed on a physical or PDF printer.

Document headers and footers can also be set, including the ability to "mail merge" variables such as page numbers.

C#:

using IronXL;
using IronXL.Printing;

WorkBook workbook = WorkBook.Load("test.xlsx");
WorkSheet worksheet = workbook.DefaultWorkSheet;
//This is how we set the print header of the worksheet
worksheet.Header.Center = "My document";
//This is how we can set the header margin
worksheet.PrintSetup.HeaderMargin = 2.33;
//This is how we set the size of the paper
//Paper size enum represents different sizes of paper
worksheet.PrintSetup.PaperSize = PaperSize.B4;
//This is how we can set the print orientation of the worksheet
worksheet.PrintSetup.PrintOrientation = PrintOrientation.Portrait;
//This is how we can set black and white printing
worksheet.PrintSetup.NoColor = true;

workbook.SaveAs("PrintSetup.xlsx");
Enter fullscreen mode Exit fullscreen mode

VB:

Imports IronXL
Imports IronXL.Printing

Private workbook As WorkBook = WorkBook.Load("test.xlsx")
Private worksheet As WorkSheet = workbook.DefaultWorkSheet
'This is how we set the print header of the worksheet
worksheet.Header.Center = "My document"
'This is how we can set the header margin
worksheet.PrintSetup.HeaderMargin = 2.33
'This is how we set the size of the paper
'Paper size enum represents different sizes of paper
worksheet.PrintSetup.PaperSize = PaperSize.B4
'This is how we can set the print orientation of the worksheet
worksheet.PrintSetup.PrintOrientation = PrintOrientation.Portrait
'This is how we can set black and white printing
worksheet.PrintSetup.NoColor = True

workbook.SaveAs("PrintSetup.xlsx")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)