Skip to content

Commit

Permalink
Merge pull request #217 from apptaro/paperSize
Browse files Browse the repository at this point in the history
support for Worksheet.setPaperSize
  • Loading branch information
Olivier Chédru committed Oct 12, 2022
2 parents e44d230 + 220e47e commit 4a483c5
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,9 @@ To keep the sheet in active tab:
```java
ws.keepInActiveTab();
```
Set page orientation (visible in print preview mode):
Set paper size and page orientation (visible in print preview mode):
```java
ws.paperSize(PaperSize.A4_PAPER);
ws.pageOrientation("landscape");
```
Set bottom, top, left, or right margin:
Expand Down
76 changes: 76 additions & 0 deletions fastexcel-writer/src/main/java/org/dhatim/fastexcel/PaperSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package org.dhatim.fastexcel;

public enum PaperSize {
LETTER_PAPER(1),
LETTER_SMALL_PAPER(2),
TABLOID_PAPER(3),
LEDGER_PAPER(4),
LEGAL_PAPER(5),
STATEMENT_PAPER(6),
EXECUTIVE_PAPER(7),
A3_PAPER(8),
A4_PAPER(9),
A4_SMALL_PAPER(10),
A5_PAPER(11),
B4_PAPER(12),
B5_PAPER(13),
FOLIO_PAPER(14),
QUARTO_PAPER(15),
STANDARD_PAPER_10_14(16),
STANDARD_PAPER_11_17(17),
NOTE_PAPER(18),
NUM9_ENVELOPE(19),
NUM10_ENVELOPE(20),
NUM11_ENVELOPE(21),
NUM12_ENVELOPE(22),
NUM14_ENVELOPE(23),
C_PAPER(24),
D_PAPER(25),
E_PAPER(26),
DL_ENVELOPE(27),
C5_ENVELOPE(28),
C3_ENVELOPE(29),
C4_ENVELOPE(30),
C6_ENVELOPE(31),
C65_ENVELOPE(32),
B4_ENVELOPE(33),
B5_ENVELOPE(34),
B6_ENVELOPE(35),
ITALY_ENVELOPE(36),
MONARCH_ENVELOPE(37),
NUM6_3_4_ENVELOPE(38),
US_STANDARD_FANFOLD(39),
GERMAN_STANDARD_FANFOLD(40),
GERMAN_LEGAL_FANFOLD(41),
B4(42),
JAPANESE_DOUBLE_POSTCARD(43),
STANDARD_PAPER_9_11(44),
STANDARD_PAPER_10_11(45),
STANDARD_PAPER_15_11(46),
INVITE_ENVELOPE(47),
LETTER_EXTRA_PAPER(50),
LEGAL_EXTRA_PAPER(51),
TABLOID_EXTRA_PAPER(52),
A4_EXTRA_PAPER(53),
LETTER_TRANSVERSE_PAPER(54),
A4_TRANSVERSE_PAPER(55),
LETTER_EXTRA_TRANSVERSE_PAPER(56),
SUPERA_SUPERA_A4_PAPER(57),
SUPERB_SUPERB_A3_PAPER(58),
LETTER_PLUS_PAPER(59),
A4_PLUS_PAPER(60),
A5_TRANSVERSE_PAPER(61),
JIS_B5_TRANSVERSE_PAPER(62),
A3_EXTRA_PAPER(63),
A5_EXTRA_PAPER(64),
B5_EXTRA_PAPER(65),
A2_PAPER(66),
A3_TRANSVERSE_PAPER(67),
A3_EXTRA_TRANSVERSE_PAPER(68);

final int xmlValue;

PaperSize(int xmlValue) {
this.xmlValue = xmlValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ public class Worksheet {
* Page orientation [landscape / portrait] for the print preview setup.
*/
private String pageOrientation = "portrait";
/**
* Paper size for the print preview setup.
*/
private PaperSize paperSize = PaperSize.LETTER_PAPER;
/**
* Scaling factor for the print setup.
*/
Expand Down Expand Up @@ -821,7 +825,7 @@ public void finish() throws IOException {

/* set page orientation for the print setup */
writer.append("<pageSetup")
.append(" paperSize=\"1\"")
.append(" paperSize=\"" + paperSize.xmlValue + "\"")
.append(" scale=\"" + pageScale + "\"")
.append(" fitToWidth=\"" + fitToWidth + "\"")
.append(" fitToHeight=\"" + fitToHeight + "\"")
Expand Down Expand Up @@ -1095,6 +1099,13 @@ public void rightMargin(float margin) {
public void pageOrientation(String orientation) {
this.pageOrientation = orientation;
}
/**
* Set the paper size.
* @param size New paper size for this worksheet
*/
public void paperSize(PaperSize size) {
this.paperSize = size;
}
/**
* @param scale = scaling factor for the print setup (between 1 and 100)
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.apache.poi.ss.usermodel.DataValidationConstraint;
import org.apache.poi.ss.usermodel.FontUnderline;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.PrintOrientation;
import org.apache.poi.ss.usermodel.SheetVisibility;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.*;
Expand Down Expand Up @@ -47,6 +48,9 @@ void singleWorksheet() throws Exception {
BigDecimal bigDecimalValue = BigDecimal.TEN;
byte[] data = writeWorkbook(wb -> {
Worksheet ws = wb.newWorksheet(sheetName);
ws.paperSize(PaperSize.A4_PAPER);
ws.pageOrientation("landscape");
ws.pageScale(80);
ws.width(0, 2);
int i = 1;
ws.hideRow(i);
Expand All @@ -72,6 +76,9 @@ void singleWorksheet() throws Exception {
assertThat(xwb.getActiveSheetIndex()).isEqualTo(0);
assertThat(xwb.getNumberOfSheets()).isEqualTo(1);
XSSFSheet xws = xwb.getSheet(sheetName);
assertThat(xws.getPrintSetup().getPaperSizeEnum()).isEqualTo(org.apache.poi.ss.usermodel.PaperSize.A4_PAPER);
assertThat(xws.getPrintSetup().getOrientation()).isEqualTo(PrintOrientation.LANDSCAPE);
assertThat(xws.getPrintSetup().getScale()).isEqualTo((short)80);
Comparable<XSSFRow> row = xws.getRow(0);
assertThat(row).isNull();
int i = 1;
Expand Down

0 comments on commit 4a483c5

Please sign in to comment.