我正在使用docx4j库将html文件转换为A4中的docx文档-纵向。 html代码包含以下元素:
在输出文档中,我看到以下问题:
即使使用纵向方向(docx4j.properties),似乎计算也是使用横向方向进行的。 从我所看到的简单示例中,一切都应该按预期进行,但并非如此。
我也尝试了以下方法:
看来这个问题很少见。我在做错什么吗?
谢谢!
下面是屏幕截图,其中包含生成的文件和源代码(Java,docx4j属性和HTML)
package app.docx;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import org.docx4j.convert.in.xhtml.XHTMLImporterImpl;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
public class Entrypoint {
public static void main(String[] args) throws Exception {
// read html content from file
String bodyXHTML = new String(Files.readAllBytes(Paths.get("path-to-file.html")));
bodyXHTML = bodyXHTML.replaceAll("\\r|\\n|\\t", "");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
List<Object> items = XHTMLImporter.convert(bodyXHTML, "");
XHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(items);
// save docx to file
String dir = System.getProperty("user.dir");
File outputFile = new File (dir, "output.docx");
wordMLPackage.save(outputFile);
}
}
# docx setup
# XHTMLImporter needs to know how to convert pixel measurements
# This is read by UnitsOfMeasurement
docx4j.DPI=96
# Page size: use a value from org.docx4j.model.structure.PageSizePaper enum
# eg A4, LETTER
docx4j.PageSize=A4
# Page size: use a value from org.docx4j.model.structure.MarginsWellKnown enum
docx4j.PageMargins=NORMAL
docx4j.PageOrientationLandscape=false
# ..
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<img src="https://www.catster.com/wp-content/uploads/2015/06/cat-breeds-maine-coon-42779359.jpg" width="100%" />
<table align="center">
<tr>
<td>Name:</td>
<td>Jackson</td>
</tr>
</table>
</body>
</html>