Excel to PDF:The page content is not centered

When I use the following code to convert, the Excel content is displayed on the left in the PDF, but I want it to be displayed in the center. How can I solve this problem?
Actual effect:
image.png (15.9 KB)
Expected results:
image.png (22.8 KB)

Workbook wbk = null;
        try {
            wbk = new Workbook(srcFile);
            Worksheet sheet = null;
            int count = wbk.getWorksheets().getCount();
            for (int i = 0; i < count; i++) {
                sheet = wbk.getWorksheets().get(i);

                String sheetName = sheet.getName();
                Range usedRange =sheet.getCells().getMaxDisplayRange();
                if(usedRange == null)
                    continue;
                int maxRow = sheet.getCells().getMaxDataRow();
                int maxColumn = sheet.getCells().getMaxDataColumn();
                double width = 0;
                for (int col = 0; col <=maxColumn; col++) {
                    width += sheet.getCells().getColumnWidthInch(col);
                }
                double height = 0;
                for (int row = 0; row <= maxRow; row++) {
                    height += sheet.getCells().getRowHeightInch(row);
                }
                PageSetup pageSetup = sheet.getPageSetup();

                pageSetup.setBottomMargin(sheet.getPageSetup().getBottomMargin());
                pageSetup.setTopMargin(sheet.getPageSetup().getTopMargin());
                pageSetup.setLeftMargin(sheet.getPageSetup().getLeftMargin());
                pageSetup.setRightMargin(sheet.getPageSetup().getRightMargin());
pageSetup.setFooter(0, "&L" + sheetName);
                    pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
                    pageSetup.setFitToPagesWide(1);
                    pageSetup.setFitToPagesTall(1);
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK);
wbk.save(dstFile, saveOptions);

TEST.7z (28.0 KB)

@serendipity.zhq

To center the content of your Excel sheet when converting it to PDF, you need to adjust the page setup margins and ensure that the content fits within the page. Here’s how you can modify your existing code:

  1. Set the left and right margins to equal values to center the content.
  2. Ensure that the FitToPagesWide and FitToPagesTall options are set correctly.

Here’s an updated version of your code snippet:

Workbook wbk = null;
try {
    wbk = new Workbook(srcFile);
    Worksheet sheet = null;
    int count = wbk.getWorksheets().getCount();
    for (int i = 0; i < count; i++) {
        sheet = wbk.getWorksheets().get(i);
        PageSetup pageSetup = sheet.getPageSetup();

        // Set equal left and right margins to center the content
        double margin = 0.5; // Adjust this value as needed
        pageSetup.setLeftMargin(margin);
        pageSetup.setRightMargin(margin);

        // Set other margins if necessary
        pageSetup.setTopMargin(sheet.getPageSetup().getTopMargin());
        pageSetup.setBottomMargin(sheet.getPageSetup().getBottomMargin());
        
        pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
        pageSetup.setFitToPagesWide(1);
        pageSetup.setFitToPagesTall(1);
        
        PdfSaveOptions saveOptions = new PdfSaveOptions();
        saveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK);
        wbk.save(dstFile, saveOptions);
    }
} catch (Exception e) {
    e.printStackTrace();
}

In this code, the left and right margins are set to the same value, which helps in centering the content on the PDF page. You can adjust the margin variable to fit your specific needs.

If you continue to experience issues, consider checking the content width and ensuring it fits within the page dimensions.

The effect I wanted was not achieved, and the font size of the document content was reduced.

@serendipity.zhq
By testing on the latest version v25.10 using the sample file and the following code, we can obtain the correct results. Please refer to the attachment. out_java.pdf (35.1 KB)

Workbook wbk = new Workbook(filePath + "JL1-01.xlsx");
Worksheet sheet = null;
int count = wbk.getWorksheets().getCount();
for (int i = 0; i < count; i++) 
{
    sheet = wbk.getWorksheets().get(i);

    String sheetName = sheet.getName();
    Range usedRange =sheet.getCells().getMaxDisplayRange();
    if(usedRange == null)
        continue;
    int maxRow = sheet.getCells().getMaxDataRow();
    int maxColumn = sheet.getCells().getMaxDataColumn();
    double width = 0;
    for (int col = 0; col <=maxColumn; col++) 
    {
        width += sheet.getCells().getColumnWidthInch(col);
    }
    double height = 0;
    for (int row = 0; row <= maxRow; row++) 
    {
        height += sheet.getCells().getRowHeightInch(row);
    }
    PageSetup pageSetup = sheet.getPageSetup();

    pageSetup.setBottomMargin(sheet.getPageSetup().getBottomMargin());
    pageSetup.setTopMargin(sheet.getPageSetup().getTopMargin());
    pageSetup.setLeftMargin(sheet.getPageSetup().getLeftMargin());
    pageSetup.setRightMargin(sheet.getPageSetup().getRightMargin());
    pageSetup.setFooter(0, "&L" + sheetName);
    pageSetup.setPaperSize(PaperSizeType.PAPER_A_4);
    pageSetup.setFitToPagesWide(1);
    pageSetup.setFitToPagesTall(1);
}
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setPrintingPageType(PrintingPageType.IGNORE_BLANK);
wbk.save(filePath + "out_java.pdf", saveOptions);

We recommend you to kindly try using our latest version: Aspose.Cells for Java 25.10.

If you still find the issue, kindly do share your complete sample (runnable) code to reproduce the issue on our end, we will check it soon.

After my comparison, I found that the following code caused the phenomenon in the picture
pageSetup.setPrintArea("");

@serendipity.zhq
Thank you for your feedback. I’m glad you found the cause of the error phenomenon. Please comment out the relevant code, you will get the correct result. If you have any questions, please feel free to contact us at any time.