Java实现WordExcelTXT转PDF的⽅法
引⾔:
前段时间公司做的教育系统,系统需要实时记录⽤户学习课程的情况和时间,所以对⼀些除视频课程之外,对⼀些⽂本⽂档型课件同样如此,初次的⽅案是讲office相关类型的⽂件进⾏转换Html⽂件,然后展⽰对应的html⽂件,PC端差不多没问题了,但是个别⽂件再转换html之后,样式出现了错乱,即时做了编码转换处理,但是还是有个别乱码,最后改变⽅案,最后统⼀将⽂件转为pdf,然后通过流的⽅式在前端展⽰,其中包括Word Excel PPT TXT PDF等⽂件,代码如下:
备注:本来是可以直接展⽰pdf的,但是Andior上pdf展⽰不了,最后统⼀就⽤IO流的⽅式进⾏读取展⽰了.
1:添加maven依赖
<!--excel word txt ppt转pdf依赖-->
<dependency>
<groupId>aspose</groupId>
<artifactId>pdf</artifactId>
<version>11.5.0</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>words</artifactId>
<version>16.4.0</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>cell</artifactId>
<version>8.9.2</version>
</dependency>
<dependency>
<groupId>aspose</groupId>
<artifactId>pdf</artifactId>
<version>11.5.0</version>
</dependency>
2:添加l⽂件(Resource⽂件夹下)
<License>
<Data>
<Products>
<Product>Aspose.Total for Java</Product>
<Product>Aspose.Words for Java</Product>
</Products>
<EditionType>Enterprise</EditionType>
<SubscriptionExpiry>20991231</SubscriptionExpiry>
<LicenseExpiry>20991231</LicenseExpiry>
<SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber>
</Data>
<Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature> </License>
3:代码如下:
3.1获取License⽂件
public static boolean getLicense(){
boolean result = false;
InputStream is = null;
try{
is =ClassLoader().getResourceAsStream("l");
License aposeLic = new License();
aposeLic.setLicense(is);
result = true;
}catch(Exception e){
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return result;
}
3.2:⽂本⽂件转码
/* 将txt 转换编码
* @param file
* @author zsqing
*/
public File saveAsUTF8(File file){
String code = "gbk";
byte[] head = new byte[3];
java修改html文件try {
InputStream inputStream = new FileInputStream(file);
if (head[0] == -1 && head[1] == -2) {
code = "UTF-16";
} else if (head[0] == -2 && head[1] == -1) {
code = "Unicode";
} else if (head[0] == -17 && head[1] == -69 && head[2] == -65) {
code = "UTF-8";
}
inputStream.close();
System.out.println(code);
if (code.equals("UTF-8")) {
return file;
}
String str = adFileToString(file, code);
FileUtils.writeStringToFile(file, str, "UTF-8");
System.out.println("转码结束");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return file;
}
3.3:word和txt转换pdf
/**
* 将word txt转换成pdf
* @param inPath
* @param outPath
* @author zsqing
*/
public void wordAndTextToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request)
{
String fileToPdfUrl="";
boolean flag = false;
File file = null;
FileOutputStream os = null;
try
{
//long old = System.currentTimeMillis();
// 新建⼀个空⽩⽂档
file = new File(outPath);
file = saveAsUTF8(file);
os = new FileOutputStream(file);
// InPath是将要被转化的⽂档
com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
/*
* 全⾯⽀持DOC,DOCX进⾏OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF间转换
*/
doc.save(os, SaveFormat.PDF);
flag = true;
//long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化⽤时
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (os != null)
{
os.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (!flag)
{
file.deleteOnExit();
}
}
}
3.4:Excel转换pdf
/**
* 将docx转换成pdf
* @param inPath
* @param outPath
* @author zsqing
*/
public void wordToPdf(String inPath, String outPath ,String localIP,HttpServletRequest request)
{
String fileToPdfUrl="";
boolean flag = false;
File file = null;
FileOutputStream os = null;
try
{
/
/long old = System.currentTimeMillis();
// 新建⼀个空⽩⽂档
file = new File(outPath);
file = saveAsUTF8(file);
os = new FileOutputStream(file);
// InPath是将要被转化的⽂档
com.aspose.words.Document doc = new com.aspose.words.Document(inPath);
/*
* 全⾯⽀持DOC,DOCX进⾏OOXML,RTF,HTML,OpenDocument,PDF,EPUB,XPS,SWF间转换
*/
doc.save(os, SaveFormat.PDF);
flag = true;
//long now = System.currentTimeMillis();
//System.out.println("共耗时:" + ((now - old) / 1000.0) + "秒"); // 转化⽤时
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
if (os != null)
{
os.close();
}
}
catch (Exception e)
{
e.printStackTrace();
}
if (!flag)
{
file.deleteOnExit();
}
}
}
总结
以上所述是⼩编给⼤家介绍的Java实现Word/Excel/TXT转PDF的⽅法,希望对⼤家有所帮助,如果⼤家有任何疑问请给我留⾔,⼩编会及时回复⼤家的。在此也⾮常感谢⼤家对⽹站的⽀持!如果你觉得本⽂对你有帮助,欢迎转载,烦请注明出处,谢谢!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论