Search code examples
javahtmlxhtmldocxdocx4j

docx4j cannot render margin properly


I am using docx4j-ImportXHTML to convert an XHTML into docx. When rendering to docx I am missing some style information.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>Title of document</title>
</head>

<body>
<p>This is <b>bold</b></p>
------------------------
<p style="margin-top:200px;margin-right:150px;margin-bottom:100px;margin-left:100px;">indent text</p>
------------------------
</body>

</html>

When running the above code, I do not see any top margin in the docx (left margin is there though). Also, if margins are defined as %, e.g., margin-left:4%, docx4j connot recognize it.

  1. Is there anything else that I am missing or is this just docx4j limitation?
  2. Is there any other better alternative in Java for converting XHTML to docx?

Solution

  • Are you trying to add space around a paragraph, or page margins?

    If you wanted page margins, you'd need paged media css to translate to w:sectPr/w:pgMargin

    At the Flying Saucer end, see https://flyingsaucerproject.github.io/flyingsaucer/r8/guide/users-guide-R8.html#xil_36

    I don't think we translate that instruction yet though.