解决方法有两种:
第1种:
<body>
<textarea id='t' rows=20 cols=20></textarea>
<script>
document.getElementById('t').innerText='a<&>'
</script>
</body>
?
第2种:
/*将字串转为html格式*/
public String strToHtml(String s)
{
if (s==null||s.equals("")) return ""
s = s.replaceAll("&", "&")
s = s.replaceAll("<", "<")
s = s.replaceAll(">", ">")
s = s.replaceAll(" ", " ")
// s = s.replaceAll("/n", "<br/>")
// s = s.replaceAll("'", "'")
return s
}
/*将html格式转为字串*/
public String strToHtml(String s)
{
if (s==null||s.equals("")) return ""
s = s.replaceAll("&","&")
s = s.replaceAll("<","<")
s = s.replaceAll(">",">")
s = s.replaceAll(" "," ")
//s = s.replaceAll("<br/>","/n")
//s = s.replaceAll("'","'")
return s
}
1、首先导入itext的jar包,private static void versionsone(String htmlCode, String pdfPath){
Document document = new Document()
try{
StyleSheet st = new StyleSheet()
st.loadTagStyle("body", "leading", "16,0")
PdfWriter.getInstance(document, new FileOutputStream(pdfPath))
document.open()
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED)
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL)
ArrayList<ArrayList>p = HTMLWorker.parseToList(dome_2(htmlCode), st)
for(int k=0k<p.size()k++){
for(int m=0m<p.get(k).size()m++){
Paragraph pCode = new Paragraph(p.get(k).get(m).toString(),FontChinese)
document.add(pCode)
}
}
}catch(Exception e){
e.printStackTrace()
}finally{
document.close()
}
}
private static Reader dome_2(String htmlCode){
if(htmlCode != null &&!htmlCode.trim().equals("")){
try{
InputStream tInputStringStream = new ByteArrayInputStream(htmlCode.getBytes())
InputStreamReader isr = new InputStreamReader(tInputStringStream,"utf-8")
BufferedReader reader = new BufferedReader(isr)
return reader
}catch(Exception e){
e.printStackTrace()
}
}
return null
}
其实此处和别人的最大的不一样的是,
ArrayList<ArrayList>p = HTMLWorker.parseToList(dome_2(htmlCode), st)
其实刚刚开始我在网上找来的代码中是这样写的“ArrayList p = HTMLWorker.parseToList(dome_2(htmlCode), st)”
但后来我通过debug发现,ArrayList 里面装的还是一个集合,然后集合中放得才是真正不含html标签的字符串。对于普通得文章,一般html标签一般都是<p></p>
占大多数。所以我直接用了两层for循环直接从里面取得我们想要的字符串。然后直接“Paragraph pCode = new Paragraph(p.get(k).get(m).toString(),FontChinese)”document.add(pCode)这样就能解决中文问题了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)