HSLFTextRuntrs_item=trs.get(l);
content=String.format("%s%s\n",content,trs_item.getRawText());
}
}
}
contentArray.add(content);
}
}catch(Exceptione){
e.printStackTrace();
}
returncontentArray;
}
读取图文样式
poi方式只能有效读取ppt内部的文字信息,对于ppt内带的图片以及文字样式,便力有不逮了。
在博文《Android开发笔记(一百四十)Word文件的读取与显示》中,提到可以解析docx内部的document.xml文件,从xml标记中获取图片信息与样式信息,然后把图文格式构造成html文件,最后由WebView网页视图加载显示html。
对于pptx文件,也可以解析pptx内部的slide*.xml幻灯片文件,采用跟解析docx类似的做法,把解析得到的图片与样式数据写入到html文件,从而曲线实现了pptx文件的读取功能。
下面是以HTML格式显示pptx文件的效果图:
下面是解析pptx并生成htmml文件的主要代码:
[java]viewplaincopy
privatevoidreadPPTX(StringpptPath){
try{
ZipFilepptxFile=newZipFile(newFile(pptPath));
intpic_index=1;//pptx中的图片名从image1开始,所以索引从1开始
for(inti=1;i<100;i++){//最多支持100张幻灯片
StringfilePath=String.format("%s%d.html",FileUtil.getFileName(pptPath),i);
StringhtmlPath=FileUtil.createFile("html",filePath);
Log.d(TAG,"i="+i+",htmlPath="+htmlPath);
output=newFileOutputStream(newFile(htmlPath));
presentPicture=0;
output.write(htmlBegin.getBytes());
ZipEntrysharedStringXML=pptxFile.getEntry("ppt/slides/slide"+i+".xml");//获取每张幻灯片
InputStreaminputStream=pptxFile.getInputStream(sharedStringXML);
XmlPullParserxmlParser=Xml.newPullParser();
xmlParser.setInput(inputStream,"utf-8");
booleanisTitle=false;//标题
booleanisTable=false;//表格
booleanisSize=false;//文字大小
booleanisColor=false;//文字颜色
booleanisCenter=false;//居中对齐
booleanisRight=false;//靠右对齐
booleanisItalic=false;//斜体
booleanisUnderline=false;//下划线
booleanisBold=false;//加粗
intevent_type=xmlParser.getEventType();//得到标签类型的状态
while(event_type!
=XmlPullParser.END_DOCUMENT){//循环读取流
switch(event_type){
caseXmlPullParser.START_TAG:
//开始标签
StringtagBegin=xmlParser.getName();
if(tagBegin.equalsIgnoreCase("ph")){//判断是否标题
StringtitleType=getAttrValue(xmlParser,"type","text");
if(titleType.equals("text")){
isTitle=false;
}else{
isTitle=true;
isSize=true;
if(titleType.equals("ctrTitle")){
output.write(centerBegin.getBytes());
isCenter=true;
output.write(String.format(fontSizeTag,getSize(60)).getBytes());
}elseif(titleType.equals("subTitle")){
output.write(centerBegin.getBytes());
isCenter=true;
output.write(String.format(fontSizeTag,getSize(24)).getBytes());
}elseif(titleType.equals("title")){
output.write(String.format(fontSizeTag,getSize(44)).getBytes());
}
}
}
if(tagBegin.equalsIgnoreCase("pPr")&&!
isTitle){//判断对齐方式
Stringalign=getAttrValue(xmlParser,"algn","l");
xmlParser.getAttributeValue(0);
if(uals("ctr")){
output.write(centerBegin.getBytes());
isCenter=true;
}
if(align.equals("r")){
output.write(divRight.getBytes());
isRight=true;
}
}
if(tagBegin.equalsIgnoreCase("srgbClr")){//判断文字颜色
Stringcolor=xmlParser.getAttributeValue(0);
output.write(String.format(spanColor,color).getBytes());
isColor=true;
}
if(tagBegin.equalsIgnoreCase("rPr")){
if(!
isTitle){
//判断文字大小
StringsizeStr=getAttrValue(xmlParser,"sz","2800");
intsize=getSize(Integer.valueOf(sizeStr)/100);
output.write(String.format(fontSizeTag,size).getBytes());
isSize=true;
}
//检测到加粗
StringbStr=getAttrValue(xmlParser,"b","");
if(bStr.equals("1")){
isBold=true;
}
//检测到斜体
StringiStr=getAttrValue(xmlParser,"i","");
if(iStr.equals("1")){
isItalic=true;
}
//检测到下划线
StringuStr=getAttrValue(xmlParser,"u","");
if(uStr.equals("sng")){
isUnderline=true;
}
}
if(tagBegin.equalsIgnoreCase("tbl")){//检测到表格
output.write(tableBegin.getBytes());
isTable=true;
}elseif(tagBegin.equalsIgnoreCase("tr")){//表格行
output.write(rowBegin.getBytes());
}elseif(tagBegin.equalsIgnoreCase("tc")){//表格列
output.write(columnBegin.getBytes());
}
if(tagBegin.equalsIgnoreCase("pic")){//检测到图片
ZipEntrypic_entry=FileUtil.getPicEntry(pptxFile,"ppt",pic_index);
if(pic_entry!
=null){
byte[]pictureBytes=FileUtil.getPictureBytes(pptxFile,pic_entry);
writeDocumentPicture(i,pictureBytes);
}
pic_index++;//转换一张后,索引+1
}
if(tagBegin.equalsIgnoreCase("p")&&!
isTable){//检测到段落,如果在表格中就无视
output.write(lineBegin.getBytes());
}
//检测到文本
if(tagBegin.equalsIgnoreCase("t")){
if(isBold==true){//加粗
output.write(boldBegin.getBytes());
}
if(isUnderline==true){//检测到下划线,输入
output.write(underlineBegin.getBytes());
}
if(isItalic==true){//检测到斜体,输入
output.write(italicBegin.getBytes());
}
Stringtext=xmlParser.nextText();
output.write(text.getBytes());//写入文本
if(isItalic==true){//输入斜体结束标签
output.write(italicEnd.getBytes());
isItalic=false;
}
if(isUnderline==true){//输入下划线结束标签
output.write(underlineEnd.getBytes());
isUnderline=false;
}
if(isBold==true){//输入加粗结束标签
output.write(boldEnd.getBytes());
isBold=false;
}
if(isSize==true){//输入字体结束标签
output.write(fontEnd.getBytes());
isSize=false;
}
if(isColor==true){//输入跨度结束标签
output.write(spanEnd.getBytes());
isColor=false;
}
//if(isCenter==true){//输入居中结束标签。
要在段落结束之前再输入该标签,因为该标签会强制换行
//output.write(centerEnd.getBytes());
//isCenter=false;
//}
if(isRight==true){//输入区块结束标签