apose word java 内存问题_aspose.words for java操作文档doc,设置一级二级三级标题以及段落表格等详情...

AsposeUtils是一个用于处理Aspose.Words for Java文档操作的工具类,包括设置标题、段落、表格等功能。类中包含验证许可、保存PDF和DOC格式文档的方法,以及创建各级标题和段落的辅助方法。此外,还处理了在Linux环境下设置字体目录的问题,以避免内存问题。
摘要由CSDN通过智能技术生成

packagecom;importjava.io.InputStream;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importcom.aspose.words.CellMerge;importcom.aspose.words.CellVerticalAlignment;importcom.aspose.words.Document;importcom.aspose.words.DocumentBuilder;importcom.aspose.words.FontSettings;importcom.aspose.words.HeightRule;importcom.aspose.words.License;importcom.aspose.words.LineStyle;importcom.aspose.words.ParagraphAlignment;importcom.aspose.words.SaveFormat;importcom.aspose.words.Table;/***

* @ClassName:AsposeUtils

* @Description: aspose.words操作文档工具类

* @Date:2019年1月11日

**/

public classAsposeUtils {private static final Logger LOGGER = LoggerFactory.getLogger(AsposeUtils.class);private static boolean AsposeLicense = false;static{try{//license.xml

InputStream is = AsposeUtils.class.getClassLoader().getResourceAsStream("license.xml");newLicense().setLicense(is);

AsposeLicense= true;

}catch(Exception e) {

e.printStackTrace();

}

}/*** 验证License

*@returnboolean*/

private static voidgetLicense() {if (!AsposeLicense) { //验证License 若不验证则转化出的pdf文档会有水印产生

LOGGER.info("**********验证失败,会产生水印***********");

}

LOGGER.info("************验证成功,已去除默认水印***********");

}/*** 保存pdf

*@parampath保存目录

*@paramdoc原文档*/

public static voidsavePdf(String path,Document doc){

String format= "pdf";

save(path,doc,SaveFormat.PDF,format);

}/*** 保存doc

*@parampath保存目录

*@paramdoc原文档*/

public static voidsaveDoc(String path,Document doc){

String format= "doc";

save(path,doc,SaveFormat.DOC,format);

}public static voidsaveDocx(String path,Document doc){

String format= "docx";

save(path,doc,SaveFormat.DOCX,format);

}/*** 保存各种格式的文档

*@parampath保存地址

*@paramdoc保存文件

*@paramsaveFormat保存格式*/

private static void save(String path,Document doc,intsaveFormat,String format){

getLicense();try{

String os= System.getProperty("os.name");if(os.toLowerCase().indexOf("linux") >= 0){

LOGGER.info("************当前使用*****linux**"+ os +"*********");//设置一个字体目录

FontSettings.getDefaultInstance().setFontsFolder("/usr/share/fonts/", false);

}

doc.save(path, saveFormat);

}catch(Exception e) {

LOGGER.info("************保存文档(格式为"+format+")出现异常***********");

e.printStackTrace();

}

}/*** 制作报表总标题

*@parambuilder

*@paramtitle*/

public static voidsetTitle(DocumentBuilder builder,String title){try{//设置字体格式

builder.insertHtml("

"+ title +"

");

}catch(Exception e) {

LOGGER.info("************制作标题"+title+"出现异常***********");

e.printStackTrace();

}

}/*** 制作一级标题

*@parambuilder

*@paramtitle*/

private static voidsetTitle1(DocumentBuilder builder,String title1){try{

builder.insertHtml("

"+ title1 +"

");

}catch(Exception e) {

LOGGER.info("************制作一级标题"+title1+"出现异常***********");

e.printStackTrace();

}

}/*** 制作二级标题

*@parambuilder

*@paramtitle*/

private static voidsetTitle2(DocumentBuilder builder,String title2){try{

builder.insertHtml("

"+ title2 +"

");

}catch(Exception e) {

LOGGER.info("************制作二级标题"+title2+"出现异常***********");

e.printStackTrace();

}

}/*** 制作三级标题

*@parambuilder

*@paramtitle*/

private static voidsetTitle3(DocumentBuilder builder,String title3){try{

builder.insertHtml("

"+ title3 +"

");

}catch(Exception e) {

LOGGER.info("************制作三级标题"+title3+"出现异常***********");

e.printStackTrace();

}

}/*** 区别各级标题

*@parambuilder

*@paramtitle

*@paramlevel*/

public static voidsetTitleS(DocumentBuilder builder,String title,String level){switch(level) {case "1":

setTitle1(builder, title);break;case "2":

setTitle2(builder, title);break;case "3":

setTitle3(builder, title);break;default:break;

}

}/*** 制作报表段落

*@parambuilder

*@parampragraph*/

public static voidsetParagraph(DocumentBuilder builder,String pragraph){try{//设置字体格式

builder.insertHtml("

   "+ pragraph +"

");

}catch(Exception e) {

LOGGER.info("************制作段落"+pragraph+"出现异常***********");

e.printStackTrace();

}

}/*** 制作一个单元格并追加数据,单元格不合并

*@parambuilder

*@paramwidth 设置单元格宽度

*@paramtext*/

public static voidsetCell(DocumentBuilder builder,Double width,String text){

builder.insertCell();if(width==null) width =3d;

builder.getCellFormat().setWidth(width);

builder.getCellFormat().setVerticalMerge(CellMerge.NONE);

builder.write(text);

}/*** 插入单元格,不设置宽度,单元格不合并

*@parambuilder

*@paramtext*/

public static voidsetCell(DocumentBuilder builder,String text){

builder.insertCell();

builder.getCellFormat().setVerticalMerge(CellMerge.NONE);

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

builder.write(text);

}public static voidsetCell(DocumentBuilder builder,Long text){

builder.insertCell();

builder.getCellFormat().setVerticalMerge(CellMerge.NONE);

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);if(text == null){

builder.write("");

}else{

builder.write(text.toString());

}

}public static voidsetCell(DocumentBuilder builder,Double text){

builder.insertCell();

builder.getCellFormat().setVerticalMerge(CellMerge.NONE);

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);if(text == null){

builder.write("");

}else{

builder.write(text.toString());

}

}/*** 垂直合并单元格的第一格

*@parambuilder

*@paramtext*/

public static voidsetStartVerticalMerge(DocumentBuilder builder,String text){

builder.insertCell();

builder.getCellFormat().setVerticalMerge(CellMerge.FIRST);

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

builder.write(text);

}/*** 垂直合并单元格的后面格

*@parambuilder

*@paramtext*/

public static voidsetThenVerticalMerge(DocumentBuilder builder){

builder.insertCell();//This cell is vertically merged to the cell above and should be empty.

builder.getCellFormat().setVerticalMerge(CellMerge.PREVIOUS);

builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER);

}/*** 水平合并单元格的第一格

*@parambuilder

*@paramtext*/

public static voidsetStartHorizontalMerge(DocumentBuilder builder,String text){

builder.insertCell();

builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST);

builder.write(text);

}/*** 水平合并单元格的后面格

*@parambuilder

*@paramtext*/

public static voidsetThenHorizontalMerge(DocumentBuilder builder){

builder.insertCell();//This cell is vertically merged to the cell above and should be empty.

builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS);

}/*** 制作表格数据行

*@parambuilder*/

public static voidsetRow(DocumentBuilder builder){try{

builder.getRowFormat().setHeadingFormat(true);

builder.getRowFormat().getBorders().setLineStyle(LineStyle.SINGLE);

builder.getRowFormat().setHeightRule(HeightRule.AUTO);

builder.getRowFormat().setAllowBreakAcrossPages(true);

}catch(Exception e) {

LOGGER.info("************制作表格数据行出现异常***********");

e.printStackTrace();

}

}/*** 制作表格

*@parambuilder

*@throwsException*/

public staticTable setTable(DocumentBuilder builder){

builder.moveToDocumentEnd();

Table table=builder.startTable();

builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER);returntable;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值