4

我正在使用Java & Itext2.1.7生成pdf,并且我编写了以下HTML。但是,当它们太长时,它不是破坏词。

String td ="<td colspan=\"2\" align=\"left\" style=\"table-layout: fixed;word-wrap:break-word;white-space: normal;\"><b>"
                    + verylongwordverylongwordverylongword + ":</b></td>"

有人可以帮我实现这一目标吗?

4

1 回答 1

5

Itext 2.7.1不支持word-wrap : break-word属性。以下是该链接。

http://demo.itextsupport.com/xmlworker/itextdoc/flatsite.html

所以我根据表格的列数手动打破了单词,并假设最大字母在 60-65 左右。以下是代码。

public String getBrokenWordsForPdfGeneration(String longString, int cutIndex){
       StringBuffer mainStringBuffer = new StringBuffer(longString);
       String returnString = "";
       while(mainStringBuffer != null && mainString.length()>0 &&                              
mainString.subString(0,mainString.length()>cutIndex?cutIndex:mainString.length()) != null){ 
          if(mainStrig.subString(0,mainString.length()>cutIndex:mainString.length()).trim().contains(" ")){
               returnString += mainStrig.subString(0,mainString.length()>cutIndex:mainString.length()).trim();
               mainStringBuffer.delete(0,mainString.length()>cutIndex:mainString.length());
          }else{
              returnString += mainStrig.subString(0,mainString.length()>cutIndex:mainString.length()).trim()+ " ";
              mainStringBuffer.delete(0,mainString.length()>cutIndex:mainString.length());
          }
    }
    return returnString;
}
于 2012-08-02T06:33:44.543 回答