我的HTML字符串有时会有中文单词而我的代码会在中文单词的地方显示空格
这是我的HTML看起来像这样
<html>
<body>
<div>
<table border='1' align='center' cellpadding='1' cellspacing='0'>
<tr>
<td bgcolor= '#d1eb9d' align='left' width='60%'><font size='2'><b>Answer choices</b></td>
<td bgcolor= '#d1eb9d' align='center' width='20%'><font size='2'><b>Percentage</b></td>
<td bgcolor= '#d1eb9d' align='center' width='20%' ><font size='2'><b>Responses</b></div></td>
</tr>
</div>
</td>
<td align='left' ><div>紅</div></td>
<td align='center' ><div >66.67%</div></td>
<td align='center'><div >2</div></td><
/tr>
</div>
</td>
<td align='left' ><div>黃</div></td>
<td align='center' ><div >66.67%</div></td>
<td align='center'><div >2</div></td>
</tr>
</div>
</td>
<td align='left' ><div>粉紅</div></td>
<td align='center' ><div >33.33%</div></td>
<td align='center'><div >1</div></td>
</tr>
</div>
</td>
<td align='left' ><div>藍</div></td>
<td align='center' ><div >33.33%</div></td>
<td align='center'><div >1</div></td>
</tr>
</div>
</td>
<td align='left' ><div>綠</div></td>
<td align='center' ><div >0.0%</div></td>
<td align='center'><div >0</div></td>
</tr>
<tr>
<td bgcolor= '#d1eb9d' ></td>
<td bgcolor= '#d1eb9d' align='right' ><font size='2'><i><b>Total</b></i></td>
<td bgcolor= '#d1eb9d' align='center' >3</td></tr>
</table>
</div>
</body>
</html>
这是我解析上面这个字符串的代码是
StringBuilder sb= new StringBuilder();
String startstr = "<div>"
+"<table border='1' align='center' cellpadding='1' cellspacing='0'>"
+"<tr>"
+"<td bgcolor= '#d1eb9d' align='left' width='60%'><font size='2'><b>Answer choices</b></td>"
+"<td bgcolor= '#d1eb9d' align='center' width='20%'><font size='2'><b>Percentage</b></td>"
+"<td bgcolor= '#d1eb9d' align='center' width='20%' ><font size='2'><b>Responses</b></div></td>"
+"</tr>";
String endString="<tr>"
+"<td bgcolor= '#d1eb9d' ></td>"
+"<td bgcolor= '#d1eb9d' align='right' ><font size='2'><i><b>Total</b></i></td>"
+"<td bgcolor= '#d1eb9d' align='center' >";
String finalString= "</td>"
+"</tr>"
+"</table>"
+"</div>";
sb.append(startstr);
int sum=0;
for (Map.Entry<String, String> queMap1 : qvo.getResponseMap().entrySet()){
String value=queMap1.getValue();
sum+=Integer.parseInt(value);
}
////////////////////////////////////////////////////////////////////////////System.out.println("type of question in gettable"+qvo.getType());
for (Map.Entry<String, String> queMap1 : qvo.getResponseMap().entrySet()){
String key=queMap1.getKey();
String value=queMap1.getValue();
String[] splitarr= key.split(",");
String type= qvo.getType();
sb.append("</div></td>");
sb.append("<td align='left' ><div>");
if(surveyType.equalsIgnoreCase("singlesurvey")){
sb.append(splitarr[2]);
}else sb.append(splitarr[0]);
sb.append("</div></td>");
sb.append("<td align='center' ><div >");
if(type.equalsIgnoreCase("M") || type.equalsIgnoreCase("P")){
sum=Integer.parseInt(surveyNumberResponses);
}
double percent=getPercentage(value,String.valueOf(sum));
sb.append(String.valueOf(percent)+"%");
sb.append("</div></td>");
// response
sb.append("<td align='center'><div >");
sb.append(value);
sb.append("</div></td>");
sb.append("</tr>");
}
sb.append(endString);
sb.append(String.valueOf(sum));
sb.append(finalString);
try {
htmlWorker.parse(new StringReader(sb.toString()));
} catch (IOException e) {}
上面的代码可以正确地呈现英语,并在中文单词的位置显示空格
英语图像看起来像这样
请建议我如何在PDF格式的HTML字符串中显示中文单词我使用iText api创建PDF
更新
现在我使用此代码解析上面的字符串仍显示空白
String ttfFontName=System.getProperty("user.dir")+"/ARIALUNI.TTF";
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
fontImp.register(ttfFontName);
FontFactory.setFontImp(fontImp);
InputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
worker.parseXHtml(writer, document, is,null, Charset.forName("UTF-8"), fontImp);
请先帮助我谢谢
答案 0 :(得分:0)
谢谢 BrunoLowagie 最后我得到了问题的解决方案。正如我在上面的问题中编辑的那样,在使用 XMLWorkerHelper 之后,我的PDF仍然在中文单词的位置显示空格,因为我没有指定字体(我注册了 XMLWorkerFontProvider < / strong> object)在我的HTML代码中的样式标记中。现在HTML代码看起来像这样
String str="<html><head>"
+ "<meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\"/>"
+ "</head>"
+ "<body>"
+ "<div>"
+ "<table border='1' align='center' cellpadding='5' cellspacing='0'>"
+ "<tr>"
+ "<td style='background-color:#d1eb9d;' align='left' width='40%'><font size='3'><b>Answer choices</b></font></td>"
+ "<td style='background-color:#d1eb9d; ' align='center' width='20%'><font size='3'><b>Percentage</b></font></td>"
+ "<td style='background-color:#d1eb9d; ' align='center' width='20%' ><font size='3'><b>Responses</b></font></td>"
+ "</tr>"
+ "<tr>"
+ "<td style=\"font-family: arial unicode ms, ARIALUNI; font-size:16px; font-weight: normal;\" align='left' ><div>藍</div></td>"
+ "<td style='text-align:center;' ><div >66.67%</div></td>"
+ "<td style='text-align:center;'><div >2</div></td></tr>"
+ "<tr>"
+ "<td style=\"font-family: arial unicode ms, ARIALUNI; font-size:16px; font-weight: normal;\" align='left' ><div>粉紅</div></td>"
+ "<td style='text-align:center;' ><div >66.67%</div></td>"
+ "<td style='text-align:center;' ><div >2</div></td>"
+ "</tr>"
+ "<tr>"
+ "<td style=\"font-family: arial unicode ms, ARIALUNI; fkont-size:16px; font-weight: normal;\" align='left' ><div>apple</div></td>"
+ "<td style='text-align:center;' ><div >33.33%</div></td>"
+ "<td style='text-align:center;'><div >1</div></td>"
+ "</tr>"
+ "<tr>"
+ "<td style=\"font-family: arial unicode ms, ARIALUNI; font-size:16px; font-weight: normal;\" align='left' ><div>紅</div></td>"
+ "<td style='text-align:center;' ><div >33.33%</div></td>"
+ "<td style='text-align:center;'><div >1</div></td>"
+ "</tr>"
+ "<tr>"
+ "<td align='left' style=\"font-family: arial unicode ms, ARIALUNI; font-size:16px; font-weight: normal;\" ><div>綠</div></td>"
+ "<td style='text-align:center;' ><div >0.0%</div></td>"
+ "<td style='text-align:center;'><div >0</div></td>"
+ "</tr>"
+ "<tr>"
+ "<td style='background-color:#d1eb9d;' ></td>"
+ "<td style='background-color:#d1eb9d;' align='right' ><font size='3'><i><b>Total</b></i></font></td>"
+ "<td style='background-color:#d1eb9d;' align='center' >3</td></tr>"
+ "</table>"
+ "</div>"
+ "</body>"
+ "</html>";
和Java代码将如下所示
String ttfFileName=System.getProperty("user.dir")+"/ARIALUNI.TTF";
XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
XMLWorkerHelper worker = XMLWorkerHelper.getInstance();
fontImp.register(ttfFileName);
FontFactory.setFontImp(fontImp);
InputStream is = new ByteArrayInputStream(sb.toString().getBytes("UTF-8"));
worker.parseXHtml(writer, document, is,null, Charset.forName("UTF-8"), fontImp);
谢谢你这个帖子帮助别人。