为什么在使用 docx4j 生成文档时使用 Xhtmlimporter 和 AlternativeFormatInputPart 存在样式不匹配

Why there is style mismatch on Using Xhtmlimporter and AlternativeFormatInputPart on generating document using docx4j

我正在尝试将文档从 HTML 生成为 docx。通过 AlternativeFormatInputPart class 文档以正确的样式正确生成。

但是

当我使用 XhtmlImporter class 时,html 文档的生成方式不同。我想使用 XhtmlImporter class

我该如何解决这个问题?

下面是Html和java用于生成文档的代码。

<!DOCTYPE html
  PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.w3.org/1999/xhtml" lang="en">
   <head>
      <title>MonthlyReport</title><style>
                    table
                    {
                        border:double #000;
                        table-layout:fixed;
                        vertical-align:top;
                        border-collapse:collapse;
                        width:7.25in;
                    
                    }
                    .main-title
                    {
                        margin-top:5%;
                        font-size:48pt;
                        font-family:Century Gothic;
                        color:#2F5897;
                        text-align:center;
                        font-weight:300;
                    }
                    .year
                    {
                        padding-top:10%;
                        text-align:center;
                        font-size:18px;
                    
                    }
                    .other-detail
                    {
                        padding-top:10%;
                        padding-bottom:10%;
                        font-weight:bold;
                        text-align:center;
                        font-size:18px;
                    }
                    .cont
                    {
                        font-family:Palatino Linotype;
                        margin:0 auto;
                        margin-top:10px;
                    }
                    .cont-table
                    {
                        border:double #000;
                        table-layout:fixed;
                        vertical-align:top;
                        border-collapse:collapse;
                        width:7.25in;
                    }
                    .cont h1
                    {
                        font-family:Century Gothic;
                        color:#2F5897;
                        font-weight:400;
                        margin-left:.15in;
                    }
                    .cont h4
                    {
                        font-family:Century Gothic;
                        font-weight:400;
                        margin-left:.15in;
                    }
                    .cont .school-detail
                    {
                        margin-left:.1in;
                        margin-right:.1in;
                    }
                    .cont ol
                    {
                        margin-left:1in;
                        margin-right:1in;
                    }
                    .cont li
                    {
                        margin-bottom:.25in;
                    }
                    .cont-heading
                    {
                        margin-left:.5in;
                        font-weight:bold;
                        text-decoration:underline;
                        margin-top:.35in;
                    
                    }
                    span
                    {
                        display:block;
                        padding-bottom:10px;
                    }
                    .rheight
                    {
                        height:3.25in;
                    }
                    span.DefaultParagraphFont{
                        display:inline;
                    }
                    .DocDefaults{
                        display:block;
                        margin-bottom:4mm;
                        line-height:115%;
                        font-size:11.0pt;
                    }
                    .Normal{
                        display:block;
                    }</style></head>
   <body>
      <table cellpadding="0" cellspacing="0" align="center">
         <tr class="rheight">
            <td> &nbsp; </td>
         </tr>
         <tr>
            <td>
               <div class="main-title"> Monthly report </div>
            </td>
         </tr>
         <tr>
            <td>
               <div class="year">2016-2017</div>
            </td>
         </tr>
         <tr>
            <td>
               <div class="other-detail">MONTH: September<br></br> CITY: BANGALORE<br></br> FACILITATOR:
                  MANIKANDAN<br></br></div>
            </td>
         </tr>
         <tr class="rheight">
            <td> &nbsp; </td>
         </tr>
      </table>
      <table cellpadding="0" cellspacing="0" align="center" class="cont-table">
         <tr>
            <td>
               <div class="cont">
                  <h1>Monthly Report</h1>
                  <h4>2016-2017</h4><br></br><p class="school-detail">
                     <p class="cont-heading">102 INTERNATONAL PUBLIC SCHOOL(ICSE)</p>
                     <ol>
                        <li><strong>ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING
                              TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY
                              SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION
                              THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES
                              CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR
                              NOTICE REGARDING THE SCHOOL.</strong><br></br>z1
                        </li>
                        <li><strong>ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING
                              BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE
                              LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK).</strong><br></br>z2
                        </li>
                        <li><strong>AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.
                              AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.</strong><br></br>z3
                        </li>
                        <li><strong>WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.
                              WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.</strong><br></br>z4
                        </li>
                        <li><strong>DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS
                              OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC.</strong><br></br>z5
                        </li>
                     </ol>
                     <p class="cont-heading">Photos</p>
                     <p class="cont-heading">102 INTERNATONAL PUBLIC SCHOOL(ICSE)</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-1</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">102 INTERNATONAL PUBLIC SCHOOL(ICSE)</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-1</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">102 INTERNATONAL PUBLIC SCHOOL(ICSE)</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-2</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">ELITE EDUCATIONAL TRUST</p>
                     <ol>
                        <li><strong>ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING
                              TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY
                              SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION
                              THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES
                              CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR
                              NOTICE REGARDING THE SCHOOL.</strong><br></br>y1
                        </li>
                        <li><strong>ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING
                              BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE
                              LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK).</strong><br></br>y2
                        </li>
                        <li><strong>AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.
                              AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.</strong><br></br>y3
                        </li>
                        <li><strong>WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.
                              WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.</strong><br></br>y4
                        </li>
                        <li><strong>DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS
                              OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC.</strong><br></br>y5
                        </li>
                     </ol>
                     <p class="cont-heading">Photos</p>
                     <p class="cont-heading">ACHALA VIDHYA MANDIRA</p>
                     <ol>
                        <li><strong>ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING
                              TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY
                              SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION
                              THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES
                              CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR
                              NOTICE REGARDING THE SCHOOL.</strong><br></br>q1
                        </li>
                        <li><strong>ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING
                              BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE
                              LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK).</strong><br></br>q2
                        </li>
                        <li><strong>AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.
                              AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.</strong><br></br>q3
                        </li>
                        <li><strong>WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.
                              WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.</strong><br></br>q4
                        </li>
                        <li><strong>DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS
                              OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC.</strong><br></br>q5
                        </li>
                     </ol>
                     <p class="cont-heading">Photos</p>
                     <p class="cont-heading">ACHALA VIDHYA MANDIRA</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-1</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">ACHALA VIDHYA MANDIRA</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-2</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">CITIZENS HIGH SCHOOL</p>
                     <ol>
                        <li><strong>ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING
                              TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY
                              SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES CONDUCTED IN THIS SCHOOL. PLEASE MENTION
                              THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR NOTICE REGARDING THE SCHOOL. ACTIVITIES
                              CONDUCTED IN THIS SCHOOL. PLEASE MENTION THE TYPE OF SESSIONS CONDUCTED, ANY SPECIFIC POINT YOU WOULD LIKE TO BRING TO OUR
                              NOTICE REGARDING THE SCHOOL.</strong><br></br>w1
                        </li>
                        <li><strong>ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING
                              BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE
                              LINK). ANY SPECIFIC ACTIVITY WORTH MENTIONING BY THE STUDENTS(ATTACH IMAGES TO SEPARATE LINK).</strong><br></br>w2
                        </li>
                        <li><strong>AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL. AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.
                              AT LEAST ONE TESTIMONIAL FROM ONE SCHOOL.</strong><br></br>w3
                        </li>
                        <li><strong>WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.
                              WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME. WHAT WENT RIGHT; WHAT DID NOT GO RIGHT &amp; REASONS FOR THE SAME.</strong><br></br>w4
                        </li>
                        <li><strong>DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS
                              OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC. DETAILS OF INTERACTION WITH THE PRINCIPAL AND/OR THE POC.</strong><br></br>w5
                        </li>
                     </ol>
                     <p class="cont-heading">Photos</p>
                     <p class="cont-heading">CITIZENS HIGH SCHOOL</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-1</p>
                           </center>I
                           </span></p>
                     <p class="cont-heading">CITIZENS HIGH SCHOOL</p>
                     <p class="Normal DocDefaults " style="margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;font-size: 14.0pt;text-decoration: underline;">
                           <center><img height="355" width="620" src="C:\Oracle\Middleware\Weblogic12.1.3\user_projects\domains\springtutorial\servers\ManagedServer\tmp\_WL_user\FMS\ti01mv\public\Hydrangeas.jpg" alt="C:/Users/Public/Pictures/Sample Pictures/Hydrangeas.jpg"></img><p style="text-align:center">Image-2</p>
                           </center>I
                           </span></p>
                  </p>
               </div>
            </td>
         </tr>
      </table>
   </body>
</html>

AlternativeFormatInputPart 方法

private static void documentGenerator(String html, File file) throws Docx4JException, JAXBException {
//Word Processing Package
WordprocessingMLPackage wordMLPackage = getWordMLPackage();
NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();
AlternativeFormatInputPart inputPart = new AlternativeFormatInputPart(AltChunkType.Xhtml);
inputPart.setContentType(new ContentType("text/html"));
inputPart.setBinaryData(html.getBytes());
Relationship altChunkRel = wordMLPackage.getMainDocumentPart().addTargetPart(inputPart);
// .. the bit in document body
CTAltChunk ac = Context.getWmlObjectFactory().createCTAltChunk();
ac.setId(altChunkRel.getId());
wordMLPackage.getMainDocumentPart().addObject(ac);
// .. content type
wordMLPackage.getContentTypeManager().addDefaultContentType("html", "text/html");
//Saving the Document
wordMLPackage.save(file);
}

XHtml进口商

WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();

NumberingDefinitionsPart ndp = new NumberingDefinitionsPart();
wordMLPackage.getMainDocumentPart().addTargetPart(ndp);
ndp.unmarshalDefaultNumbering();

// Convert the XHTML, and add it into the empty docx we made
XHTMLImporterImpl XHTMLImporter = new XHTMLImporterImpl(wordMLPackage);
XHTMLImporter.setRunFormatting(FormattingOption.CLASS_PLUS_OTHER);
XHTMLImporter.setHyperlinkStyle("Hyperlink");
wordMLPackage.getMainDocumentPart().getContent().addAll(XHTMLImporter.convert(unescaped, null));
wordMLPackage.save(new java.io.File("C:/OUT_from_XHTML.docx"));

正如我在 中解释的那样,当您保存包含 altChunk 的 docx 时,您将依赖 Word 来转换内容。

Word的转换不同于docx4j的XHTMLImporterImpl;后者并不旨在模仿前者。

有关 docx4j 的 XHTMLImporterImpl 的具体问题可以在此处、相关的 docx4j 论坛或 https://github.com/plutext/docx4j-ImportXHTML/issues 适当提出。

如果您坚持每个 post 一期,您将获得最佳结果。