Java实现word文档在线预览,读取office文件

时间:2023-03-09 15:34:07
Java实现word文档在线预览,读取office文件

想要实现word或者其他office文件的在线预览,大部分都是用的两种方式,一种是使用openoffice转换之后再通过其他插件预览,还有一种方式就是通过POI读取内容然后预览。

一、使用openoffice方式实现word预览

主要思路是:

1.通过第三方工具openoffice,将word、excel、ppt、txt等文件转换为pdf文件

2.通过swfTools将pdf文件转换成swf格式的文件

3.通过FlexPaper文档组件在页面上进行展示

我使用的工具版本:

openof:3.4.1

swfTools:1007

FlexPaper:这个关系不大,我随便下的一个。推荐使用1.5.1

JODConverter:需要jar包,如果是maven管理直接引用就可以

操作步骤:

1.office准备

下载openoffice:

http://www.openoffice.org/download/index.html

从过往文件,其他语言中找到中文版3.4.1的版本

下载后,解压缩,安装

然后找到安装目录下的program 文件夹

在目录下运行

soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard

如果运行失败,可能会有提示,那就加上   .\   在运行试一下

这样openoffice的服务就开启了。

2.

将flexpaper文件中的js文件夹(包含了flexpaper_flash_debug.js,flexpaper_flash.js,jquery.js,这三个js文件主要是预览swf文件的插件)拷贝至网站根目录;将FlexPaperViewer.swf拷贝至网站根目录下(该文件主要是用在网页中播放swf文件的播放器)

项目结构:

Java实现word文档在线预览,读取office文件

页面代码:

fileUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>文档在线预览系统</title>
<style>
body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;}
a {color:#CE4614;}
#msg-box {color: #CE4614; font-size:0.9em;text-align:center;}
#msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;}
#msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;}
#msg-box .nav {margin-top:20px;}
</style> </head>
<body>
<div id="msg-box">
<form name="form1" method="post" enctype="multipart/form-data" action="docUploadConvertAction.jsp">
<div class="title">
请上传要处理的文件,过程可能需要几分钟,请稍候片刻。
</div>
<p>
<input name="file1" type="file">
</p>
<p>
<input type="submit" name="Submit" value="上传">
</p>
</form >
</div>
</body>
</html>

docUploadConvertAction.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>  

<%@page import="java.io.*"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="filetest.DocConverter"%>
<%
//文件上传采用cos组件上传,可更换为commons-fileupload上传,文件上传后,保存在upload文件夹
//获取文件上传路径
String saveDirectory =application.getRealPath("/")+"upload";
//打印上传路径信息
System.out.println(saveDirectory);
//每个文件最大50m
int maxPostSize = 50 * 1024 * 1024 ;
//采用cos缺省的命名策略,重名后加1,2,3...如果不加dfp重名将覆盖
DefaultFileRenamePolicy dfp = new DefaultFileRenamePolicy();
//response的编码为"UTF-8",同时采用缺省的文件名冲突解决策略,实现上传,如果不加dfp重名将覆盖
MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8",dfp);
//MultipartRequest multi = new MultipartRequest(request, saveDirectory, maxPostSize,"UTF-8");
//输出反馈信息
Enumeration files = multi.getFileNames();
while (files.hasMoreElements()) {
System.err.println("ccc");
String name = (String)files.nextElement();
File f = multi.getFile(name);
if(f!=null){
String fileName = multi.getFilesystemName(name);
//获取上传文件的扩展名
String extName=fileName.substring(fileName.lastIndexOf(".")+1);
//文件全路径
String lastFileName= saveDirectory+"\\" + fileName;
//获取需要转换的文件名,将路径名中的'\'替换为'/'
String converfilename = saveDirectory.replaceAll("\\\\", "/")+"/"+fileName;
System.out.println(converfilename);
//调用转换类DocConverter,并将需要转换的文件传递给该类的构造方法
DocConverter d = new DocConverter(converfilename);
//调用conver方法开始转换,先执行doc2pdf()将office文件转换为pdf;再执行pdf2swf()将pdf转换为swf;
d.conver();
//调用getswfPath()方法,打印转换后的swf文件路径
System.out.println(d.getswfPath());
//生成swf相对路径,以便传递给flexpaper播放器
String swfpath = "upload"+d.getswfPath().substring(d.getswfPath().lastIndexOf("/"));
System.out.println(swfpath);
//将相对路径放入sessio中保存
session.setAttribute("swfpath", swfpath);
out.println("上传的文件:"+lastFileName);
out.println("文件类型"+extName);
out.println("<hr>");
}
} %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
body {margin-top:100px;background:#fff;font-family: Verdana, Tahoma;}
a {color:#CE4614;}
#msg-box {color: #CE4614; font-size:0.9em;text-align:center;}
#msg-box .logo {border-bottom:5px solid #ECE5D9;margin-bottom:20px;padding-bottom:10px;}
#msg-box .title {font-size:1.4em;font-weight:bold;margin:0 0 30px 0;}
#msg-box .nav {margin-top:20px;}
</style>
</head>
<body>
<div>
<form name="viewForm" id="form_swf" action="documentView.jsp" method="POST">
<input type='submit' value='预览' class='BUTTON SUBMIT'/>
</form>
</div>
</body>
</html>

documentView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String swfFilePath=session.getAttribute("swfpath").toString();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/flexpaper_flash.js"></script>
<script type="text/javascript" src="js/flexpaper_flash_debug.js"></script>
<style type="text/css" media="screen">
html, body { height:100%; }
body { margin:0; padding:0; overflow:auto; }
#flashContent { display:none; }
</style> <title>文档在线预览系统</title>
</head>
<body>
<div style="position:absolute;left:50px;top:10px;">
<a id="viewerPlaceHolder" style="width:820px;height:650px;display:block"></a> <script type="text/javascript">
var fp = new FlexPaperViewer(
'FlexPaperViewer',
'viewerPlaceHolder', { config : {
SwfFile : escape('<%=swfFilePath%>'),
Scale : 0.6,
ZoomTransition : 'easeOut',
ZoomTime : 0.5,
ZoomInterval : 0.2,
FitPageOnLoad : true,
FitWidthOnLoad : false,
FullScreenAsMaxWindow : false,
ProgressiveLoading : false,
MinZoomSize : 0.2,
MaxZoomSize : 5,
SearchMatchAll : false,
InitViewMode : 'SinglePage', ViewModeToolsVisible : true,
ZoomToolsVisible : true,
NavToolsVisible : true,
CursorToolsVisible : true,
SearchToolsVisible : true, localeChain: 'en_US'
}});
</script>
</div>
</body>
</html>

转换类:

DocConverter.java

package filetest;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream; import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter; /**
* doc docx格式转换
*/
public class DocConverter {
private static final int environment = 1;// 环境 1:windows 2:linux
private String fileString;// (只涉及pdf2swf路径问题)
private String outputPath = "";// 输入路径 ,如果不设置就输出在默认的位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile; public DocConverter(String fileString) {
ini(fileString);
} /**
* 重新设置file
*
* @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
} /**
* 初始化
*
* @param fileString
*/
private void ini(String fileString) {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
} /**
* 转为PDF
*
* @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath()+ "****");
} catch (java.net.ConnectException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
} /**
* 转换成 swf
*/
@SuppressWarnings("unused")
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1) {// windows环境处理
try {
Process p = r.exec("D:/pdf2swf/pdf2swf.exe "+ pdfFile.getPath() + " -o "+ swfFile.getPath() + " -T 9");
//Process p=new Process();
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
} } catch (IOException e) {
e.printStackTrace();
throw e;
}
} else if (environment == 2) {// linux环境处理
try {
Process p = r.exec("pdf2swf " + pdfFile.getPath()
+ " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:"
+ swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已经存在不需要转换****");
}
} static String loadStream(InputStream in) throws IOException { int ptr = 0;
in = new BufferedInputStream(in);
StringBuffer buffer = new StringBuffer(); while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
} return buffer.toString();
}
/**
* 转换主方法
*/
@SuppressWarnings("unused")
public boolean conver() { if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
} if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}
try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
e.printStackTrace();
return false;
} if (swfFile.exists()) {
return true;
} else {
return false;
}
} /**
* 返回文件路径
*
* @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
} }
/**
* 设置输出路径
*/
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
if (!outputPath.equals("")) {
String realName = fileName.substring(fileName.lastIndexOf("/"),
fileName.lastIndexOf("."));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + ".swf");
} else {
swfFile = new File(outputPath + realName + ".swf");
}
}
} }

然后运行。

中间遇到的问题,存放上传的upload文件夹要创建。

存放pdf2swf的位置的代码有问题,不知道为什么我第一次运行失败后,修改为正确地址,然后重启重写了很多次还是报错地址错误,最后将推荐哦那个样的代码放到新的项目里却可以正常运行不报错。

运行结果:

Java实现word文档在线预览,读取office文件

二、利用jodconverter(基于OpenOffice服务)将文件转化为html格式或者pdf格式(不成熟):

package org.pan.poi.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.text.SimpleDateFormat;
import java.util.Date; import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
/**
* @包名 org.pan.poi.poi
* @创建人 蒋文章
* @日期 2018/4/23 0023
* @时间 15:39
* @描述
* 利用jodconverter(基于OpenOffice服务)将文件(*.doc、*.docx、*.xls、*.ppt)转化为html格式或者pdf格式,
* 使用前请检查OpenOffice服务是否已经开启, OpenOffice进程名称:soffice.exe | soffice.bin
*/
public class Doc2HtmlUtil { private static Doc2HtmlUtil doc2HtmlUtil; /**
* 获取Doc2HtmlUtil实例
*/
public static synchronized Doc2HtmlUtil getDoc2HtmlUtilInstance() {
if (doc2HtmlUtil == null) {
doc2HtmlUtil = new Doc2HtmlUtil();
}
return doc2HtmlUtil;
} /**
* 转换文件成html
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2Html(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".html";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".html";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".html";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".html";
}else{
return null;
} File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream构建输入文件
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
} os.close();
fromFileInputStream.close();
} catch (IOException e) {
} OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 转换完之后删除word文件
docInputFile.delete();
return htmFileName;
} /**
* 转换文件成pdf
*
* @param fromFileInputStream:
* @throws IOException
*/
public String file2pdf(InputStream fromFileInputStream, String toFilePath,String type) throws IOException {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String timesuffix = sdf.format(date);
String docFileName = null;
String htmFileName = null;
if("doc".equals(type)){
docFileName = "doc_" + timesuffix + ".doc";
htmFileName = "doc_" + timesuffix + ".pdf";
}else if("docx".equals(type)){
docFileName = "docx_" + timesuffix + ".docx";
htmFileName = "docx_" + timesuffix + ".pdf";
}else if("xls".equals(type)){
docFileName = "xls_" + timesuffix + ".xls";
htmFileName = "xls_" + timesuffix + ".pdf";
}else if("ppt".equals(type)){
docFileName = "ppt_" + timesuffix + ".ppt";
htmFileName = "ppt_" + timesuffix + ".pdf";
}else{
return null;
} File htmlOutputFile = new File(toFilePath + File.separatorChar + htmFileName);
File docInputFile = new File(toFilePath + File.separatorChar + docFileName);
if (htmlOutputFile.exists())
htmlOutputFile.delete();
htmlOutputFile.createNewFile();
if (docInputFile.exists())
docInputFile.delete();
docInputFile.createNewFile();
/**
* 由fromFileInputStream构建输入文件
*/
try {
OutputStream os = new FileOutputStream(docInputFile);
int bytesRead = 0;
byte[] buffer = new byte[1024 * 8];
while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
} os.close();
fromFileInputStream.close();
} catch (IOException e) {
} OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
} catch (ConnectException e) {
System.err.println("文件转换出错,请检查OpenOffice服务是否启动。");
}
// convert
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docInputFile, htmlOutputFile);
connection.disconnect();
// 转换完之后删除word文件
docInputFile.delete();
return htmFileName;
} public static void main(String[] args) throws IOException {
Doc2HtmlUtil coc2HtmlUtil = getDoc2HtmlUtilInstance();
File file = null;
FileInputStream fileInputStream = null; /* file = new File("D:/poi-test/exportExcel.xls");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/xls","xls");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/xls","xls");*/ file = new File("F:/test/JavaWeb.doc");
fileInputStream = new FileInputStream(file);
coc2HtmlUtil.file2Html(fileInputStream, "F:/test","doc");
// coc2HtmlUtil.file2pdf(fileInputStream, "F:/test","doc"); /* file = new File("D:/poi-test/周报模版.ppt");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/ppt","ppt");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/ppt","ppt"); file = new File("D:/poi-test/test.docx");
fileInputStream = new FileInputStream(file);
// coc2HtmlUtil.file2Html(fileInputStream, "D:/poi-test/openOffice/docx","docx");
coc2HtmlUtil.file2pdf(fileInputStream, "D:/poi-test/openOffice/docx","docx");*/ } }

三、使用poi读取word内容(不成熟):

package org.pan.poi.poi;

import java.io.File;
import java.io.FileInputStream;
import java.io.OutputStream; import org.apache.commons.io.FileUtils;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun; /**
* @author panmingshuai
* @description
* @Time 2018年4月2日 下午5:46:30
*
*/
public class WordTest {
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception {
/**
* 读取doc文档
* */
FileInputStream in = FileUtils.openInputStream(new
File("F:\\物流知识点.doc"));
HWPFDocument doc = new HWPFDocument(in);
Range range = doc.getRange();
for(int i=0; i<range.numParagraphs(); i++){
System.out.println(
range.getParagraph(i).text());
}
String docText = range.text();
System.out.println(docText);
in.close(); /**
* 读取docx文档
*/
// FileInputStream in = FileUtils.openInputStream(new
// File("F:\\doc和docx的区别.docx"));
// XWPFDocument doc = new XWPFDocument(in);
// for(XWPFParagraph paragraph : doc.getParagraphs()){
// System.out.println("-------------" + paragraph.getText());
// }
// XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
// System.out.println(extractor.getText()); // FileInputStream in = FileUtils.openInputStream(new
// //File("F:\\物流知识点.docx"));
// File("http://120.220.22.20:8000/group1/M00/00/01/CoDIiVp7n3KAD6hTABByAASZAsg816.doc"));
// XWPFDocument doc = new XWPFDocument(in);
// for(XWPFParagraph paragraph : doc.getParagraphs()){
// System.out.println("-------------" + paragraph.getText());
// }
// XWPFWordExtractor extractor = new XWPFWordExtractor(doc);
// System.out.println(extractor.getText()); /**
* 写入docx文档
*/
/*XWPFDocument doc = new XWPFDocument();
XWPFParagraph para = doc.createParagraph();
//首行缩进
para.setIndentationFirstLine(1000);
// 一个XWPFRun代表具有相同属性的一个区域。
XWPFRun run = para.createRun();
run.setBold(true); // 加粗
run.setText("加粗的内容");
run = para.createRun();
run.setColor("FF0000");
run.setText("红色的字。");
run = para.createRun();
run.setFontSize(17);
run.setText("17号的字");
OutputStream os = FileUtils.openOutputStream(new File("E:\\qwe123.docx"));
doc.write(os);
os.close();*/ /**
* XWPFParagraph设置样式:
*
*
* //setAlignment()指定应适用于此段落中的文本的段落对齐方式。CENTER LEFT...
* //p1.setAlignment(ParagraphAlignment.LEFT);
* //p1.setBorderBetween(Borders.APPLES);
* //p1.setBorderBottom(Borders.APPLES);
* //p1.setBorderLeft(Borders.APPLES);指定应显示在左边页面指定段周围的边界。
* //p1.setBorderRight(Borders.ARCHED_SCALLOPS);指定应显示在右侧的页面指定段周围的边界。
* //p1.setBorderTop(Borders.ARCHED_SCALLOPS);指定应显示上方一组有相同的一组段边界设置的段落的边界。这几个是对段落之间的格式的统一,相当于格式刷
* //p1.setFirstLineIndent(99);//---正文宽度会稍微变窄
* //p1.setFontAlignment(1);//---段落的对齐方式 1左 2中 3右 4往上 左 不可写0和负数
* //p1.setIndentationFirstLine(400);//---首行缩进,指定额外的缩进,应适用于父段的第一行。
* //p1.setIndentationHanging(400);//---首行前进,指定的缩进量,应通过第一行回到开始的文本流的方向上移动缩进从父段的第一行中删除。
* //p1.setIndentationLeft(400);//---整段缩进(右移)指定应为从左到右段,该段的内容的左边的缘和这一段文字左边的距和右边文本边距和左段权中的那段文本的右边缘之间的缩进,
* 如果省略此属性,则应假定其值为零。
* //p1.setIndentationRight(400);//---指定应放置这一段,该段的内容从左到右段的右边缘的正确文本边距和右边文本边距和左段权中的那段文本的右边缘之间的缩进,
* 如果省略此属性,则应假定其值为零。
* //p1.setIndentFromLeft(400);//---整段右移
* //p1.setIndentFromRight(400);
* //p1.setNumID(BigInteger.TEN);
* //p1.setPageBreak(true);//--指定当渲染此分页视图中的文档,这一段的内容都呈现在文档中的新页的开始。
* //p1.setSpacingAfter(6);//--指定应添加在文档中绝对单位这一段的最后一行之后的间距。
* //p1.setSpacingAfterLines(6);//--指定应添加在此线单位在文档中的段落的最后一行之后的间距。
* //p1.setSpacingBefore(6);//--指定应添加上面这一段文档中绝对单位中的第一行的间距。
* //p1.setSpacingBeforeLines(6);//--指定应添加在此线单位在文档中的段落的第一行之前的间距。
* //p1.setSpacingLineRule(LineSpacingRule.AT_LEAST);//--指定行之间的间距如何计算存储在行属性中。
* //p1.setStyle("");//--此方法提供了样式的段落,这非常有用.
* //p1.setVerticalAlignment(TextAlignment.CENTER);//---指定的文本的垂直对齐方式将应用于此段落中的文本
* //p1.setWordWrapped(true);//--此元素指定是否消费者应中断超过一行的文本范围,通过打破这个词 (打破人物等级) 的两行或通过移动到下一行 (在词汇层面上打破) 这个词的拉丁文字。
* //XWPFRun r1=p1.createRun();//p1.createRun()将一个新运行追加到这一段
* //setText(String value)或setText(String value,int pos)
* //r1.setText(data);
* //r1.setTextPosition(20);//这个相当于设置行间距的,具体这个20是怎么算的,不清楚,此元素指定文本应为此运行在关系到周围非定位文本的默认基线升降的量。
* 不是真正意义上的行间距
* //r1.setStrike(true);//---设置删除线的,坑人!!!
* //r1.setStrikeThrough(true);---也是设置删除线,可能有细微的区别吧
* //r1.setEmbossed(true);---变的有重影(变黑了一点)
* //r1.setDoubleStrikethrough(true);---设置双删除线
* //r1.setColor("33CC00");//---设置字体颜色 ★
* //r1.setFontFamily("fantasy");
* //r1.setFontFamily("cursive");//---设置ASCII(0 - 127)字体样式
* //r1.setBold(jiacu);//---"加黑加粗"
* //r1.setFontSize(size);//---字体大小
* //r1.setImprinted(true);//感觉与setEmbossed(true)类似,有重影
* //r1.setItalic(true);//---文本会有倾斜,是一种字体?
* //r1.setShadow(true);//---文本会变粗有重影,与前面两个有重影效果的方法感觉没什么区别
* //r1.setSmallCaps(true);//---改变了 英文字母 的格式
* //r1.setSubscript(VerticalAlign.BASELINE);//---valign垂直对齐的
* //r1.setUnderline(UnderlinePatterns.DASH);//--填underline type设置下划线
* //document.createTable(2, 2);//--创建一个制定行列的表
* //document.enforceReadonlyProtection();//--强制执行制度保护
* //r1.setDocumentbackground(doc, "FDE9D9");//设置页面背景色
* //r1.testSetUnderLineStyle(doc);//设置下划线样式以及突出显示文本
* //r1.addNewPage(doc, BreakType.PAGE);
* //r1.testSetShdStyle(doc);//设置文字底纹
*/
}
}