【问题标题】:loses centering attribute from RTF to HTML Conversion失去从 RTF 到 HTML 转换的居中属性
【发布时间】:2021-05-01 02:58:27
【问题描述】:

我正在尝试使用 OO 将 RTF 输入(来自 MS Word 2000)转换为 HTML。 如果我打开 RTF 文件,屏幕上的一切看起来都完全正确。 如果我将其保存为 HTML,然后在 OO 中重新打开 HTML 文件,几乎所有内容看起来都相同(例外:表格)。 但是,如果我在 Firefox 中打开 HTML 文件,则文本不正确。具体来说,在 RTF 中居中或右对齐的段落以及在 OO HTML 中查看时现在都是左对齐的。

这很奇怪,因为HTML文件的文本是

<P CLASS="western" ALIGN=CENTER STYLE="text-indent: 0in; margin-bottom: 0in">
<FONT COLOR="#000000"><FONT FACE="Verdana, sans-serif"><FONT SIZE=4 STYLE="font-size: 16pt"><B>Some text that should be centered</B></FONT></FONT></FONT></P>

而“西方”类是

P.western { font-size: 10pt; so-language: en-US }

有人知道为什么居中没有按预期工作吗?改天我会处理桌子的问题。 网址:https://netsuite.folio3.com/

【问题讨论】:

    标签: html rtf


    【解决方案1】:

    这里有一个非常有用的链接到这个问题

    http://user.services.openoffice.org/en/forum/viewtopic.php?f=30&t=46850

    【讨论】:

      【解决方案2】:

      我创建了一个脚本来在 rtf 到 html 转换之后设置 centerright 对齐。我用javascript和Java制作了它。按照代码:

      两者都需要两个 css 类:

      .wricent {
          text-align: center;
      }
      .wririgh {
          text-align: right;
      }
      

      JavaScript

      function fixCenterRightAlign(rtf, html) {
        html = fixAlign(rtf, html, 'qc');
        return fixAlign(rtf, html, 'qr');
      }
      
      function fixAlign(rtf, html, align) {
        let lastCountParBeforePard = 0;
        let countParBeforePard = 0;
        let indexPard = 0;
        let iqc = 0;
        let toContinue = true;
        let firstCicle = true;
        const p_class = '<p class=';
        const c_class = align === 'qc' ? '<p class=wricent' : '<p class=wririgh';
        while(toContinue) {
          let indexNextP = 0;
          iqc = rtf.substr(indexPard).indexOf('\\' + align);
          if (iqc > -1) {
            iqc += indexPard;
            let parQtLeft = getParQt(rtf.substr(0, iqc));
            let rtfFirstQc = rtf.substr(iqc);
            indexPard = rtfFirstQc.indexOf('\\pard');
            if (indexPard > -1) {
              rtfFirstQc = rtfFirstQc.substr(0, indexPard);
              countParBeforePard = getParBeforePard(rtfFirstQc, indexPard);
            } else {
              toContinue = false;
              indexPard = 0;
              countParBeforePard = 0;
            }
            let parQt = getParQt(rtfFirstQc) + (lastCountParBeforePard - countParBeforePard);
            firstCicle && parQt++;
            indexPard += iqc;
      
            if (parQt > 0) {
              for (let i = 0; i < (parQtLeft + parQt); i++) {
                let actualIdexNextP = html.substr(indexNextP).indexOf(p_class);
      
                if ((i + 1) > parQtLeft && actualIdexNextP > -1) {
                  html = replaceAt(html, indexNextP + actualIdexNextP, c_class);
                }
                indexNextP += c_class.length + actualIdexNextP;
              }
            }
            lastCountParBeforePard = angular.copy(countParBeforePard);
          } else {
            toContinue = false;
          }
          firstCicle = false;
        }
        return html;
      }
      function replaceAt(text, index, character) {
          return text.substr(0, index) + character + text.substr(index + character.length);
      }
      
      function getParBeforePard(rtfFirstQc, indexPard) {
        let text = rtfFirstQc.substr(indexPard - 6, indexPard);
        return getParQt(text);
      }
      
      function getParQt(text) {
        let pardQt = text.match(new RegExp('\\\\pard', "g"));
        let parQt = text.match(new RegExp('\\\\par', "g"));
        pardQt = pardQt ? pardQt.length : 0;
        return (parQt ? parQt.length : 0) - pardQt;
      }
      

      Java

          private final String RTF_CENTER_TAG = "qc";
          private final String RTF_RIGHT_TAG = "qr";
      
          /**
           * Fix the alignment center and right of the html template
           * @param rtf String containing the <b>rtf template</b>
           * @param html String containing the <b>html template from the rtf convertion</b>
           * @author desilva
           * @return String with the html template with the center/right align fixed
           */
          private String stylizeAligment(String rtf, String html) {
              html = fixAlign(rtf, html, this.RTF_CENTER_TAG);
              return fixAlign(rtf, html, this.RTF_RIGHT_TAG);
          }
      
          /**
           * Fix the align of the html template based on the rtf and the rtf tag to fix
           * @param rtf String containing the <b>rtf template</b>
           * @param html String containing the <b>html template from the rtf convertion</b>
           * @param tagAlign
           * @return
           */
          private String fixAlign(String rtf, String html, String tagAlign) {
              Integer parQt = 0;
              Integer pardQt = 0;
              Integer lastCountParBeforePard = 0;
              Integer countParBeforePard = 0;
              Integer indexPard = 0;
              Integer iqc = 0;
              boolean toContinue = true;
              boolean firstCicle = true;
              String pClass = "<p class=";
              String cClass = (tagAlign.equals(this.RTF_CENTER_TAG) ? "<p class=wricent" : "<p class=wririgh");
              while(toContinue) {
                int indexNextP = 0;
                iqc = rtf.substring(indexPard).indexOf("\\" + tagAlign);
                if (iqc > -1) {
                  iqc += indexPard;
                  Integer pardQtLeft = this.getMatches(rtf.substring(0, iqc), "\\\\pard");
                  Integer parQtLeft = this.getMatches(rtf.substring(0, iqc), "\\\\par") - pardQtLeft;
                  String rtfFirstQc = rtf.substring(iqc);
                  indexPard = rtfFirstQc.indexOf("\\pard");
                  if (indexPard > -1) {
                    rtfFirstQc = rtfFirstQc.substring(0, indexPard);
                    countParBeforePard = this.getParBeforePard(rtfFirstQc, indexPard);
                  } else {
                    toContinue = false;
                    indexPard = 0;
                    countParBeforePard = 0;
                  }
                  pardQt = this.getMatches(rtfFirstQc,"\\\\pard");
                  parQt = this.getMatches(rtfFirstQc,"\\\\par") - pardQt;
                  parQt += (lastCountParBeforePard - countParBeforePard);
                  if(firstCicle) parQt++;
                  indexPard += iqc;
      
                  if (parQt > 0) {
                    for (int i = 0; i < (parQtLeft + parQt); i++) {
                      Integer actualIdexNextP = html.substring(indexNextP).indexOf(pClass);
      
                      if ((i + 1) > parQtLeft && actualIdexNextP > -1) {
                        html = this.replaceAt(html, indexNextP + actualIdexNextP, cClass);
                      }
                      indexNextP += cClass.length() + actualIdexNextP;
                    }
                  }
                  lastCountParBeforePard = countParBeforePard;
                } else {
                  toContinue = false;
                }
                firstCicle = false;
              }
              return html;
          }
      
          private String replaceAt(String text, int index, String character) {
              return text.substring(0, index) + character + text.substring(index + character.length());
          }
      
          private int getParBeforePard(String rtfFirstQc, int indexPard) {
            String text = rtfFirstQc.substring(indexPard - 6, indexPard);
            int pardQt = this.getMatches(text, "\\\\pard");
            return this.getMatches(text, "\\\\par") - pardQt;
          }
      
          private Integer getMatches(String input, String regex) {
              Pattern pattern = Pattern.compile(regex);
              Matcher matcher = pattern.matcher(input);
              int from = 0;
              int count = 0;
              while(matcher.find(from)) {
                  count++;
                  from = matcher.start() + 1;
              }
      
              return count;
          }
      

      使用方法stylizeAligmentfixCenterRightAlign方法接收到的String rtf和它的html转换后,返回的是带有align的html固定。

      我需要创建它,因为我使用 javax.swing.text.rtf 将 rtf 转换为 html。

      在我的情况下,带下划线的文本也不起作用,所以要修复它,只需输入 css 样式:

        p span u {
          text-decoration: underline;
        }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-01-03
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 2012-02-17
        相关资源
        最近更新 更多