Skip to content

Commit

Permalink
Upgrade to poi-tl 1.11.0 #25 #28
Browse files Browse the repository at this point in the history
  • Loading branch information
draco1023 committed Jan 7, 2022
1 parent 94cc017 commit e4a8444
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ repositories {
}

dependencies {
api 'com.deepoove:poi-tl:1.9.1'
api 'org.apache.poi:ooxml-schemas:1.4'
api 'com.deepoove:poi-tl:1.11.0'
api 'org.apache.poi:poi-ooxml-full:5.1.0'
api 'org.apache.commons:commons-lang3:3.10'
api 'commons-io:commons-io:2.6'
api 'commons-io:commons-io:2.11.0'
implementation 'net.sourceforge.cssparser:cssparser:0.9.29'
implementation 'org.jsoup:jsoup:1.12.1'
implementation 'net.sf.saxon:Saxon-HE:10.0'
Expand Down
41 changes: 21 additions & 20 deletions src/main/java/org/ddr/poi/html/HtmlRenderContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.steadystate.css.dom.CSSStyleDeclarationImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.apache.poi.ooxml.util.POIXMLUnits;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BodyType;
Expand All @@ -42,6 +43,7 @@
import org.ddr.poi.html.util.RenderUtils;
import org.ddr.poi.html.util.WhiteSpaceRule;
import org.jsoup.internal.StringUtil;
import org.openxmlformats.schemas.officeDocument.x2006.sharedTypes.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDrawing;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
Expand All @@ -55,7 +57,6 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STThemeColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;

import javax.xml.namespace.QName;
import java.io.IOException;
Expand Down Expand Up @@ -173,27 +174,27 @@ public HtmlRenderContext(RenderContext<String> context) {

CTSectPr sectPr = getXWPFDocument().getDocument().getBody().getSectPr();
CTPageSz pgSz = sectPr.getPgSz();
// 页面尺寸单位是twip
int w = pgSz.getW().intValue();
pageWidth = new CSSLength(w, CSSLengthUnit.TWIP);
int h = pgSz.getH().intValue();
pageHeight = new CSSLength(h, CSSLengthUnit.TWIP);

long w = POIXMLUnits.parseLength(pgSz.xgetW());
pageWidth = new CSSLength(w, CSSLengthUnit.EMU);
long h = POIXMLUnits.parseLength(pgSz.xgetH());
pageHeight = new CSSLength(h, CSSLengthUnit.EMU);

CTPageMar pgMar = sectPr.getPgMar();
int top = pgMar.getTop().intValue();
marginTop = new CSSLength(top, CSSLengthUnit.TWIP);
int right = pgMar.getRight().intValue();
marginRight = new CSSLength(right, CSSLengthUnit.TWIP);
int bottom = pgMar.getBottom().intValue();
marginBottom = new CSSLength(bottom, CSSLengthUnit.TWIP);
int left = pgMar.getLeft().intValue();
marginLeft = new CSSLength(left, CSSLengthUnit.TWIP);

availablePageWidth = new CSSLength(w - left - right, CSSLengthUnit.TWIP).toEMU();
availablePageHeight = new CSSLength(h - top - bottom, CSSLengthUnit.TWIP).toEMU();

int fontSize = getXWPFDocument().getStyles().getDefaultRunStyle().getFontSize();
defaultFontSize = fontSize > 0 ? new CSSLength(fontSize, CSSLengthUnit.PT) : DEFAULT_FONT_SIZE;
long top = POIXMLUnits.parseLength(pgMar.xgetTop());
marginTop = new CSSLength(top, CSSLengthUnit.EMU);
long right = POIXMLUnits.parseLength(pgMar.xgetRight());
marginRight = new CSSLength(right, CSSLengthUnit.EMU);
long bottom = POIXMLUnits.parseLength(pgMar.xgetBottom());
marginBottom = new CSSLength(bottom, CSSLengthUnit.EMU);
long left = POIXMLUnits.parseLength(pgMar.xgetLeft());
marginLeft = new CSSLength(left, CSSLengthUnit.EMU);

availablePageWidth = (int) (w - left - right);
availablePageHeight = (int) (h - top - bottom);

Double fontSize = getXWPFDocument().getStyles().getDefaultRunStyle().getFontSizeAsDouble();
defaultFontSize = fontSize != null ? new CSSLength(fontSize, CSSLengthUnit.PT) : DEFAULT_FONT_SIZE;
}

@Override
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/ddr/poi/html/tag/TableCellRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ public void renderEnd(Element element, HtmlRenderContext context) {
if (paragraphs.size() > 1) {
XmlCursor xmlCursor = paragraphs.get(0).getCTP().newCursor();
if (!xmlCursor.toFirstChild()) {
xmlCursor.removeXml();
paragraphs.remove(0);
((XWPFTableCell) context.getContainer()).removeParagraph(0);
}
xmlCursor.dispose();
}
Expand Down
48 changes: 36 additions & 12 deletions src/main/java/org/ddr/poi/html/util/RenderUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.steadystate.css.dom.CSSStyleDeclarationImpl;
import com.steadystate.css.dom.CSSValueImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ooxml.util.POIXMLUnits;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.BodyType;
import org.apache.poi.xwpf.usermodel.IBody;
Expand All @@ -38,6 +39,7 @@
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPrGeneral;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
Expand All @@ -57,6 +59,7 @@
import org.w3c.dom.css.CSSValue;

import java.math.BigInteger;
import java.util.List;
import java.util.function.Function;

/**
Expand Down Expand Up @@ -138,7 +141,7 @@ public static STUnderline.Enum underline(String textDecorationStyle) {
}
}

public static CTPPr getPPr(CTStyle ctStyle) {
public static CTPPrGeneral getPPr(CTStyle ctStyle) {
return ctStyle.isSetPPr() ? ctStyle.getPPr() : ctStyle.addNewPPr();
}

Expand Down Expand Up @@ -171,11 +174,13 @@ public static CTInd getInd(CTPPr pPr) {
}

public static CTColor getColor(CTRPr rPr) {
return rPr.isSetColor() ? rPr.getColor() : rPr.addNewColor();
List<CTColor> colorList = rPr.getColorList();
return colorList.isEmpty() ? rPr.addNewColor() : colorList.get(0);
}

public static CTUnderline getUnderline(CTRPr rPr) {
return rPr.isSetU() ? rPr.getU() : rPr.addNewU();
List<CTUnderline> uList = rPr.getUList();
return uList.isEmpty() ? rPr.addNewU() : uList.get(0);
}

/**
Expand All @@ -188,21 +193,30 @@ public static int getAvailableWidthInEMU(IBody body) {
if (body.getPartType() == BodyType.DOCUMENT) {
XWPFDocument document = (XWPFDocument) body;
CTSectPr sectPr = document.getDocument().getBody().getSectPr();
int availableWidth = sectPr.getPgSz().getW().intValue()
- sectPr.getPgMar().getLeft().intValue() - sectPr.getPgMar().getRight().intValue();
return Units.TwipsToEMU((short) availableWidth);
long availableWidth = POIXMLUnits.parseLength(sectPr.getPgSz().xgetW())
- POIXMLUnits.parseLength(sectPr.getPgMar().xgetLeft())
- POIXMLUnits.parseLength(sectPr.getPgMar().xgetRight());
return (int) availableWidth;

} else if (body.getPartType() == BodyType.TABLECELL) {
XWPFTableCell tableCell = ((XWPFTableCell) body);
CTTblWidth tcW = tableCell.getCTTc().getTcPr().getTcW();
if (TableWidthType.DXA.getStWidthType().equals(tcW.getType())) {
int availableWidth = tcW.getW().intValue() - TABLE_CELL_MARGIN * 2;
return availableWidth > 0 ? Units.TwipsToEMU((short) availableWidth) : 0;
long availableWidth = POIXMLUnits.parseLength(tcW.xgetW()) - twipsToEMU(TABLE_CELL_MARGIN) * 2;
return availableWidth > 0 ? (int) availableWidth : 0;
} else if (TableWidthType.PCT.getStWidthType().equals(tcW.getType())) {
CTTblWidth tblW = tableCell.getTableRow().getTable().getCTTbl().getTblPr().getTblW();
if (TableWidthType.DXA.getStWidthType().equals(tblW.getType())) {
int availableWidth = tblW.getW().intValue() * tcW.getW().intValue() / 5000 - TABLE_CELL_MARGIN * 2;
return availableWidth > 0 ? Units.TwipsToEMU((short) availableWidth) : 0;
String cellWidth = tcW.xgetW().getStringValue();
double percent;
if (cellWidth.endsWith("%")) { // 2011
percent = Double.parseDouble(cellWidth.replace("%", ""));
} else { // 2006
percent = Double.parseDouble(cellWidth) / 5000;
}
long availableWidth = Math.round(POIXMLUnits.parseLength(tblW.xgetW()) * percent)
- twipsToEMU(TABLE_CELL_MARGIN) * 2;
return availableWidth > 0 ? (int) availableWidth : 0;
} else if (TableWidthType.NIL.getStWidthType().equals(tblW.getType())) {
return 0;
} else {
Expand Down Expand Up @@ -485,13 +499,23 @@ public static int largerFontSizeInHalfPoints(int inheritedSizeInHalfPoints) {
return FONT_SIZE_IN_HALF_POINTS[FONT_SIZE_IN_HALF_POINTS.length - 1];
}

/**
* poi 5.x 版本{@link Units}中的该方法被删除了
*
* @param twips (1/20th of a point) typically used for row heights
* @return equivalent EMUs
*/
public static int twipsToEMU(int twips) {
return twips * Units.EMU_PER_DXA;
}

/**
* EMU转twip
*
* @see Units#TwipsToEMU
* @see #twipsToEMU
*/
public static int emuToTwips(int emu) {
return (int) (emu * 20L / Units.EMU_PER_POINT);
return emu / Units.EMU_PER_DXA;
}

/**
Expand Down

0 comments on commit e4a8444

Please sign in to comment.