版本:23.8
编程语言:java
问题截图:
原html文件:
新建文本文档 (2).zip (937 字节)
转换后的PDF:
toPdf2.zip (77.1 KB)
转换代码:
public static void main(String[] args) throws Exception {
Document doc = new Document(new FileInputStream("C:\\Users\\admin\\Desktop\\新建文本文档 (2).html"));
try {
NodeCollection childNodes = doc.getChildNodes(NodeType.PARAGRAPH, true);
if (childNodes != null) {
for (Paragraph para : (Iterable<Paragraph>) childNodes) {
if (para.getListFormat().isListItem()) {
ListLevel listLevel = para.getListFormat().getListLevel();
listLevel.getFont().setColor(Color.BLACK);
if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_2) {
listLevel.setNumberStyle(NumberStyle.ARABIC);
listLevel.setNumberFormat("\u0001.\u0001、");
}
if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_3) {
listLevel.setNumberStyle(NumberStyle.ARABIC);
listLevel.setNumberFormat("\u0001.\u0001.\u0002、");
}
if (para.getParagraphFormat().getStyleIdentifier() == StyleIdentifier.HEADING_4) {
listLevel.setNumberStyle(NumberStyle.ARABIC);
listLevel.setNumberFormat("\u0001.\u0001.\u0002.\u0003、");
listLevel.getFont().setSize(10.5);
listLevel.getFont().setBold(true);
listLevel.getFont().getShading().clearFormatting();
}
}
}
}
} catch (Exception e) {
logger.warn("html转换word,处理样式异常", e);
}
FileOutputStream fileOutputStream = new FileOutputStream("C:\\Users\\admin\\Desktop\\toPdf2.pdf");
doc.updateListLabels();
doc.save(fileOutputStream, SaveFormat.PDF);
}