Bug 63221 - Please add methods getTopMargin(), getBottomMargin(), etc, to XSSFSheet
Summary: Please add methods getTopMargin(), getBottomMargin(), etc, to XSSFSheet
Status: RESOLVED FIXED
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 4.0.x-dev
Hardware: All All
: P2 enhancement (vote)
Target Milestone: ---
Assignee: POI Developers List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-03-02 16:22 UTC by David Gauntt
Modified: 2019-03-10 16:15 UTC (History)
0 users



Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Gauntt 2019-03-02 16:22:24 UTC
The XSSFPrintSetup class contains the methods getHeaderMargin() and getFooterMargin().  However, Excel documents also contain top, bottom, left, and right margin settings, so the methods getTopMargin(), getBottomMargin(), etc, should be added.  It would also be useful to add these as methods of XSSFSheet.

The code fragments below can be used as a template.

class XSSFPrintSetup {
	public double getTopMargin() {
		return pageMargins.getTop();
	}
}

class XSSFSheet {
	public double getTopMargin() {
		final CTPageMargins  pageMargins;
		if(worksheet.isSetPageMargins()) {
			pageMargins = ctWorksheet.getPageMargins();
		} else {
			pageMargins = ctWorksheet.addNewPageMargins();
		}
		return pageMargins.getTop();
	}
}

This is based on the following workaround:

	public static double getTopMargin(XSSFSheet sheet) {
		final CTWorksheet ctSheet = sheet.getCTWorksheet();
		final CTPageMargins margins = ctSheet.getPageMargins();
		return margins.getTop();
	}
Comment 1 David Gauntt 2019-03-02 16:30:12 UTC
I have refined the workaround:

	public static double getTopMargin(XSSFSheet sheet) {
		return getPageMargins(sheet).getTop();
	}

	public static double getBottomMargin(XSSFSheet sheet) {
		return getPageMargins(sheet).getBottom();
	}


	private static CTPageMargins getPageMargins(XSSFSheet sheet) {
		final CTWorksheet ctSheet = sheet.getCTWorksheet();
		if (ctSheet.isSetPageMargins()) {
			return ctSheet.getPageMargins();
		} else {
			return ctSheet.addNewPageMargins();
		}
	}
Comment 2 Dominik Stadler 2019-03-10 16:15:36 UTC
Added methods to XSSFPrintSetup via r1855154, XSSFSheet already has setMargin/getMargin which receives a constant for the margin to set and thus this can already be used to set any of those margins.