Java实现在线预览以及⽔印--openOffice openoffice的安装
代码展⽰
//我是将⽂件传到了hdfs,这⾥是引⽤hdfs的服务读取⽂件流
@Autowired
BaFileService baFileService;
@Autowired
FileServiceClient fileServiceClient
public static final String waterMark ="";
public static final String pdfPath ="";
public static final String fontPath ="";
/**
*⽂件预览,这⾥是将⽂件统⼀转换成了pdf⽅便预览
**/
@RequestMapping(value ="/preview", method ={ RequestMethod.POST,
RequestMethod.GET })
public void preview(HttpServletRespon respon,
@RequestParam("uuid") String uuid,@RequestParam("token") String token)throws Exception {
FileDto file = (uuid);
//⽔印⽂字地址
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/src/module/library/"+waterMark);
//读取⽔印⽂字内容
String waterMark = adContent(is);
//pdf存放地址
InputStream pdfIs = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/src/module/library/"+pdfPath);
//读取pdf存放地址内容
String pdfPath = adContent(pdfIs);
//新建⼀个pdf的⽂件地址
String pDFPath = pdfPath+UUID.randomUUID().toString()+".pdf";
//读取字体
InputStream fontIs = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/src/module/library/"+fontPath); String fontPath = adContent(fontIs);
File file = null;
String path = Path().substring(1));
InputStream in = ad(path);
//因为pdfReader不能从hdfs读取,所以我选择了下载copy⼀份⽂件到本地
String copyPath = pdfPath+UUID.randomUUID().toString()+"."+Suffix();
if("csv".Suffix())||"txt".Suffix())){
LOGGER.info("后缀名1:"+Suffix());
copyFile(in, copyPath);
}el{
LOGGER.info("后缀名2:"+Suffix());
copyFile1(in, copyPath);
}
LOGGER.info("开始转换");
if(!"pdf".Suffix())){
//如果不是pdf后缀的⽂件,就转换成pdf
file = PreviewUtil.openOfficeToPDF(copyPath);
LOGGER.info("转换成功");
}
//如果不需要⽔印,后⾯的代码可以忽略
BufferedInputStream br;
OutputStream out;
Field f;
int len;
byte[] b =new byte[1024];
try{
try{
PdfReader reader;
if("pdf".Suffix())){
reader =new PdfReader(copyPath);
}el{
reader =new AbsolutePath());
}
f = Class().getDeclaredField("encrypted");
f.tAccessible(true);
f.t(reader,fal);
// 加完⽔印的⽂件
PdfStamper stamper =new PdfStamper(reader,new FileOutputStream(pDFPath));
int total = NumberOfPages()+1;
PdfContentByte content;
// 设置字体
BaFont ba = ateFont(fontPath, BaFont.IDENTITY_H,BaFont.NOT_EMBEDDED); // 循环对每页插⼊⽔印
for(int i =1; i < total; i++){
Document document =new PageSize(i));
LOGGER.info("aaa");
// 在内容上⽅加⽔印
//content = OverContent(i);
content = UnderContent(i);
// 开始
content.beginText();
// 设置颜⾊
content.tColorFill(Color.lightGray);
// 设置字体及字号
content.tFontAndSize(ba,26);
// 页⾯宽度
float x = PageSize().getWidth();
// 页⾯⾼度
float y = PageSize().getHeight();
// 设置起始位置
//x轴⾃增200横向⽔印
int a =0;
int c =0;
int z =0;
LOGGER.info("bbb");
for(a =0;a<y;){
for(c = z;a<y;){
content.showTextAligned(Element.ALIGN_CENTER, waterMark, c, a,45);
c= c+200;
a= a+200;
}
if(z<x){
a =0;
c =0;
z = z+200;
}
}
//y轴⾃增200横向⽔印
z =0;
a =0;
for(c =200;a<y;){
for(a = z;a<y;){
content.showTextAligned(Element.ALIGN_CENTER, waterMark, c, a,45);
c = c+200;
a = a+200;
}
if(z<y){
a =0;
c =0;
z = z+200;
z = z+200;
}
}
}
stamper.clo();
LOGGER.Filename());
out = OutputStream();
br =new BufferedInputStream(new FileInputStream(pDFPath));
<();
respon.tContentType("application/octet-stream;chart = utf-8");
respon.tHeader("Content-Disposition",
"inline; filename="+ Filename()+".pdf","UTF-8"));
while((len = br.read(b))!=-1){
督查工作方案out.write(b,0, len);
}
br.clo();
out.clo();
}catch(IOException e){
e.printStackTrace();
}catch(SecurityException e){
e.printStackTrace();
}catch(IllegalArgumentException e){
e.printStackTrace();
}catch(DocumentException e){
e.printStackTrace();
}
}
/**
*⽂件复制
**/
public static void copyFile(InputStream in,String path){
try
{
忧愁的诗句
BufferedReader isr =new BufferedReader(new InputStreamReader(in,"GB2312"));// 读取int len =0;
FileOutputStream fileOutputStream =new FileOutputStream(path);
OutputStreamWriter osw =new OutputStreamWriter(fileOutputStream,"UTF-8");
while((len = ad())!=-1)
{
osw.write(len);
创造力英语
}
in.clo();
osw.clo();
fileOutputStream.clo();
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e1){
System.out.println("复制单个⽂件出错");
e1.printStackTrace();
}
}
public class ReadContentUtil {
/**
*通过流获取内容
**/
public static String readContent(InputStream is){
String content ="";
try{
//包装流
BufferedInputStream bis =new BufferedInputStream(is);
BufferedInputStream bis =new BufferedInputStream(is);
//读取⽂件内容
byte[] b =new byte[bis.available()];
content =new String(b);
//System.out.println(new String(b));//可以得到中⽂
bis.clo();
}catch(FileNotFoundException e){
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e){
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
}
/**
*openoffice的转换
**/
public static final String utilPath ="";
private static Random random =new Random();
private static final Logger LOGGER = LoggerFactory
.getLogger(PreviewUtil.class);
//private static OfficeManager officeManager;
public static File openOfficeToPDF(String inputFilePath){
return office2pdf(inputFilePath);
}
服装店的英文
/**
* 根据操作系统的名称,获取OpenOffice 4的安装⽬录<br>
* 如我的OpenOffice 4安装在:C:/Program Files (x86)/OpenOffice 3<br>
*
* @return OpenOffice 3的安装⽬录
*/
public static String getOfficeHome(){
String osName = Property("os.name");
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("static/src/module/library/"+utilPath); String path = adContent(is);
System.out.println("操作系统名称:"+ osName);
return path;
//return "C://Program Files (x86)//OpenOffice 4";
}
/**
* 连接OpenOffice 并且启动OpenOffice
*
* @return
我的铃铛小说*/
public static OfficeManager getOfficeManager(){
DefaultOfficeManagerConfiguration config =new DefaultOfficeManagerConfiguration();
// 设置OpenOffice 3的安装⽬录
config.tOfficeHome(getOfficeHome());
config.tMaxTasksPerProcess(100);
//config.tMaxTasksPerProcess(10);
// 启动OpenOffice的服务
// configuration.tOfficeHome(getOfficeHome());
LOGGER.info("开始⽣成随机端⼝号"+"1111");
//因为openoffice是有默认的端⼝号,这⾥是为了解决并发问题
int random =rand(6000);
if(random==-1){
LOGGER.warn("no port can be ud");
return null;
}
}
config.tPortNumbers(random);
//config.tTaskExecutionTimeout(1000 * 60 * 5L);
//config.tTaskQueueTimeout(1000 * 60 * 60 * 24L);
OfficeManager officeManager = config.buildOfficeManager();
officeManager.start();
return officeManager;
}
public static int random(){
Int(Math.abs(65536-10000))+10000;
团结的图片}
/
*
public OpenOfficeConnection getConnect(int port) {
//logger.debug("[connectPort-port:"+port+"]");
return new SocketOpenOfficeConnection(port);
} */
好看的反义词/**
* 转换⽂件
*
* @param inputFile
* @param outputFilePath_end
* @param inputFilePath
* @param outputFilePath
* @param converter
*/
public static File converterFile(File inputFile, String outputFilePath_end, String inputFilePath,
OfficeDocumentConverter converter){
File outputFile =new File(outputFilePath_end);
// 假如⽬标路径不存在,则新建该路径
跑跑镇if(!ParentFile().exists()){
}
System.out.println("⽂件:"+ inputFilePath +"\n转换为\n⽬标⽂件:"+ outputFile +"\n成功!");
return outputFile;
}
/**
* 使Office2003-2007全部格式的⽂档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf⽂件<br>
*
* @param inputFilePath
* 源⽂件路径,如:"e:/test.docx"
* @param outputFilePath
* ⽬标⽂件路径,如:"e:/test_docx.pdf"
* @return
*/
public static File office2pdf(String inputFilePath){
OfficeManager officeManager = null;
try{
if(StringUtils.isEmpty(inputFilePath)){
System.out.println("输⼊⽂件地址为空,转换终⽌!");
return null;
}
File inputFile =new File(inputFilePath);
// 转换后的⽂件路径
String outputFilePath_end =getOutputFilePath(inputFilePath);
if(!ists()){
System.out.println("输⼊⽂件不存在,转换终⽌!");
return null;
}
// 获取OpenOffice的安装路径