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 Jul 03, 2025 8:28 pm

P


The PdfDocument generated using the code below, send the last column of the Excel spreadsheet to another page in the PDF file

PdfDocument pdf = ExcelToPDF(fullPathName)
pdf.SaveToFile(newFullPathName, Spire.Pdf.FileFormat.PDF);

method:
public PdfDocument ExcelToPDF(string fullPathName)
{
if (String.IsNullOrWhiteSpace(fullPathName)) { throw new ArgumentException("No source file name provided"); }
string ext = Path.GetExtension(fullPathName).ToLower();
if (String.IsNullOrWhiteSpace(ext)) { throw new ArgumentException("Source file has no extension"); }
if (!File.Exists(fullPathName)) { throw new IOException("Source file does not exist"); }
if (!FileSystemHelper.ExcelExtensions.Contains(ext)) { throw new ArgumentException("No Excel file provided"); }

PdfDocument newPdf = new PdfDocument();
MemoryStream ms = new MemoryStream();
using (Spire.Xls.Workbook excelFile = new Workbook())
{
excelFile.LoadFromFile(fullPathName);
excelFile.SaveToStream(ms, Spire.Xls.FileFormat.PDF);
}
newPdf.LoadFromStream(ms);
return newPdf;
}

I had to add the following line to get it to work properly with the provided file adding the lines:

Spire.Xls.PageSetup pageSetup;
pageSetup=excelFile.Worjsheets[0].PageSetup;
pageSetup.FitToPageWide=1;
pageSetup.FitToPageTall=0;

INFO:
Spire.PDF for .NET version 11.6.14
Spire.XLS for .NET version 15.6.6

Winforms Application, net framework 4.8.1
Windows 11, 64 bit
Attachments
Excel issue.zip
(290.74 KiB) Downloaded 222 times

alej2020
 
Posts: 6
Joined: Mon Jan 27, 2025 2:34 pm

Fri Jul 04, 2025 6:18 am

Hello,

The issue you encountered, where the last column appears on a separate page, is caused by the total width of your original Excel file exceeding the default width of the PDF page. The method you used is to set the number of pages that the sheet will scale to in both width and height directions, thereby achieving the effect of adapting to the page width.

You can use the following simpler method to automatically adjust the Excel table to fit the PDF page width:

Code: Select all
excelFile.ConverterSetting.SheetFitToWidth = true;
(Adjust width to fit the page)
Sincerely,
Talia
E-iceblue support team
User avatar

talia.liu
 
Posts: 331
Joined: Mon Apr 14, 2025 3:33 am

Return to Spire.XLS