Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Dec 14, 2023 6:33 pm

Hi. I've built a Windows Forms application that I'm using to print Excel documents with Spire.xls. The problem that I'm facing is that when I print any documents with the program I wrote, the entire document doesn't print on a single page. Instead, each document is slightly pushed to the right and downward a bit, which leads to the rightmost content being cut off on the page, as well as some of the bottom content in some cases depending on how far towards the bottom of the page the document goes.

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

aaden_619
 
Posts: 2
Joined: Thu Dec 14, 2023 6:13 pm

Fri Dec 15, 2023 5:43 am

Hi,

Thank you for your inquiry.
Did you use the latest version of Spire. Xls for .Net 13.12? If not, please use the latest version to test again. If the issue still exists, please provide your most representative or urgent test file for printing to help us do further investigation, you can attach here or send it to us via email ([email protected] ). Thank you in advance.

Sincerely,
Ula
E-iceblue support team
User avatar

Ula.wang
 
Posts: 282
Joined: Mon Aug 07, 2023 1:38 am

Fri Dec 15, 2023 6:40 pm

Ula.wang wrote:Hi,

Thank you for your inquiry.
Did you use the latest version of Spire. Xls for .Net 13.12? If not, please use the latest version to test again. If the issue still exists, please provide your most representative or urgent test file for printing to help us do further investigation, you can attach here or send it to us via email ([email protected] ). Thank you in advance.

Sincerely,
Ula
E-iceblue support team



Hi.

I'm using the free version of Spire.XLS, version 12.7.0. However, I assumed that if this doesn't work with the free version, it won't work for the commercial version either since the issue isn't a limitation on number of worksheets but rather just the fact that the entire page doesn't print out correctly.

I'm willing to give the commercial version a try to see if that's the issue, in which case I assume that I would need to install Spire.XLS Pack Version:13.12 rather than Spire.XLS for .NET Standard Edition Version:12.12? Please let me know if that's the case and also if I should be able to print correctly in the free version of Spire.XLS.

aaden_619
 
Posts: 2
Joined: Thu Dec 14, 2023 6:13 pm

Mon Dec 18, 2023 8:50 am

Hello,

Thanks for your feedback.
After investigation, I reproduced your issue (as show in the screenshot below) and logged it into our bug tracking system with the ticket number SPIREXLS-5048. Our development team will investigate and fix it. Once it is resolved, I will inform you in time. Sorry for the inconvenience caused.
Snipaste_2023-12-18_16-50-29.png
Snipaste_2023-12-18_16-50-29.png (640.69 KiB) Viewed 3849 times


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Wed Jan 24, 2024 6:34 am

Hello,

Thanks for your waitting.
For the issue with the number SPIREXLS-5048, I have some info to tell you:
If you want to set the margin and print area to take effect, you need to put these relevent code before initializing the PrintController object, due to when you get first PrintDocument object, the print setting are computed internally by Spire.Xls, the print setting doesn’t take effect after getting first PrintDocument object.
I put the complete code below for your reference.
Code: Select all
  // Create a Workbook object
            Workbook wb = new Workbook();
            // Load the Excel file from the specified path
            wb.LoadFromFile(@"../../data/AA1001_file_csv_qc.xlsx");

            // Get the first worksheet in the workbook
            Worksheet worksheet = wb.Worksheets[0];
            // Retrieve the page setup settings of the worksheet
            PageSetup pageSetup = worksheet.PageSetup;

            // Set the top, bottom, left, and right margins to zero
            pageSetup.TopMargin = 0;
            pageSetup.BottomMargin = 0;
            pageSetup.LeftMargin = 0;
            pageSetup.RightMargin = 0;

            // Set the print area to range C2:G49
            pageSetup.PrintArea = "B2:G49";
            // Set the page to fit the print area
            pageSetup.IsFitToPage = true;

            // Set the print controller to StandardPrintController
            wb.PrintDocument.PrintController = new StandardPrintController();
            // Retrieve the printer settings
            PrinterSettings settings = wb.PrintDocument.PrinterSettings;
            // Set duplex printing mode to simplex (single-sided)
            settings.Duplex = Duplex.Simplex;
            // Set the start page number to 0
            settings.FromPage = 0;
            // Set the end page number to the total number of worksheets
            settings.ToPage = wb.Worksheets.Count;
            // Execute the print job
            wb.PrintDocument.Print();


Sincerely
Abel
E-iceblue support team
User avatar

Abel.He
 
Posts: 1010
Joined: Tue Mar 08, 2022 2:02 am

Return to Spire.XLS