繁体   English   中英

使用docx4j将docx部分转换为html

[英]Convert docx part to html using docx4j

我有一个应用程序尝试从数据库中提取一些数据,然后将其保存在docx文件中。 这些数据的某些部分是html代码,因此使用docx4j能够将html代码转换为docx格式。 一个相关的帖子在这里

现在,我想使用docx4j将这部分文本(在docx文件中的表格单元中)转换回html格式,并将html代码保存到数据库中。

我包装了docx4j示例中的一些代码,并具有以下代码:

public class AltChunkAddOfTypeHtml {

   private static ObjectFactory factory;
   private final static String inputfilepath = System.getProperty("user.dir")
         + "/test.docx";

   public static void main(String[] args) throws Exception {

      WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage
            .createPackage();
      MainDocumentPart mdp = wordMLPackage.getMainDocumentPart();
      factory = Context.getWmlObjectFactory();
      Tbl table = factory.createTbl();
      Tr tableRow = factory.createTr();

      Tc tableCell = factory.createTc();

      wordMLPackage.getMainDocumentPart().addObject(table);

      String xhtml = "<html><head><title>Import me</title></head><body><p>Hello World!This is the html code converted into docx!!!</p><b>tested by david</b></body></html>";
      ;

      mdp.addAltChunk(AltChunkType.Xhtml, xhtml.getBytes(), tableCell);

      tableRow.getContent().add(tableCell);
      table.getContent().add(tableRow);
      // Round trip
      wordMLPackage = mdp.convertAltChunks();

      wordMLPackage.save(new java.io.File(inputfilepath));

      List<Object> tableCells = getAllElementFromObject(
            wordMLPackage.getMainDocumentPart(), Tc.class);
      System.out.println(tableCells.size());

      /* only one tc in wordMLPackage */
      List<Object> paragraphsInTc = getAllElementFromObject(
            tableCells.get(0), P.class);
      System.out.println(paragraphsInTc.size());
      System.out.println("Ready to create html.");

      WordprocessingMLPackage wordMLPackage2 = WordprocessingMLPackage
            .createPackage();
      for (Object o : paragraphsInTc) {

         wordMLPackage2.getMainDocumentPart().addObject(o);
      }

      HTMLSettings htmlSettings = Docx4J.createHTMLSettings();

      htmlSettings.setWmlPackage(wordMLPackage2);

      OutputStream os;
      os = new FileOutputStream(new java.io.File(
            System.getProperty("user.dir") + "/sample.html"));
      System.out.println("Creating html.");
      Docx4J.toHTML(htmlSettings, os, Docx4J.FLAG_EXPORT_PREFER_XSL);

   }

   private static List<Object> getAllElementFromObject(Object obj,
         Class<?> toSearch) {
      List<Object> result = new ArrayList<Object>();
      if (obj instanceof JAXBElement)
         obj = ((JAXBElement<?>) obj).getValue();

      if (obj.getClass().equals(toSearch))
         result.add(obj);
      else if (obj instanceof ContentAccessor) {
         List<?> children = ((ContentAccessor) obj).getContent();
         for (Object child : children) {
            result.addAll(getAllElementFromObject(child, toSearch));
         }

      }
      return result;
   }
}

它对我有用,下面是我得到的html:

<?xml version="1.0" encoding="utf-8"?><!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"><head><meta content="text/html; charset=utf-8" http-equiv="Content-Type" /><style><!--/*paged media */ div.header {display: none }div.footer {display: none } /*@media print { */@page { size: A4; margin: 10%; @top-center {content: element(header) } @bottom-center {content: element(footer) } }/*element styles*/ .del  {text-decoration:line-through;color:red;} .ins {text-decoration:none;background:#c0ffc0;padding:1px;}
/* TABLE STYLES */

/* PARAGRAPH STYLES */
.DocDefaults {display:block;margin-bottom: 4mm;line-height: 115%;font-size: 11.0pt;}
.Normal {display:block;}

/* CHARACTER STYLES */ span.DefaultParagraphFont {display:inline;}
--></style><script type="text/javascript"><!--function toggleDiv(divid){if(document.getElementById(divid).style.display == 'none'){document.getElementById(divid).style.display = 'block';}else{document.getElementById(divid).style.display = 'none';}}
--></script></head><body>

  <!-- userBodyTop goes here -->

  <div style="color:red">TO HIDE THESE MESSAGES, TURN OFF debug level logging for org.docx4j.convert.out.common.writer.AbstractMessageWriter </div>

  <div class="document">

  <p class="Normal DocDefaults " style="text-align: left;position: relative; margin-left: 0in;margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: normal;color: #000000;font-style: normal;font-size: 11.0pt;;font-family: Calibri;">Hello World!This is the html code converted into docx!!!</span></p>

  <p class="Normal DocDefaults " style="text-align: left;position: relative; margin-left: 0in;margin-bottom: 0in;"><span class="DefaultParagraphFont " style="font-weight: bold;color: #000000;font-style: normal;font-size: 11.0pt;;font-family: Calibri;">tested by david</span></p></div>

  <!-- userBodyTail goes here -->

  </body></html>

由于我需要将此html代码保存到数据库中,因此仍然可以使转换后的html干净吗? 就像之前将其导入docx一样? 像这样:

<html><head><title>Import me</title></head><body><p>Hello World!This is the html code converted into docx!!!</p><b>tested by david</b></body></html>

或者,也许有更好的解决方案来完成从docx到html的转换? 希望我能说清楚。 任何提示表示赞赏。 提前致谢。

通过阅读段落来解决,并从单词开始运行,然后添加html标签。

  /**
     * Convert the description in table cell back into html code to be saved into database
     * 
     * @param tc
     * @return
     */
    private String convertTcToHtml(Tc tc) {
        StringBuilder sb = new StringBuilder();
        sb.append("<html><body>");

        List<Object> paragraphs = getAllElementFromObject(tc, P.class);
        if (paragraphs == null || paragraphs.size() == 0) {
            return null;
        }

        /* Description exported from alm only has one paragraph in word. */
        List<Object> runs = getAllElementFromObject(paragraphs.get(0), R.class);
        addRunsToHtmlStringBuffer(sb, runs);

        /* If user modify description in word it may generate more paragraphs in word. */
        if (paragraphs.size() > 1) {
            sb.append("<br />");
            for (int i = 1; i < paragraphs.size(); i++) {
                List<Object> moreRuns = getAllElementFromObject(paragraphs.get(i), R.class);
                addRunsToHtmlStringBuffer(sb, moreRuns);
                /* Every paragraph should be starting from a new line */
                sb.append("<br />");
            }
        }

        sb.append("</body></html>");
        return sb.toString();
    }

    /**
     * Add Texts of a list of Runs to the html string builder
     * 
     * @param sb
     * @param runs
     */
    private void addRunsToHtmlStringBuffer(StringBuilder sb, List<Object> runs) {
        if (runs != null && runs.size() > 0) {
            for (Object r : runs) {
                R run = (R) r;

                List<Object> brs = getAllElementFromObject(run, Br.class);
                if (brs != null && brs.size() > 0) {
                    LOGGER.info("BR:");
                    sb.append("<br/>");
                }

                /* One run usually has one text */
                List<Object> texts = getAllElementFromObject(run, Text.class);
                if (texts != null && texts.size() > 0) {
                    StringBuilder text_sb = new StringBuilder();
                    for (Object t : texts) {
                        Text text = (Text) t;
                        text_sb.append(text.getValue());
                    }

                    String htmlText = replaceWithHtmlCharacters(text_sb.toString());

                    if (run.getRPr() != null && run.getRPr().getB() != null && (run.getRPr().getB().isVal())) {
                        LOGGER.info("Bold Text:");
                        sb.append("<b>");
                        sb.append(htmlText);
                        sb.append("</b>");
                    } else {
                        LOGGER.info("Normal Text:");
                        sb.append(htmlText);
                    }
                }
            }
        }
    }

    /**
     * Replace ", <, > with html special charactors
     * 
     * @param text
     * @return
     */
    private String replaceWithHtmlCharacters(String text) {
        text = text.replace("\"", "&quot;");
        text = text.replace("<", "&lt;");
        text = text.replace(">", "&gt;");

        return text;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM