相关文章推荐
爱喝酒的圣诞树  ·  mac下连接夜神模拟器_kill ...·  11 月前    · 
粗眉毛的作业本  ·  Notepad++使用和主题更换的一点说明_ ...·  1 年前    · 
帅气的斑马  ·  Pandas将csv数据拆分成多列并保存 - 知乎·  1 年前    · 
大气的梨子  ·  oracle中搜索子串INSTR的使用以及将 ...·  1 年前    · 
发财的煎饼果子  ·  vertica最佳实践之优化projecti ...·  2 年前    · 
Code  ›  aspose-words java word 转换 服务器乱码解决 - 崔笑颜的博客开发者社区
png string aspose
https://cloud.tencent.com/developer/article/1784043
捣蛋的金针菇
1 年前
作者头像
崔笑颜
0 篇文章

aspose-words java word 转换 服务器乱码解决 - 崔笑颜的博客

前往专栏
腾讯云
开发者社区
文档 意见反馈 控制台
首页
学习
活动
专区
工具
TVP
文章/答案/技术大牛
发布
首页
学习
活动
专区
工具
TVP
返回腾讯云官网
社区首页 > 专栏 > 小小码农一个。 > aspose-words java word 转换 服务器乱码解决 - 崔笑颜的博客

aspose-words java word 转换 服务器乱码解决 - 崔笑颜的博客

作者头像
崔笑颜
发布 于 2021-02-02 18:03:46
2.7K 0
发布 于 2021-02-02 18:03:46
举报

在本机测试是好的 但是到服务器就乱码 想到pdf是一种为打印设计的文档格式,可能跟字体相关。而且转出来的pdf中文都是空心方框,不是单纯的乱码,很有可能是缺少字体渲染不出来造成的。 服务器上用fc-list命令查看字体,果然没有中文字体 接下来将本机Windows的字体上传到CentOS服务器上。 具体操作: 也可以直接使用我的字体 经测试没问题 有的系统版本的原因 也会导致乱码 链接: https://pan.baidu.com/s/1Fj6pqfNDi__sdBJthdFCFg 提取码: k35v 将Windows上字体文件 C:\Windows\Fonts目录,复制到CentOS的/usr/share/fonts目录 重启服务器

如果你转换的文档中有 公式系列的话 那么还需要再导入一种公式字体 这里已经准备好啦 链接: https://pan.baidu.com/s/1c8WYZMcqVeRHc23vVe3xrQ 提取码: c329

WX20190809-100929@2x
WX20190809-100929@2x

同样需要重启服务器

已知问题 着重号无法转换 官网也为给出解决方案

未命名文件
未命名文件

aspose-words word转图片

这里所用到的jar包 和这里所用的的一样 jar包下载 和谐文件 将word转换为单张图片

 
	// 将word 转化为图片一张
	public static String parseFileToBase64_PNG(String wordfile) throws Exception {
		//文件流
		InputStream inputStream = new FileInputStream(wordfile);
		//文件 获取文件名字
		File file = new File(wordfile);
		String name = file.getName();
		//截取不带后缀名的字段
		String fileName = name.substring(0, name.lastIndexOf("."));
		//文件上传路径
		String parent = file.getParent();
		List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
		BufferedImage image = null;
			bufferedImages = wordToImg(inputStream);
			image = MergeImage.mergeImage(false, bufferedImages);
		boolean png = ImageIO.write(image, "png", new File(parent + "/" + fileName + ".png"));// 写入流中
		if(png == false){
			return "转换失败";
		//关闭流
		inputStream.close();
		return "转换成功";
	 * @Description: word和txt文件转换图片
	private static List<BufferedImage> wordToImg(InputStream inputStream) throws Exception {
		if (!isWordLicense()) {
			return null;
		try {
			Document doc = new Document(inputStream);
			ImageSaveOptions options = new ImageSaveOptions(SaveFormat.PNG);
			options.setPrettyFormat(true);
			options.setUseAntiAliasing(true);
			options.setUseHighQualityRendering(true);
			int pageCount = doc.getPageCount();
			List<BufferedImage> imageList = new ArrayList<BufferedImage>();
			for (int i = 0; i < pageCount; i++) {
				OutputStream output = new ByteArrayOutputStream();
				options.setPageIndex(i);
				doc.save(output, options);
				ImageInputStream imageInputStream = ImageIO.createImageInputStream(parse(output));
				imageList.add(ImageIO.read(imageInputStream));
			return imageList;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
	 * @Description: 验证aspose.word组件是否授权:无授权的文件有水印和试用标记
	public static boolean isWordLicense() {
		boolean result = false;
		try {
			// InputStream inputStream = new
			// FileInputStream("D:\\Workspaces\\TestFilters\\lib\\license.xml");
			// 避免文件遗漏
			String licensexml = "<License>\n" + "<Data>\n" + "<Products>\n"
					+ "<Product>Aspose.Total for Java</Product>\n" + "<Product>Aspose.Words for Java</Product>\n"
					+ "</Products>\n" + "<EditionType>Enterprise</EditionType>\n"
					+ "<SubscriptionExpiry>20991231</SubscriptionExpiry>\n"
					+ "<LicenseExpiry>20991231</LicenseExpiry>\n"
					+ "<SerialNumber>23dcc79f-44ec-4a23-be3a-03c1632404e9</SerialNumber>\n" + "</Data>\n"
					+ "<Signature>\n"
					+ "sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=\n"
					+ "</Signature>\n" + "</License>";
			InputStream inputStream = new ByteArrayInputStream(licensexml.getBytes());
			com.aspose.words.License license = new com.aspose.words.License();
			license.setLicense(inputStream);
			result = true;
		} catch (Exception e) {
			e.printStackTrace();
		return result;
// outputStream转inputStream
	public static ByteArrayInputStream parse(OutputStream out) throws Exception {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		baos = (ByteArrayOutputStream) out;
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		return swapStream;
 

新建一个工具类

package util;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
 * @program: transformation
 * @description: 多张图片合成
 * @author: cuixy
 * @create: 2019-07-26 17:10
public class MergeImage {
     * 合并任数量的图片成一张图片
     * @param isHorizontal
     *            true代表水平合并,fasle代表垂直合并
     * @param imgs
     *            待合并的图片数组
     * @return
     * @throws IOException
    public static BufferedImage mergeImage(boolean isHorizontal, List<BufferedImage> imgs) throws IOException {
        // 生成新图片
        BufferedImage destImage = null;
        // 计算新图片的长和高
        int allw = 0, allh = 0, allwMax = 0, allhMax = 0;
        // 获取总长、总宽、最长、最宽
        for (int i = 0; i < imgs.size(); i++) {
            BufferedImage img = imgs.get(i);
            allw += img.getWidth();
            if (imgs.size() != i + 1) {
                allh += img.getHeight() + 2;
            } else {
                allh += img.getHeight();
            if (img.getWidth() > allwMax) {
                allwMax = img.getWidth();
            if (img.getHeight() > allhMax) {
                allhMax = img.getHeight();
        // 创建新图片
        if (isHorizontal) {
            destImage = new BufferedImage(allw, allhMax, BufferedImage.TYPE_INT_RGB);
        } else {
            destImage = new BufferedImage(allwMax, allh, BufferedImage.TYPE_INT_RGB);
        // 注释,分隔线从灰色变成纯黑
        // Graphics2D g2 = (Graphics2D) destImage.getGraphics();
        // g2.setBackground(Color.LIGHT_GRAY);
        // g2.clearRect(0, 0, allw, allh);
        // g2.setPaint(Color.RED);
        // 合并所有子图片到新图片
        int wx = 0, wy = 0;
        for (int i = 0; i < imgs.size(); i++) {
            BufferedImage img = imgs.get(i);
            int w1 = img.getWidth();
            int h1 = img.getHeight();
            // 从图片中读取RGB
            int[] ImageArrayOne = new int[w1 * h1];
            ImageArrayOne = img.getRGB(0, 0, w1, h1, ImageArrayOne, 0, w1); // 逐行扫描图像中各个像素的RGB到数组中
            if (isHorizontal) { // 水平方向合并
                destImage.setRGB(wx, 0, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            } else { // 垂直方向合并
                destImage.setRGB(0, wy, w1, h1, ImageArrayOne, 0, w1); // 设置上半部分或左半部分的RGB
            wx += w1;
            wy += h1 + 2;
        return destImage;
}

将word转换为单页 每页为一张图片

/**
	 * word 转每页pdf
	 * @param wordfile
	 * @return
	 * @throws Exception
	public static String parseFileToBase64_PNG1(String wordfile) throws Exception {
		if (!isWordLicense()) {
			return null;
		// 声明一个
		InputStream inputStream = new FileInputStream(wordfile);
		//文件 获取文件名字
		File file = new File(wordfile);
		String name = file.getName();
		//截取不带后缀名的字段
		String fileName = name.substring(0, name.lastIndexOf("."));
		//文件上传路径
		String parent = file.getParent();
		//创建同名文件夹
		new File(parent+"/"+ fileName).mkdir();
		List<BufferedImage> bufferedImages = wordToImg1(inputStream);
		for (int i = 0; i < bufferedImages.size(); i++){
			ImageIO.write(bufferedImages.get(i), "png", new File(parent +"/"+ fileName +"/"+ "第"+ i +"页" + fileName + ".png"));
		return "转换成功";
	}

图片转换为base64 前台预览

业务需求 word转换成在线预览 结合 这里是word转图片 也可以直接转换为在线预览

public static String parseFileToBase64_PNG1(InputStream inputStream, int pageNum) throws Exception {
		// String png_base64 = "";
		List<BufferedImage> bufferedImages = new ArrayList<BufferedImage>();
		BufferedImage image = null;
		ByteArrayOutputStream baos = new ByteArrayOutputStream();// io流
		bufferedImages = wordToImg1(inputStream, pageNum);
		image = mergeImage(false, bufferedImages);
		ImageIO.write(image, "png", baos);// 写入流中
		byte[] bytes = baos.toByteArray();// 转换成字节
 
推荐文章
爱喝酒的圣诞树  ·  mac下连接夜神模拟器_kill process(ps): uiautomator-CSDN博客
11 月前
粗眉毛的作业本  ·  Notepad++使用和主题更换的一点说明_notepad主题风格不见了_davidsmith8的博客-CSDN博客
1 年前
帅气的斑马  ·  Pandas将csv数据拆分成多列并保存 - 知乎
1 年前
大气的梨子  ·  oracle中搜索子串INSTR的使用以及将一个字符串按照某个字符分隔成多行_sxjlinux的博客-CSDN博客
1 年前
发财的煎饼果子  ·  vertica最佳实践之优化projection_vertica带projection入库_weiha666的博客-CSDN博客
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号