We are using Aspose.Cells SDK to convert all excel documents to PDF. I now have a requirement to fit all the columns of an excel worksheet into 1 page prior to saving it as .pdf. How can I accomplish this? Please see the code snippet below-
if (fileExtension.ToLower().Contains(".xls"))
{
//Convert Excel doc to PDF
var workbook = new Aspose.Cells.Workbook(mStream);
workbook.Worksheets.RemoveAt("help_Overview");
if (workbook.Worksheets.Any())
{
var pdfStream = new MemoryStream();
workbook.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf);
BlobContainerClient container = new BlobContainerClient(_settings.CloudStorageConnection, "abc");
BlobClient blob = container.GetBlobClient($"/xyz/{newFilePath}");
pdfStream.Position = 0;
blob.Upload(pdfStream, true);
pdfStream.Close();
}
}
Any help is greatly appreciated. Thank you in advance.
Option 1: using PdfSaveOptions.AllColumnsInOnePagePerSheet, set it to true, all the columns of one sheet will be into one page. However, the width of paper size set in PageSetup will be ignored.
if (fileExtension.ToLower().Contains(".xls"))
{
//Convert Excel doc to PDF
var workbook = new Aspose.Cells.Workbook(mStream);
workbook.Worksheets.RemoveAt("help_Overview");
if (workbook.Worksheets.Any())
{
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.AllColumnsInOnePagePerSheet = true;
var pdfStream = new MemoryStream();
workbook.Save(pdfStream, saveOptions);
BlobContainerClient container = new BlobContainerClient(_settings.CloudStorageConnection, "abc");
BlobClient blob = container.GetBlobClient($"/xyz/{newFilePath}");
pdfStream.Position = 0;
blob.Upload(pdfStream, true);
pdfStream.Close();
}
}
Option 2: using PageSetup.SetFitToPages(1, 0), it will shrink content of one sheet to fit the width of paper size set in PageSetup. However, the minimun zoom is 10%, if a sheet contain too much columns with data, there is a possibility that columns will be split into more than one pages.
if (fileExtension.ToLower().Contains(".xls"))
{
//Convert Excel doc to PDF
var workbook = new Aspose.Cells.Workbook(mStream);
workbook.Worksheets.RemoveAt("help_Overview");
if (workbook.Worksheets.Any())
{
foreach(Worksheet sheet in workbook.Worksheets)
{
sheet.PageSetup.SetFitToPages(1, 0);
}
var pdfStream = new MemoryStream();
workbook.Save(pdfStream, Aspose.Cells.SaveFormat.Pdf);
BlobContainerClient container = new BlobContainerClient(_settings.CloudStorageConnection, "abc");
BlobClient blob = container.GetBlobClient($"/xyz/{newFilePath}");
pdfStream.Position = 0;
blob.Upload(pdfStream, true);
pdfStream.Close();
}
}
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.
Sets consent for personalized advertising.
Cookie Notice
To provide you with the best experience, we use cookies for personalization, analytics, and ads. By using our site, you agree to our cookie policy.
More info
Enables storage, such as cookies, related to analytics.
Enables storage, such as cookies, related to advertising.
Sets consent for sending user data to Google for online advertising purposes.