DEV Community

IronSoftware
IronSoftware

Posted on • Originally published at ironsoftware.com

1 1

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

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay