This WFA has been built on .NET 5.0 using C#. Keep in mind that if I open the Excel document and attempt to print normally (via Excel) the page prints out just fine. I've added my code below:
- Code: Select all
// Create a Workbook object
Workbook wb = new Workbook();
wb.LoadFromFile(currentFileName);
//Set the print controller to StandardPrintController, which will prevent print process from showing
wb.PrintDocument.PrintController = new StandardPrintController();
//Get PrinterSettings from the workbook
PrinterSettings settings = wb.PrintDocument.PrinterSettings;
settings.PrinterName = @"\\placeholder_for_printer_name";
settings.Duplex = Duplex.Simplex;
settings.FromPage = 0;
settings.ToPage = wb.Worksheets.Count;
/* This has been commented out because it didn't work
// Set page size
float width = (float) 215.9;
float height = (float) 279.4;
wb.Worksheets[0].PageSetup.SetCustomPaperSize(width, height);
*/
//Get the first worksheet
Worksheet worksheet = wb.Worksheets[0];
//Get the PageSetup object of the first worksheet
PageSetup pageSetup = worksheet.PageSetup;
//Set page margins
pageSetup.TopMargin = 0;
pageSetup.BottomMargin = 0;
pageSetup.LeftMargin = 0;
pageSetup.RightMargin = 0;
//Specify print area (even specifying the print area didn't work)
pageSetup.PrintArea = "B2:G49";
//Fit worksheet on one page
pageSetup.IsFitToPage = true;
try
{
//Print the workbook
wb.PrintDocument.Print();
}
// Catch block is after this