We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Convert excel to PDF change the document paper size

Hello,
I have a template in excel whose print area I have set and size is Letter 8.511. It is dynamic excel template, based on user input the columns get hide or show. So, sometimes the output will be 40 columns , sometimes it will be less. At the end I am converting Excel to PDF but the paper size comes out to be very different, sometimes 19.520 etc.
My code to convert excel to pdf -

ConvertExcelToPDF(buffer, “Landscape”, “PaperLetter”, 1, 1, true, true);

    public byte[] ConvertExcelToPDF(byte[] ExcelOutput, string PageOrientation, string PaperSize, int FitToPagesTall, int FitToPagesWide, bool CenterHorizontally, bool CenterVertically)
    {
        PageOrientationType PageOrientationType;
        PaperSizeType PaperSizeType;
        MemoryStream PDFStream = new MemoryStream();
        MemoryStream ExcelStream = new MemoryStream(ExcelOutput);
        Workbook wb = new Workbook(ExcelStream);

        PageOrientationType = (PageOrientationType)Enum.Parse(typeof(PageOrientationType), PageOrientation);

        PaperSizeType = (PaperSizeType)Enum.Parse(typeof(PaperSizeType), PaperSize);

        foreach (Worksheet ws in wb.Worksheets)
        {
            ws.PageSetup.PaperSize = PaperSizeType;
            ws.PageSetup.Orientation = PageOrientationType;
            //ws.PageSetup.CenterHorizontally = CenterHorizontally;
            //ws.PageSetup.CenterVertically = CenterVertically;
            ws.PageSetup.FitToPagesTall = FitToPagesTall;
            ws.PageSetup.FitToPagesWide = FitToPagesWide;
            ws.PageSetup.RightMarginInch = 0.25;
            ws.PageSetup.TopMarginInch = 0.25;
            ws.PageSetup.LeftMarginInch = 0.25;
            ws.PageSetup.BottomMarginInch = 0.25;
            ws.PageSetup.HeaderMarginInch = 0.3;
            ws.PageSetup.FooterMarginInch = 0.3;
            ws.PageSetup.Zoom = 50;
            //ws.PageSetup.PrintArea = "Print_Area";
        }

        ImageSaveOptions imgSaveOption = new ImageSaveOptions();
        imgSaveOption.ImageOrPrintOptions.OnePagePerSheet = true;

        PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(SaveFormat.Pdf);
        pdfSaveOptions.OnePagePerSheet = true;
        pdfSaveOptions.DefaultFont = "Arial Narrow";
        pdfSaveOptions.PrintingPageType = PrintingPageType.Default;

        wb.CalculateFormula();
        wb.Save(PDFStream, pdfSaveOptions);

        return PDFStream.ToArray();
    } 

I have one more question, why after every time I convert excel to PDF, the font become bold for whole document. I have attached a message box once I generate PDF from Excel.

I would appreciate your help.

Thanks,
Sarthak

aspose convertion.jpg (28.5 KB)
pdf size.PNG (759 Bytes)

@spathak

For your Paper Size Problem:

It is because of the reason that OnePagePerSheet is set as true. If you want that the paper size is always of the same size, then do not set OnePagePerSheet and set it as false.

For your font issue in the generated PDF file:

Please download and try the following latest version and it should fix your issue. If you still face the issue, please provide us your source file and the generated PDF file. We will look into it and help you asap.

Thank you for your help. It is resolved now. I appreciate it.

@spathak

Thanks for your feedback and using Aspose.Cells.

It is good to know that your issue is resolved now. Let us know if you encounter any other issue, we will be glad to look into it and help you further.