We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Convert xlsx to pdf with reasonable size instead of A4

Hi,

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 20.4.0.38 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?

Greetings

Jan


5 Replies

KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 4, 2023 03:09 PM UTC

Hi Jan,


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[0].PageSetup.TopMargin = 0;

workbook.Worksheets[0].PageSetup.BottomMargin = 0;

workbook.Worksheets[0].PageSetup.LeftMargin = 0;

workbook.Worksheets[0].PageSetup.RightMargin = 0;

workbook.Worksheets[0].PageSetup.HeaderMargin = 0;

workbook.Worksheets[0].PageSetup.FooterMargin = 0;


Kindly try this suggestion and let us know if this helps.


Regards,

Keerthi.



JS Jan Schlacher January 29, 2023 05:32 PM UTC

Hi,


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



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 30, 2023 09:08 AM UTC

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.


https://help.syncfusion.com/file-formats/xlsio/excel-to-pdf-converter-settings?cs-save-lang=1&cs-lang=csharp#fit-sheet-on-one-page



JS Jan Schlacher January 30, 2023 12:21 PM UTC

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 



KK Konduru Keerthi Konduru Ravichandra Raju Syncfusion Team January 31, 2023 01:29 PM UTC

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[0].PageSetup.PaperSize = ExcelPaperSize.A2Paper;


Please go through the below links.

https://help.syncfusion.com/cr/file-formats/Syncfusion.XlsIO.Interfaces.IPageSetupBase.html#Syncfusion_XlsIO_Interfaces_IPageSetupBase_PaperSize

https://help.syncfusion.com/cr/file-formats/Syncfusion.XlsIO.ExcelPaperSize.html


Loader.
Live Chat Icon For mobile
Up arrow icon