1.修改过字体了,并且也是先转换成utf-8,后缀名改为.odt,但是依旧乱码。
2.只有txt乱码,word文档,其他文档,都不会乱码
代码如下
/**
* office文件 转pdf
*
* @param sourceFile 源文件路径
* @param destFile 目标文件路径
* @return
*/
@SuppressWarnings("AlibabaLowerCamelCaseVariableNaming")
public boolean officeToPDF(String sourceFile, String destFile) {
try {
File inputFile = new File(sourceFile);
if (!inputFile.exists()) {
// 找不到源文件, 则返回false
return false;
}
// 如果目标路径不存在, 则新建该路径
File outputFile = new File(destFile);
if (!outputFile.getParentFile().exists()) {
outputFile.getParentFile().mkdirs();
}
//如果目标文件存在,则删除
if (outputFile.exists()) {
outputFile.delete();
}
if (sourceFile.contains(".txt")) {
// 讲txt转为odt
String odtFile = sourceFile.replace(".txt", ".odt");
copyByLineEncoding(sourceFile, "gbk", odtFile, "gbk");
inputFile = new File(odtFile);
// html、pdf 不转换
} else if (sourceFile.contains(".htm") || sourceFile.contains(".pdf")) {
return true;
}
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
OpenOfficeConnection connection = new SocketOpenOfficeConnection(openOfficeHost, openOfficePort);
connection.connect();
//用于测试openOffice连接时间
log.info("连接时间:" + df.format(new Date()));
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
converter.convert(inputFile, outputFile);
//测试word转PDF的转换时间
log.info("转换时间:" + df.format(new Date()));
connection.disconnect();
return true;
} catch (Exception e) {
e.printStackTrace();
log.error("openOffice连接失败!请检查IP,端口");
}
return false;
}
使用到的复制文件的方法
/**
* 文件复制
*
* @param srcFile
* @param srcEncoding
* @param destFile
* @param destEncoding
*/
private void copyByLineEncoding(String srcFile, String srcEncoding, String destFile,
String destEncoding) {
BufferedReader reader = null;
BufferedWriter writer = null;
try {
reader = new BufferedReader(new InputStreamReader(
new FileInputStream(srcFile), srcEncoding));
writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(destFile), destEncoding));
char[] charArray = new char[512];
int size;
while ((size = reader.read(charArray, 0, charArray.length)) != -1) {
writer.write(charArray, 0, size);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
如果是文本的话,直接生成pdf都行,
至于doc这些,把windows的字体全部拷贝过去试试呗,之前我们用其他api,也出现了部分doc乱码的问题,把字体包拷贝全就没事了
您好,我是问答小助手,你的问题已经有小伙伴为您解答了问题,您看下是否解决了您的问题。
如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~
试过用free spire.pdf for java这个api来转换 效果还行
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.*;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.graphics.*;
public class CreatePdfDocumentInJava {
public static void main(String[] args) throws FileNotFoundException, IOException {
//创建PdfDocument对象
PdfDocument doc = new PdfDocument();
//添加一页
PdfPageBase page = doc.getPages().add();
//标题文字
String title = "Java基础语法";
//创建单色画刷对象
PdfSolidBrush brush1 = new PdfSolidBrush(new PdfRGBColor(Color.BLUE));
PdfSolidBrush brush2 = new PdfSolidBrush(new PdfRGBColor(Color.BLACK));
//创建TrueType字体对象
PdfTrueTypeFont font1= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,14),true);
PdfTrueTypeFont font2= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,10),true);
//创建PdfStringFormat对象
PdfStringFormat format1 = new PdfStringFormat();
format1.setAlignment(PdfTextAlignment.Center);//设置文字居中
//使用drawString方法绘制标题文字
page.getCanvas().drawString(title, font1, brush1, new Point2D.Float(page.getActualBounds(true).width / 2, 0),format1);
//从txt文件读取内容到字符串
String body = readFileToString("C:\\Users\\Administrator\\Desktop\\bodyText.txt");
//创建PdfStringFormat对象
PdfStringFormat format2 = new PdfStringFormat();
format2.setParagraphIndent(20);//设置段首缩进
//创建Rectangle2D对象
Rectangle2D.Float rect = new Rectangle2D.Float(0, 30, page.getActualBounds(true).width,page.getActualBounds(true).height);
//使用drawString方法在矩形区域绘制主体文字
page.getCanvas().drawString(body, font2, brush2, rect,format2);
//保存到PDF文档
doc.saveToFile("ouput.pdf");
}
//自定义方法读取txt文件内容到字符串
private static String readFileToString(String filepath) throws FileNotFoundException, IOException {
StringBuilder sb = new StringBuilder();
String s ="";
BufferedReader br = new BufferedReader(new FileReader(filepath));
while( (s = br.readLine()) != null) {
sb.append(s + "\n");
}
br.close();
String str = sb.toString();
return str;
}
}