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?
@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;’
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?