You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XSSFPrintSetup.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import org.apache.poi.ooxml.POIXMLException;
  17. import org.apache.poi.ss.usermodel.PageOrder;
  18. import org.apache.poi.ss.usermodel.PaperSize;
  19. import org.apache.poi.ss.usermodel.PrintCellComments;
  20. import org.apache.poi.ss.usermodel.PrintOrientation;
  21. import org.apache.poi.ss.usermodel.PrintSetup;
  22. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageMargins;
  23. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPageSetup;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTWorksheet;
  25. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STCellComments;
  26. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STOrientation;
  27. import org.openxmlformats.schemas.spreadsheetml.x2006.main.STPageOrder;
  28. /**
  29. * Page setup and page margins settings for the worksheet.
  30. */
  31. public class XSSFPrintSetup implements PrintSetup {
  32. private CTWorksheet ctWorksheet;
  33. private CTPageSetup pageSetup;
  34. private CTPageMargins pageMargins;
  35. protected XSSFPrintSetup(CTWorksheet worksheet) {
  36. this.ctWorksheet = worksheet;
  37. if(ctWorksheet.isSetPageSetup()) {
  38. this.pageSetup = ctWorksheet.getPageSetup();
  39. } else {
  40. this.pageSetup = ctWorksheet.addNewPageSetup();
  41. }
  42. if(ctWorksheet.isSetPageMargins()) {
  43. this.pageMargins = ctWorksheet.getPageMargins();
  44. } else {
  45. this.pageMargins = ctWorksheet.addNewPageMargins();
  46. }
  47. }
  48. /**
  49. * Set the paper size.
  50. *
  51. * @param size the paper size.
  52. */
  53. public void setPaperSize(short size) {
  54. pageSetup.setPaperSize(size);
  55. }
  56. /**
  57. * Set the paper size as enum value.
  58. *
  59. * @param size value for the paper size.
  60. */
  61. public void setPaperSize(PaperSize size) {
  62. setPaperSize((short) (size.ordinal() + 1));
  63. }
  64. /**
  65. * Set the scale.
  66. * Valid values range from 10 to 400.
  67. * This setting is overridden when fitToWidth and/or fitToHeight are in use
  68. *
  69. * @param scale the scale to use
  70. */
  71. public void setScale(short scale) {
  72. if (scale < 10 || scale > 400) throw new POIXMLException("Scale value not accepted: you must choose a value between 10 and 400.");
  73. pageSetup.setScale(scale);
  74. }
  75. /**
  76. * Set the page numbering start.
  77. * Page number for first printed page. If no value is specified, then 'automatic' is assumed.
  78. *
  79. * @param start the page numbering start
  80. */
  81. public void setPageStart(short start) {
  82. pageSetup.setFirstPageNumber(start);
  83. }
  84. /**
  85. * Set the number of pages wide to fit the sheet in
  86. *
  87. * @param width the number of pages
  88. */
  89. public void setFitWidth(short width) {
  90. pageSetup.setFitToWidth(width);
  91. }
  92. /**
  93. * Set the number of pages high to fit the sheet in
  94. *
  95. * @param height the number of pages
  96. */
  97. public void setFitHeight(short height) {
  98. pageSetup.setFitToHeight(height);
  99. }
  100. /**
  101. * Set whether to go left to right or top down in ordering
  102. *
  103. * @param leftToRight left to right
  104. */
  105. public void setLeftToRight(boolean leftToRight) {
  106. if (leftToRight)
  107. setPageOrder(PageOrder.OVER_THEN_DOWN);
  108. else
  109. setPageOrder(PageOrder.DOWN_THEN_OVER);
  110. }
  111. /**
  112. * Set whether to print in landscape
  113. *
  114. * @param ls landscape
  115. */
  116. public void setLandscape(boolean ls) {
  117. if (ls)
  118. setOrientation(PrintOrientation.LANDSCAPE);
  119. else
  120. setOrientation(PrintOrientation.PORTRAIT);
  121. }
  122. /**
  123. * Use the printer's defaults settings for page setup values and don't use the default values
  124. * specified in the schema. For example, if dpi is not present or specified in the XML, the
  125. * application shall not assume 600dpi as specified in the schema as a default and instead
  126. * shall let the printer specify the default dpi.
  127. *
  128. * @param valid Valid
  129. */
  130. public void setValidSettings(boolean valid) {
  131. pageSetup.setUsePrinterDefaults(valid);
  132. }
  133. /**
  134. * Set whether it is black and white
  135. *
  136. * @param mono Black and white
  137. */
  138. public void setNoColor(boolean mono) {
  139. pageSetup.setBlackAndWhite(mono);
  140. }
  141. /**
  142. * Set whether it is in draft mode
  143. *
  144. * @param d draft
  145. */
  146. public void setDraft(boolean d) {
  147. pageSetup.setDraft(d);
  148. }
  149. /**
  150. * Print the include notes
  151. *
  152. * @param printNotes print the notes
  153. */
  154. public void setNotes(boolean printNotes) {
  155. if (printNotes){
  156. pageSetup.setCellComments(STCellComments.AS_DISPLAYED);
  157. }
  158. }
  159. /**
  160. * Set no orientation.
  161. *
  162. * @param orientation Orientation.
  163. */
  164. public void setNoOrientation(boolean orientation) {
  165. if (orientation) {
  166. setOrientation(PrintOrientation.DEFAULT);
  167. }
  168. }
  169. /**
  170. * Set whether to use page start
  171. *
  172. * @param page Use page start
  173. */
  174. public void setUsePage(boolean page) {
  175. pageSetup.setUseFirstPageNumber(page);
  176. }
  177. /**
  178. * Sets the horizontal resolution.
  179. *
  180. * @param resolution horizontal resolution
  181. */
  182. public void setHResolution(short resolution) {
  183. pageSetup.setHorizontalDpi(resolution);
  184. }
  185. /**
  186. * Sets the vertical resolution.
  187. *
  188. * @param resolution vertical resolution
  189. */
  190. public void setVResolution(short resolution) {
  191. pageSetup.setVerticalDpi(resolution);
  192. }
  193. /**
  194. * Sets the header margin.
  195. *
  196. * @param headerMargin header margin
  197. */
  198. public void setHeaderMargin(double headerMargin) {
  199. pageMargins.setHeader(headerMargin);
  200. }
  201. /**
  202. * Sets the footer margin.
  203. *
  204. * @param footerMargin footer margin
  205. */
  206. public void setFooterMargin(double footerMargin) {
  207. pageMargins.setFooter(footerMargin);
  208. }
  209. /**
  210. * Sets the number of copies.
  211. *
  212. * @param copies number of copies
  213. */
  214. public void setCopies(short copies) {
  215. pageSetup.setCopies(copies);
  216. }
  217. /**
  218. * Orientation of the page: landscape - portrait.
  219. *
  220. * @param orientation - Orientation of the page
  221. * @see PrintOrientation
  222. */
  223. public void setOrientation(PrintOrientation orientation) {
  224. STOrientation.Enum v = STOrientation.Enum.forInt(orientation.getValue());
  225. pageSetup.setOrientation(v);
  226. }
  227. /**
  228. * Orientation of the page: landscape - portrait.
  229. *
  230. * @return Orientation of the page
  231. * @see PrintOrientation
  232. */
  233. public PrintOrientation getOrientation() {
  234. STOrientation.Enum val = pageSetup.getOrientation();
  235. return val == null ? PrintOrientation.DEFAULT : PrintOrientation.valueOf(val.intValue());
  236. }
  237. public PrintCellComments getCellComment() {
  238. STCellComments.Enum val = pageSetup.getCellComments();
  239. return val == null ? PrintCellComments.NONE : PrintCellComments.valueOf(val.intValue());
  240. }
  241. /**
  242. * Set print page order.
  243. *
  244. * @param pageOrder An enum indicating the wanted ordering of printed pages
  245. */
  246. public void setPageOrder(PageOrder pageOrder) {
  247. STPageOrder.Enum v = STPageOrder.Enum.forInt(pageOrder.getValue());
  248. pageSetup.setPageOrder(v);
  249. }
  250. /**
  251. * get print page order.
  252. *
  253. * @return The currently set ordering of printed pages
  254. */
  255. public PageOrder getPageOrder() {
  256. return (pageSetup.getPageOrder() == null) ? null : PageOrder.valueOf(pageSetup.getPageOrder().intValue());
  257. }
  258. /**
  259. * Returns the paper size.
  260. *
  261. * @return short - paper size
  262. */
  263. public short getPaperSize() {
  264. return (short) pageSetup.getPaperSize();
  265. }
  266. /**
  267. * Returns the paper size as enum.
  268. *
  269. * @return PaperSize paper size
  270. * @see PaperSize
  271. */
  272. public PaperSize getPaperSizeEnum() {
  273. return PaperSize.values()[getPaperSize() - 1];
  274. }
  275. /**
  276. * Returns the scale.
  277. *
  278. * @return short - scale
  279. */
  280. public short getScale() {
  281. return (short) pageSetup.getScale();
  282. }
  283. /**
  284. * Set the page numbering start.
  285. * Page number for first printed page. If no value is specified, then 'automatic' is assumed.
  286. *
  287. * @return page number for first printed page
  288. */
  289. public short getPageStart() {
  290. return (short) pageSetup.getFirstPageNumber();
  291. }
  292. /**
  293. * Returns the number of pages wide to fit sheet in.
  294. *
  295. * @return number of pages wide to fit sheet in
  296. */
  297. public short getFitWidth() {
  298. return (short) pageSetup.getFitToWidth();
  299. }
  300. /**
  301. * Returns the number of pages high to fit the sheet in.
  302. *
  303. * @return number of pages high to fit the sheet in
  304. */
  305. public short getFitHeight() {
  306. return (short) pageSetup.getFitToHeight();
  307. }
  308. /**
  309. * Returns the left to right print order.
  310. *
  311. * @return left to right print order
  312. */
  313. public boolean getLeftToRight() {
  314. return getPageOrder() == PageOrder.OVER_THEN_DOWN;
  315. }
  316. /**
  317. * Returns the landscape mode.
  318. *
  319. * @return landscape mode
  320. */
  321. public boolean getLandscape() {
  322. return getOrientation() == PrintOrientation.LANDSCAPE;
  323. }
  324. /**
  325. * Use the printer's defaults settings for page setup values and don't use the default values
  326. * specified in the schema. For example, if dpi is not present or specified in the XML, the
  327. * application shall not assume 600dpi as specified in the schema as a default and instead
  328. * shall let the printer specify the default dpi.
  329. *
  330. * @return valid settings
  331. */
  332. public boolean getValidSettings() {
  333. return pageSetup.getUsePrinterDefaults();
  334. }
  335. /**
  336. * Returns the black and white setting.
  337. *
  338. * @return black and white setting
  339. */
  340. public boolean getNoColor() {
  341. return pageSetup.getBlackAndWhite();
  342. }
  343. /**
  344. * Returns the draft mode.
  345. *
  346. * @return draft mode
  347. */
  348. public boolean getDraft() {
  349. return pageSetup.getDraft();
  350. }
  351. /**
  352. * Returns the print notes.
  353. *
  354. * @return print notes
  355. */
  356. public boolean getNotes() {
  357. return getCellComment() == PrintCellComments.AS_DISPLAYED;
  358. }
  359. /**
  360. * Returns the no orientation.
  361. *
  362. * @return no orientation
  363. */
  364. public boolean getNoOrientation() {
  365. return getOrientation() == PrintOrientation.DEFAULT;
  366. }
  367. /**
  368. * Returns the use page numbers.
  369. *
  370. * @return use page numbers
  371. */
  372. public boolean getUsePage() {
  373. return pageSetup.getUseFirstPageNumber();
  374. }
  375. /**
  376. * Returns the horizontal resolution.
  377. *
  378. * @return horizontal resolution
  379. */
  380. public short getHResolution() {
  381. return (short) pageSetup.getHorizontalDpi();
  382. }
  383. /**
  384. * Returns the vertical resolution.
  385. *
  386. * @return vertical resolution
  387. */
  388. public short getVResolution() {
  389. return (short) pageSetup.getVerticalDpi();
  390. }
  391. /**
  392. * Returns the header margin.
  393. *
  394. * @return header margin
  395. */
  396. public double getHeaderMargin() {
  397. return pageMargins.getHeader();
  398. }
  399. /**
  400. * Returns the footer margin.
  401. *
  402. * @return footer margin
  403. */
  404. public double getFooterMargin() {
  405. return pageMargins.getFooter();
  406. }
  407. /**
  408. * Returns the number of copies.
  409. *
  410. * @return number of copies
  411. */
  412. public short getCopies() {
  413. return (short) pageSetup.getCopies();
  414. }
  415. /**
  416. * Sets the top margin.
  417. *
  418. * @param topMargin top margin
  419. */
  420. public void setTopMargin(double topMargin) {
  421. pageMargins.setTop(topMargin);
  422. }
  423. /**
  424. * Returns the top margin.
  425. *
  426. * @return top margin
  427. */
  428. public double getTopMargin() {
  429. return pageMargins.getTop();
  430. }
  431. /**
  432. * Sets the bottom margin.
  433. *
  434. * @param bottomMargin bottom margin
  435. */
  436. public void setBottomMargin(double bottomMargin) {
  437. pageMargins.setBottom(bottomMargin);
  438. }
  439. /**
  440. * Returns the bottom margin.
  441. *
  442. * @return bottom margin
  443. */
  444. public double getBottomMargin() {
  445. return pageMargins.getBottom();
  446. }
  447. /**
  448. * Sets the left margin.
  449. *
  450. * @param leftMargin left margin
  451. */
  452. public void setLeftMargin(double leftMargin) {
  453. pageMargins.setLeft(leftMargin);
  454. }
  455. /**
  456. * Returns the left margin.
  457. *
  458. * @return left margin
  459. */
  460. public double getLeftMargin() {
  461. return pageMargins.getLeft();
  462. }
  463. /**
  464. * Sets the right margin.
  465. *
  466. * @param rightMargin right margin
  467. */
  468. public void setRightMargin(double rightMargin) {
  469. pageMargins.setRight(rightMargin);
  470. }
  471. /**
  472. * Returns the right margin.
  473. *
  474. * @return right margin
  475. */
  476. public double getRightMargin() {
  477. return pageMargins.getRight();
  478. }
  479. }