Workbook workbook = new Workbook(filepath);
//Validate in case file is corrupted. Normally excel file should have at least 1 sheet
if (workbook.Worksheets.Any())
{
var worksheet = workbook.Worksheets[0];
worksheet.AutoFitRows();
//Make all worksheet visible false except for first one for pdf conversion
// If there is any value in workSheetArray mark thse worksheet indexes as visible
//Item.Index starts at 0 so must add +1 to comapare with workSheetArray values
foreach (Worksheet item in workbook.Worksheets)
{
item.IsVisible = workSheetArray==null || !workSheetArray.Any()? item.Index == 0: workSheetArray.Contains((item.Index + 1).ToString());
}
}
var stream = workbook.SaveToStream();
workbook.Save(convertedPdfTempFile, SaveFormat.Pdf);
I tried using the following sample code with your provided Excel file using our latest available version, Aspose.Cells for .NET v22.2 (please try it) and it works absolutely fine. Please find attached (below) the output PDF file.
e.g. Sample code:
Workbook workbook = new Workbook("e:\\test2\\test1.xlsx");
foreach (Worksheet item in workbook.Worksheets)
{
item.AutoFitRows();
}
workbook.Save("e:\\test2\\out1.pdf", SaveFormat.Pdf);
When I call SaveToStream first and later call Save method , PDF alignment shows some issues .
var stream = workbook.SaveToStream();
workbook.Save(convertedPdfTempFile, SaveFormat.Pdf);
When I call Save first and then call SaveToStream, the PDF alignment looks good.
workbook.Save(convertedPdfTempFile, SaveFormat.Pdf);
var stream = workbook.SaveToStream();
The method workbook.SaveToStream() saves workbook to xls(Excel 2003) format, however your source file is xlsx(Excel 2007) format. Some default data is changed during this process. For your case, the paper size is changed from Letter to A4.
Could you please share more detail that why var stream = workbook.SaveToStream();(It seems that the result stream is not used in your code snippet.) is called before saving to pdf?
SaveToStream method saves the file in XLS (older) file format, you cannot save to XLSX file format using this method. To save to newer Excel (XLSX) file format, you may use Workbook.Save (Stream, SaveOptions) overloaded method.