1. 程式人生 > >aspose.words for java操作文檔doc,設置一級二級三級標題以及段落表格等詳情

aspose.words for java操作文檔doc,設置一級二級三級標題以及段落表格等詳情

rip oid license vat itl format 字體 pdf str

實現將aspose.words的相關組件jar包

以下是我自己編輯整理的工具類,歡迎交流

package com;

import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.aspose.words.CellMerge;
import com.aspose.words.CellVerticalAlignment;
import com.aspose.words.Document;
import com.aspose.words.DocumentBuilder;
import com.aspose.words.FontSettings; import com.aspose.words.HeightRule; import com.aspose.words.License; import com.aspose.words.LineStyle; import com.aspose.words.ParagraphAlignment; import com.aspose.words.SaveFormat; import com.aspose.words.Table; /** * * @ClassName:AsposeUtils * @Description: aspose.words操作文檔工具類 * @Date:2019年1月11日 *
*/ public class AsposeUtils { 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");
new License().setLicense(is); AsposeLicense = true; } catch (Exception e) { e.printStackTrace(); } } /** * 驗證License * @return boolean */ private static void getLicense() { if (!AsposeLicense) { // 驗證License 若不驗證則轉化出的pdf文檔會有水印產生 LOGGER.info("**********驗證失敗,會產生水印***********"); } LOGGER.info("************驗證成功,已去除默認水印***********"); } /** * 保存pdf * @param path保存目錄 * @param doc原文檔 */ public static void savePdf(String path,Document doc){ String format = "pdf"; save(path,doc,SaveFormat.PDF,format); } /** * 保存doc * @param path保存目錄 * @param doc原文檔 */ public static void saveDoc(String path,Document doc){ String format = "doc"; save(path,doc,SaveFormat.DOC,format); } public static void saveDocx(String path,Document doc){ String format = "docx"; save(path,doc,SaveFormat.DOCX,format); } /** * 保存各種格式的文檔 * @param path保存地址 * @param doc保存文件 * @param saveFormat保存格式 */ private static void save(String path,Document doc,int saveFormat,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(); } } /** * 制作報表總標題 * @param builder * @param title */ public static void setTitle(DocumentBuilder builder,String title){ try { //設置字體格式 builder.insertHtml("<p style=‘text-align:center‘><font face=‘宋體‘ size=‘30‘ color=‘black‘>"+ title +"</font></p>"); } catch (Exception e) { LOGGER.info("************制作標題"+title+"出現異常***********"); e.printStackTrace(); } } /** * 制作一級標題 * @param builder * @param title */ private static void setTitle1(DocumentBuilder builder,String title1){ try { builder.insertHtml("<h1 style=‘text-align:left;font-family:Simsun;‘>"+ title1 +"</h1>"); } catch (Exception e) { LOGGER.info("************制作一級標題"+title1+"出現異常***********"); e.printStackTrace(); } } /** * 制作二級標題 * @param builder * @param title */ private static void setTitle2(DocumentBuilder builder,String title2){ try { builder.insertHtml("<h2 style=‘text-align:left;font-family:Simsun;‘>"+ title2 +"</h2>"); } catch (Exception e) { LOGGER.info("************制作二級標題"+title2+"出現異常***********"); e.printStackTrace(); } } /** * 制作三級標題 * @param builder * @param title */ private static void setTitle3(DocumentBuilder builder,String title3){ try { builder.insertHtml("<h3 style=‘text-align:left;font-family:Simsun;‘>"+ title3 +"</h3>"); } catch (Exception e) { LOGGER.info("************制作三級標題"+title3+"出現異常***********"); e.printStackTrace(); } } /** * 區別各級標題 * @param builder * @param title * @param level */ public static void setTitleS(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; } } /** * 制作報表段落 * @param builder * @param pragraph */ public static void setParagraph(DocumentBuilder builder,String pragraph){ try { //設置字體格式 builder.insertHtml("<p><font face=‘宋體‘>&nbsp;&nbsp; "+ pragraph +"</font></p>"); } catch (Exception e) { LOGGER.info("************制作段落"+pragraph+"出現異常***********"); e.printStackTrace(); } } /** * 制作一個單元格並追加數據,單元格不合並 * @param builder * @param width 設置單元格寬度 * @param text */ public static void setCell(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); } /** * 插入單元格,不設置寬度,單元格不合並 * @param builder * @param text */ public static void setCell(DocumentBuilder builder,String text){ builder.insertCell(); builder.getCellFormat().setVerticalMerge(CellMerge.NONE); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); builder.write(text); } public static void setCell(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 void setCell(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()); } } /** * 垂直合並單元格的第一格 * @param builder * @param text */ public static void setStartVerticalMerge(DocumentBuilder builder,String text){ builder.insertCell(); builder.getCellFormat().setVerticalMerge(CellMerge.FIRST); builder.getCellFormat().setVerticalAlignment(CellVerticalAlignment.CENTER); builder.write(text); } /** * 垂直合並單元格的後面格 * @param builder * @param text */ public static void setThenVerticalMerge(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); } /** * 水平合並單元格的第一格 * @param builder * @param text */ public static void setStartHorizontalMerge(DocumentBuilder builder,String text){ builder.insertCell(); builder.getCellFormat().setHorizontalMerge(CellMerge.FIRST); builder.write(text); } /** * 水平合並單元格的後面格 * @param builder * @param text */ public static void setThenHorizontalMerge(DocumentBuilder builder){ builder.insertCell(); // This cell is vertically merged to the cell above and should be empty. builder.getCellFormat().setHorizontalMerge(CellMerge.PREVIOUS); } /** * 制作表格數據行 * @param builder */ public static void setRow(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(); } } /** * 制作表格 * @param builder * @throws Exception */ public static Table setTable(DocumentBuilder builder){ builder.moveToDocumentEnd(); Table table = builder.startTable(); builder.getParagraphFormat().setAlignment(ParagraphAlignment.CENTER); return table; } }

aspose.words for java操作文檔doc,設置一級二級三級標題以及段落表格等詳情