apache poi word to html conversion - words boundry

时间:2016-06-20 14:49:39

标签: java html apache-poi openxml docx

我使用下面的代码将word转换为html文件

    public Map convert(String wordDocPath, String htmlPath,
        Map conversionParams)
{
    log.info("Converting word file "+wordDocPath)
    try
    {
        String workingFolder = "C:\temp"
        File workingFolderFile = new File(workingFolder)

        FileInputStream fis = new FileInputStream(wordDocPath);
        XWPFDocument document = new XWPFDocument(fis);
        XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(workingFolderFile));
        options.setExtractor(new FileImageExtractor(workingFolderFile))
        File htmlFile = new File(htmlPath);
        OutputStream out = new FileOutputStream(htmlFile)
        XHTMLConverter.getInstance().convert(document, out, options);

        log.info("Converted to HTML file "+htmlPath)

    }
    catch(Exception e)
    {
        log.error("Exception :"+e.getMessage(),e)
    }
}

代码正确生成html输出。

我需要在像[[AGENT_NAME]]这样的文档中加入一些参数,稍后我会在代码中用regex替换它们。但apache poi并没有将这种模式视为单个单词,有时会分裂“[[”,“AGENT_NAME”& “]]”并插入一些带有样式的标签。由于它,我无法编写正则表达式并替换参数。

apache poi如何决定单词边界?有没有办法控制它?

1 个答案:

答案 0 :(得分:0)

经过所有的努力,我终于决定编写代码解析word doc并合并splited runs。这是代码,希望它能帮助别人

注意:我将模式用作${pattern}

void mergeSplittedPatterns(XWPFDocument document)
{
    List<XWPFParagraph> paragraphs = document.paragraphs

    for(XWPFParagraph paragraph : paragraphs)
    {
        List<XWPFRun> runs = paragraph.getRuns()

        int firstCharRun,closingCharRun
        boolean firstCharFound = false;
        boolean secondCharFoundImmediately = false;
        boolean closingCharFound = false;
        boolean gotoNextRun = true

        boolean scan = (runs!=null && runs.size()>0)
        int index = 0

        while(scan)
        {
            gotoNextRun = true;
            XWPFRun run = runs.get(index)
            String runText = run.getText(0)
            if(runText!=null)
                for (int i = 0; i < runText.length(); i++)
            {
                char character = runText.charAt(i);

                if(secondCharFoundImmediately)
                {
                    closingCharFound = (character=="}")
                    if(closingCharFound)
                    {
                        closingCharRun = index

                        if(firstCharRun==closingCharRun)
                        {
                            firstCharFound = secondCharFoundImmediately = closingCharFound = false
                            continue;
                        }
                        else
                        {
                            String mergedText= ""
                            for(int j=firstCharRun;j<=closingCharRun;j++)
                            {
                                mergedText += runs.get(j).getText(0)
                            }
                            runs.get(firstCharRun).setText(mergedText,0)

                            for(int j=closingCharRun;j>firstCharRun;j--)
                            {
                                paragraph.removeRun(j)
                            }
                            firstCharFound = secondCharFoundImmediately = closingCharFound = gotoNextRun = false
                            index = firstCharRun
                            break;
                        }
                    }
                }
                else if(firstCharFound)
                {
                    secondCharFoundImmediately = (character=="{")
                    if(!secondCharFoundImmediately)
                    {
                        firstCharFound = secondCharFoundImmediately = closingCharFound = false
                    }
                }
                else if(character=="\$")
                {
                    firstCharFound = true;
                    firstCharRun = index
                }
            }

            if(gotoNextRun)
            {
                index++;
            }

            if(index>=runs.size())
            {
                scan = false;
            }
        }
    }
}