Excel-to-PDF: Document is full size in excel- but only takes up a small part of the PDF

I have an excel document that I want to convert to PDF. The document prints out as 1 page in excel, I'd like for it to do the same once converted. The content ends up taking up only a quarter of the first page of the PDF. What am I doing wrong?

Here's the code I use:

Workbook wb1 = new Workbook();

wb1.Open("FILE.XLS");

MemoryStream stream = new MemoryStream();

wb1.Save(stream, FileFormatType.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.PageSetup.PageHeight = PageSize.A4Height;

pdf.PageSetup.PageWidth = PageSize.A4Width;

pdf.DestinationType = DestinationType.FitPage;

pdf.BindXML(xmlDoc, null);

pdf.Save("Test.PDF", Aspose.Pdf.SaveType.OpenInBrowser,Response);

I've attached the resulting PDF.

Hi,

Thanks for considering Aspose.

While converting Excel file to Pdf format there are some limitations: Images, Drawing Object and charts etc. are lost while converting to pdf format.

I tried the following test with sample code, it 's ok.

Workbook wb = new Workbook();

wb.Open(“d:\bookpdf.xls”);

wb.Save(“d:\bookpdf.xml”,FileFormatType.AsposePdf);

Pdf pdf = new Pdf();

pdf.BindXML(“d:\bookpdf.xml”,null);

pdf.Save(“d:\bookpdf.pdf”);

Did you check xml file, is it completely documented or something missing?

For Reference please see:

Convert Excel Workbook to PDF

Regards

Amjad Sahi

Aspose Nanjing Team

All the document elements come over they're just small. All those boxes are populated in code before it converts the document.

Is there some reason I can't use a stream like you can with word? I want to avoid saving files an xml file just to convert it again.

I've attached the actual Excel document.

You can save it to stream without any problem.

I run the following code and the output pdf file is different with yours. I attach the pdf file here.

Workbook wb1 = new Workbook();

wb1.Open("d:\\test\\FM_M2K-003.xls");

MemoryStream stream = new MemoryStream();

wb1.Save(stream, FileFormatType.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;

pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;

pdf.DestinationType = Aspose.Pdf.DestinationType.FitPage;

pdf.BindXML(xmlDoc, null);

pdf.Save("d:\\test\\Test.PDF");

This is my latest version of Aspose.Cells and I use Aspose.Pdf v2.9.5.

I tried that version of Cells, and the latest posted. I’ve also tried the last PDF with the same results. Have you tried the document that I attacked in my last post?

I rolled back PDF to 2.9.5.0 and I still get the same results. I’ve tried other excel documents with random data and some come out fine, other do not. Am I missing a setting somewhere for the page size from excel to xml (stream)?

Hi,

Thanks for considering Aspose.

Well, We did test your xls file and convert it to pdf file (test.pdf) with your code and it is perfect as you can see the attached file.

It looks very odd as you are finding same results.

We sort out the things and response you soon.

Regards

Amjad Sahi

Aspose Nanjing Team

Have you tried my attached Aspose.Cells? I used the following test code with your Excel file:

Workbook wb1 = new Workbook();

wb1.Open("d:\\test\\FM_M2K-003.xls");

MemoryStream stream = new MemoryStream();

wb1.Save(stream, FileFormatType.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;

pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;

pdf.DestinationType = Aspose.Pdf.DestinationType.FitPage;

pdf.BindXML(xmlDoc, null);

pdf.Save("d:\\test\\Test.PDF");

By the way, while converting to xls file to pdf file, please set paper size to A4 for all worksheets. Aspose.Cells now only supports A4 size in this conversion.

By the way, while converting to xls file to pdf file, please set paper
size to A4 for
> all worksheets. Aspose.Cells now only supports A4 size
in this conversion.

Is there any timeline on adding support for legal, tabloid and letter in this scenario? We need to convert from xls to pdf and would like to be able to choose sizes other than A4.

Thanks,

- Al

We will support these paper size in the future version but don't have a specific plan yet.

Laurence:

Have you tried my attached Aspose.Cells? I used the following test code with your Excel file:

Workbook wb1 = new Workbook();

wb1.Open("d:\\test\\FM_M2K-003.xls");

MemoryStream stream = new MemoryStream();

wb1.Save(stream, FileFormatType.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;

pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;

pdf.DestinationType = Aspose.Pdf.DestinationType.FitPage;

pdf.BindXML(xmlDoc, null);

pdf.Save("d:\\test\\Test.PDF");

By the way, while converting to xls file to pdf file, please set paper size to A4 for all worksheets. Aspose.Cells now only supports A4 size in this conversion.

I used this code exactly with your posted Cells. I made a new C# app added a button and pasted that chunk in there. Set the reference to the file you posted and I get the same thing as originally posted. I also tried ASP.NET (which is what I plan to use this code with).

Could you please post your project here?

Attached is my output file. Please check it.

Technically that isn’t right either. This document is 1 page, your’s came out too wide and pushed it to a second page. Mines too small, yours is too big!

1. This xls file is not 1 page. When you choose "Print Preview", you can see it's 2x2 page. I attach the screenshot here.

2. I run your program and get the same result. Please check the attached pdf file.

Are you running the program on winxp, win2003 or windows vista?

I was finally able to duplicate your results. Page setup in excel was set to fit page 1 x 1. I changed it back to 100% scaling and I get the same results you do. Now my next problem is making it fit on one page. The software seems to have some support for it since my previous results are right content at 1/4 of the page size. Any ideas?

You can change the scaling to 80% to make it fit on one page. Following is the sample code:

License lic = new License();
lic.SetLicense("d:\\Aspose.custom.lic");

Workbook wb1 = new Workbook();

wb1.Open("d:\\test\\FM_M2K-003.xls");

MemoryStream stream = new MemoryStream();

wb1.Worksheets[0].PageSetup.Zoom = 80;

wb1.Save(stream, FileFormatType.AsposePdf);

stream.Seek(0, SeekOrigin.Begin);

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(stream);

Aspose.Pdf.Pdf pdf = new Aspose.Pdf.Pdf();

pdf.PageSetup.PageHeight = Aspose.Pdf.PageSize.A4Height;

pdf.PageSetup.PageWidth = Aspose.Pdf.PageSize.A4Width;

pdf.DestinationType = Aspose.Pdf.DestinationType.FitPage;

pdf.BindXML(xmlDoc, null);

pdf.Save("d:\\test\\Test.PDF");

Hi,

I have converted *.xls to .pdf by using Aspose.cells & Aspose.pdf.

So can we convert *.xlsx to .pdf?

Thanks,

Neelesh Katiyar

Hi Neelesh,

Yes, you can convert an .xlsx file to .pdf.

Thank you.

Hi Amjad,

First truely thanks for reply.

i have tried with it .xlsx with same C# code it has converted .xls file successfully but it doesn't convert .xlsx. it throws exception "This file's format is not supported or you don't specify a correct format.".

And i am facing some problem

1.How i convert all the sheets of excel file.

2. Size of file is increased after converting it into pdf.

Thanks,

Neelesh