CenterVertically and CenterHorizontally options not working when used with OnePagePerSheet option

Hi,

I’m using Aspose.Cells 19.4.0.0 with my application. When I use CenterVertically and CenterHorizontally properies of pagesetup with OnePagePerSheet option as shown in sample code below, the saved PDF file do not seem honor CenterVertically and CenterHorizontally options. It always shows data in top left corner of the page in saved pdf.

        string dataDir = @"c:\temp\";
        string sourceFile = dataDir + "Book2.xlsx";
        Workbook workbook = new Workbook(sourceFile);
        WorksheetCollection worksheets = workbook.Worksheets;
        Worksheet worksheet = worksheets[0];

        // Get the pagesetup object
        PageSetup pageSetup = worksheet.PageSetup;

        // Specify Center on page Horizontally and Vertically
        pageSetup.CenterHorizontally = true;
        pageSetup.CenterVertically = true;

        Aspose.Cells.PdfSaveOptions pdfOptions = new Aspose.Cells.PdfSaveOptions();
        pdfOptions.OnePagePerSheet = true;
        pdfOptions.EmbedStandardWindowsFonts = true;

        pdfOptions.Compliance = Aspose.Cells.Rendering.PdfCompliance.None;
        workbook.Save(dataDir + "Book2_1.pdf", pdfOptions);

The API document says “If OnePagePerSheet is true , all content of one sheet will output to only one page in result. The paper size of pagesetup will be invalid, and the other settings of pagesetup will still take effect.”. But this doesn’t seem to be the case.

Can you tell us why this settings do not work when used together?

Thanks,
Hiren Patel

@hirenp,

Could you please zip your template Excel file and attach it here, we will check it soon.

I’ve attached test xlsx file zip.

Book2.zip (6.4 KB)

@hirenp,
I have tested this sample code with the latest version Aspose.Cells for .NET 20.11 but could not observe any issue as the text is displayed in the center of PDF as expected. Please give a try to this code using the latest version and share your feedback.
Book2.pdf (12.5 KB)

@hirenp,
We have investigated bit more. ‘CenterHorizontally’ or ‘CenterVertically’ is effective only when width/height of page content is smaller than the Paper size. When ‘OnePagePerSheet’ is set, paper size is ignored, so ‘CenterHorizontally’ or ‘CenterVertically’ is not effective in this case.

You just need to set the same value for LeftMargin and RightMargin, TopMargin and BottomMargin in ‘PageSetup’ when ‘OnePagePerSheet’ is set.

Actually, in your sample file ‘Book2.xlsx’, the value of LeftMargin and RightMargin are same, and the value of TopMargin and BottomMargin are also same.
The content in the output pdf file is already center aligned, to check it clearly, please also print gridlines(add code ‘pageSetup.PrintGridlines = true;’

@ahsaniqbalsidiqui,

Are you confirming that “CenterHorizontally” or “CenterVertically” options are only effective when page content is smaller than the Paper size and “OnePagePerSheet” option is not set?

@hirenp,
Yes, your understanding is right.

@hirenp,
You can use the following code to set same left/right, top/bottom margin when OnePagePerSheet is set:

...
// Get the pagesetup object
PageSetup pageSetup = worksheet.PageSetup;

//set same left/rigth, top/bottom margin
double hMargin = (pageSetup.LeftMargin + pageSetup.RightMargin) / 2;
pageSetup.LeftMargin = pageSetup.RightMargin = hMargin;

double vMargin = (pageSetup.TopMargin + pageSetup.BottomMargin) / 2;
pageSetup.TopMargin = pageSetup.BottomMargin = vMargin;

// Specify Center on page Horizontally and Vertically
pageSetup.CenterHorizontally = true;
pageSetup.CenterVertically = true;
...

Moreover, also note that even the content is smaller paper size, center on page only works on the area which minus left/top and top/bottom margin.

See attached screenshot: print center.png (65.5 KB)