diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPrintSetup.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPrintSetup.java index 2078038c9..ecbbe8ecd 100644 --- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPrintSetup.java +++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFPrintSetup.java @@ -35,16 +35,23 @@ import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPageOrder; * Page setup and page margins settings for the worksheet. */ public class XSSFPrintSetup implements PrintSetup { - private CTWorksheet ctWorksheet; private CTPageSetup pageSetup; private CTPageMargins pageMargins; - protected XSSFPrintSetup(CTWorksheet worksheet) { this.ctWorksheet = worksheet; - this.pageSetup = ctWorksheet.getPageSetup() == null ? ctWorksheet.addNewPageSetup() : ctWorksheet.getPageSetup(); - this.pageMargins = ctWorksheet.getPageMargins() == null ? ctWorksheet.addNewPageMargins() : ctWorksheet.getPageMargins(); + + if(ctWorksheet.isSetPageSetup()) { + this.pageSetup = ctWorksheet.getPageSetup(); + } else { + this.pageSetup = ctWorksheet.addNewPageSetup(); + } + if(ctWorksheet.isSetPageMargins()) { + this.pageMargins = ctWorksheet.getPageMargins(); + } else { + this.pageMargins = ctWorksheet.addNewPageMargins(); + } } /** diff --git a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPrintSetup.java b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPrintSetup.java index c3cee3953..3001197cd 100644 --- a/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPrintSetup.java +++ b/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFPrintSetup.java @@ -24,13 +24,12 @@ import org.apache.poi.ss.usermodel.PaperSize; import org.apache.poi.ss.usermodel.PageOrder; import org.apache.poi.ss.usermodel.PrintOrientation; import org.apache.poi.ss.usermodel.PrintCellComments; +import org.apache.poi.xssf.XSSFITestDataProvider; /** * Tests for {@link XSSFPrintSetup} */ public class TestXSSFPrintSetup extends TestCase { - - public void testSetGetPaperSize() { CTWorksheet worksheet = CTWorksheet.Factory.newInstance(); CTPageSetup pSetup = worksheet.addNewPageSetup(); @@ -206,5 +205,47 @@ public class TestXSSFPrintSetup extends TestCase { printSetup.setCopies((short) 15); assertEquals(15, pSetup.getCopies()); } + + public void testSetSaveRead() throws Exception { + XSSFWorkbook wb = new XSSFWorkbook(); + XSSFSheet s1 = wb.createSheet(); + assertEquals(false, s1.getCTWorksheet().isSetPageSetup()); + assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); + + XSSFPrintSetup print = s1.getPrintSetup(); + assertEquals(true, s1.getCTWorksheet().isSetPageSetup()); + assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); + + print.setCopies((short)3); + print.setLandscape(true); + assertEquals(3, print.getCopies()); + assertEquals(true, print.getLandscape()); + + XSSFSheet s2 = wb.createSheet(); + assertEquals(false, s2.getCTWorksheet().isSetPageSetup()); + assertEquals(true, s2.getCTWorksheet().isSetPageMargins()); + + // Round trip and check + wb = XSSFITestDataProvider.instance.writeOutAndReadBack(wb); + + s1 = wb.getSheetAt(0); + s2 = wb.getSheetAt(1); + + assertEquals(true, s1.getCTWorksheet().isSetPageSetup()); + assertEquals(true, s1.getCTWorksheet().isSetPageMargins()); + assertEquals(false, s2.getCTWorksheet().isSetPageSetup()); + assertEquals(true, s2.getCTWorksheet().isSetPageMargins()); + + print = s1.getPrintSetup(); + assertEquals(3, print.getCopies()); + assertEquals(true, print.getLandscape()); + } + /** + * Open a file with print settings, save and check. + * Then, change, save, read, check + */ + public void testRoundTrip() { + // TODO + } }