aspose页面文件预览问题

问题:

预览pdf文件时,项目在windows上正常,在linux上中文乱码

解决:

将C:\Windows\Fonts下的字体(我需要的是宋体)上传到项目根目录/fonts上,并添加FontSettings.setFontsFolder(Word2PdfUtil.class.getResource("/").getPath()+"fonts", false);

代码如下:

import java.io.InputStream;
import java.io.OutputStream;

import com.aspose.words.Document;
import com.aspose.words.FontSettings;
import com.aspose.words.SaveFormat;

public class Word2PdfUtil {

	public static void doc2pdf(InputStream fileInputStream, OutputStream outputStream) {
		try {
			if (AsposeLicenseUtil.setWordsLicense()) {				FontSettings.setFontsFolder(Word2PdfUtil.class.getResource("/").getPath() + "fonts", false);
				Document doc = new Document(fileInputStream); // Address是将要被转化的word文档
				doc.save(outputStream, SaveFormat.PDF);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

参考:

https://apireference-qa.aspose.com/java/words/com.aspose.words/FontSettings

https://blog.csdn.net/qq_28800347/article/details/80895156

https://blog.csdn.net/qq_35617517/article/details/80879955

 

问题:

文档大小,页数有限制,且预览的文件上显示

This document was truncated here because it was created using Aspose.Words in Evaluation Mode.

解决:

设置license

/**
	 * 获取License的输入流
	 *
	 * @return
	 */
	private static InputStream getLicenseInput() {
		InputStream inputStream = null;
		ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
		try {
			inputStream = new FileInputStream(contextClassLoader.getResource("aspose"+File.separator+"license.xml").getPath());
		} catch (FileNotFoundException e) {
			logger.error("license not found!", e);
		}
		return inputStream;
	}

	/**
	 * 设置License
	 *
	 * @return true表示已成功设置License, false表示失败
	 */
	public static boolean setWordsLicense() {
		com.aspose.words.License aposeLic = new com.aspose.words.License();
		if (aposeLic.getIsLicensed()) {
			return aposeLic.getIsLicensed();
		}
		InputStream licenseInput = getLicenseInput();
		if (licenseInput != null) {
			try {
				aposeLic.setLicense(licenseInput);
				return aposeLic.getIsLicensed();
			} catch (Exception e) {
				logger.error("set words license error!", e);
			}
		}
		return false;
	}

参考:

https://www.cnblogs.com/zhangzhxb/p/5984766.html

https://blog.csdn.net/xiaosanshao9/article/details/51915952
 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先机子上安装有office,在COM中添加引用Microsoft.Word.11.0.Object.Library(或11.0以上) Microsoft.Office.Interop.Word.Application myWordApp = null; Microsoft.Office.Interop.Word.Document doc = null; object Filename = path + "\\" + TaskID + ".docx";//目的文件 object templateFile = System.Windows.Forms.Application.StartupPath + @"\Template.docx";//模板文件,有一个五列一行的表 System.IO.File.Copy(templateFile.ToString(), Filename.ToString(), true);//模板WORD中有一个五列的表头,分别是卡号,串口号,发送指令条数,接收指令条数,收发成功率 myWordApp = new Microsoft.Office.Interop.Word.Application(); doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); /////显示页码 object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; object oFirstPage = true; oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.Add(ref oAlignment, ref oFirstPage); myWordApp.Selection.Sections[1].Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].PageNumbers.NumberStyle = Microsoft.Office.Interop.Word.WdPageNumberStyle.wdPageNumberStyleNumberInDash; for (int i = 2; i < 102; i++)//举例100台 { doc.Tables[1].Rows.Add(ref Nothing);//表格增加一行 doc.Tables[1].Cell(i, 1).Range.Text = "250297";//卡号 doc.Tables[1].Cell(i, 2).Range.Text = "COM12";//串口号 doc.Tables[1].Cell(i, 3).Range.Text = "100";//发送指令条数 doc.Tables[1].Cell(i, 4).Range.Text = "99";//接收指令条数 doc.Tables[1].Cell(i, 5).Range.Text = "99%";//收发成功率 } doc.SaveAs(ref Filename, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object savechanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges;//不保存挂起的更改 ////下面是直接打印,文档不显示 //doc.PrintOut(ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); object readOnly=false; object isVisable = true;////文档打开状态为可视 doc = myWordApp.Documents.Open(ref Filename, ref Nothing, ref readOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisable, ref Nothing, ref Nothing, ref Nothing, ref Nothing); doc.PrintPreview();//打印预览
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值