Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save Excel File as PDF and fit to page

Tags:

c#

excel

pdf

i have a task to create function to export Excel file into PDF with requirements that all columns must fit in 1 page .

This is the code i use to convert excel file to pdf.

xlApp = new Excel.Application();
xlWb = xlApp.Workbooks.Open(textBox3.Text);
xlWb.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF, Path.GetDirectoryName(textBox3.Text)+ "\\" + Path.GetFileNameWithoutExtension(textBox3.Text) + ".pdf");

But when i open it , it appears that the columns is separated, like if i have like 10 columns , it will split like the first 6 columns in the first page and the last 4 columns in the second page.

How to make it fit to page? it is okay if the potrait is landscape too

like image 660
trytocode Avatar asked Jan 23 '26 06:01

trytocode


1 Answers

so after i do some research , this is the way to setup your page orientation , and you can fit to page

//setup your page orientation + fit to pages
    xlWs2.PageSetup.Orientation = Excel.XlPageOrientation.xlLandscape;
    xlWs2.PageSetup.Zoom = false;
    xlWs2.PageSetup.FitToPagesWide = 1;
    xlWs2.PageSetup.FitToPagesTall = false;
//this is how to save as PDF     
xlWb2.ExportAsFixedFormat(Excel.XlFixedFormatType.xlTypePDF,Path.GetDirectoryName(textBox3.Text) + "\\" + Path.GetFileNameWithoutExtension(textBox3.Text) + ".pdf");

P.S :xlWs2 is excel.Worksheets, xlWb2 is excel.Workbooks

like image 126
trytocode Avatar answered Jan 24 '26 22:01

trytocode



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!