使用Docx4j将HTML转换为Docx时跨页表格断裂及文档全局属性、单元格间距问题求助
Let's break down solutions for each of your problems using Docx4j 8.3.2 and Java 8:
1. Fix Table Cross-Page Breakage
Tables splitting across pages can be resolved either by adding CSS to your HTML or post-processing the imported DOCX content. Here's the post-processing approach (more reliable if CSS isn't fully supported):
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage(); XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage); List<Object> content = XHTMLImporter.convert(file, null); // Post-process tables to prevent splitting for (Object obj : content) { if (obj instanceof Tbl) { Tbl table = (Tbl) obj; // Prevent entire table from splitting TblPr tblPr = table.getTblPr() != null ? table.getTblPr() : new TblPr(); CantSplit cantSplit = new CantSplit(); cantSplit.setVal(Boolean.TRUE); tblPr.getCantSplitOrTblWOrTblCaptionOrTblDescription().add(cantSplit); table.setTblPr(tblPr); // Optional: Keep individual rows together for (Object trObj : table.getContent()) { if (trObj instanceof Tr) { Tr row = (Tr) trObj; TrPr trPr = row.getTrPr() != null ? row.getTrPr() : new TrPr(); KeepLines keepLines = new KeepLines(); keepLines.setVal(Boolean.TRUE); trPr.getKeepLinesOrKeepNextOrNoWrap().add(keepLines); KeepNext keepNext = new KeepNext(); keepNext.setVal(Boolean.TRUE); trPr.getKeepLinesOrKeepNextOrNoWrap().add(keepNext); row.setTrPr(trPr); } } } } wordMLPackage.getMainDocumentPart().getContent().addAll(content); File fileDos = new File(urlWord); wordMLPackage.save(fileDos); // Don't forget to save!
2. Set Global Paragraph Properties
To modify global styles like paragraph spacing, update the default "Normal" style in your document:
// After creating the WordprocessingMLPackage Styles styles = wordMLPackage.getMainDocumentPart().getStyleDefinitionsPart().getJaxbElement(); Style normalStyle = null; // Find the Normal style for (Style style : styles.getStyle()) { if ("Normal".equals(style.getStyleId())) { normalStyle = style; break; } } if (normalStyle != null) { PPr pPr = normalStyle.getPPr() != null ? normalStyle.getPPr() : new PPr(); Spacing spacing = new Spacing(); // Set values in twips (1 twip = 1/20 pt) spacing.setBefore(BigInteger.valueOf(120)); // 6pt before paragraph spacing.setAfter(BigInteger.valueOf(120)); // 6pt after paragraph spacing.setLine(BigInteger.valueOf(240)); // 1.5x line spacing (160 = 1x) spacing.setLineRule(STLineSpacingRule.AUTO); pPr.setSpacing(spacing); normalStyle.setPPr(pPr); }
3. Fix Inconsistent Table Cell Spacing
Inconsistencies often come from default Word cell margins or inconsistent HTML styles. Force consistent margins for all tables with this post-processing step:
// Add this loop after converting HTML to content (same as the table break fix) for (Object obj : content) { if (obj instanceof Tbl) { Tbl table = (Tbl) obj; TblPr tblPr = table.getTblPr() != null ? table.getTblPr() : new TblPr(); TblCellMar cellMar = new TblCellMar(); // Set 2.5pt margin on all sides (50 twips = 2.5pt) TblWidth marginWidth = new TblWidth(); marginWidth.setW(BigInteger.valueOf(50)); marginWidth.setType(STTblWidth.DXA); cellMar.setTop(marginWidth); cellMar.setBottom(marginWidth); cellMar.setLeft(marginWidth); cellMar.setRight(marginWidth); tblPr.getTblCellMarOrTblBordersOrTblLook().add(cellMar); table.setTblPr(tblPr); } }
Bonus Check:
If the issue persists, verify your HTML has consistent padding/margin styles on <td> elements (e.g., <td style="padding: 2.5pt;">). The post-processing above will override most inconsistent HTML styles, though.
内容的提问来源于stack exchange,提问作者MartinGam

