we are trying to create a quick preview of xlsx files in an web application. We use dotnet 6.0 with the Syncfusion.XlsIORenderer.Net.Core 18.104.22.168 package.
To solve this problem we render a selected worksheet of the Excel file to a PDF on the server side. Since we do not need the PDF for printing but for reading the content, we do not want to create the PDF in A4 but in the exact size of the Excel worksheet. Is there a way to minimize the size of the PDF to the exact area so that we do not create empty areas?
We already tried to use the CustomPaperSize XlsIORendererSettings, but it seems that this method won't work for larger Excel files. Sometimes the font size seems also way too small, so we actually would need to "zoom" converted PDF?
Is there any easy way of rendering only the filled part of a Excel worksheet to a PDF file?
By default, Excel to PDF conversion considers the page settings set in the worksheet page setup. If you wish to ignore the empty spaces, you can set the margins to 0 using the following code snippet.
workbook.Worksheets.PageSetup.TopMargin = 0;
workbook.Worksheets.PageSetup.BottomMargin = 0;
workbook.Worksheets.PageSetup.LeftMargin = 0;
workbook.Worksheets.PageSetup.RightMargin = 0;
workbook.Worksheets.PageSetup.HeaderMargin = 0;
workbook.Worksheets.PageSetup.FooterMargin = 0;
Kindly try this suggestion and let us know if this helps.
thanks for the fast reply!
That helps a little but we still want to render the worksheet on a single pdf without page breaks. Is there any way to do this?
Jan, your requirement of rendering the entire worksheet into a single PDF page can be achieved through FitSheetOnOnePage option of Excel to PDF converter settings. The respective code snippet is documented under ASP.NET Core tab in the below link.
Hi, the problem is that it will always render on an A4 page. If the worksheet is small, the cells rendered way to big and if the worksheet is very big > 200 columns it will fit on an A4 page and you cannot read anything on the pdf untill you
Jan, it is the behavior of Microsoft Excel to print the Excel worksheet in single PDF page using FitSheetOnOnePage, irrespective of the number of columns. Instead, you can modify the Excel paper size through PaperSize property of IPageSetup.
Please find the code snippet below.
//Sets the required paper size
workbook.Worksheets.PageSetup.PaperSize = ExcelPaperSize.A2Paper;
Please go through the below links.