、package com.gaopin.admin.commons.utils;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPictureData;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.apache.xmlbeans.XmlException;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSectPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblWidth;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STTblWidth;
import com.baidubce.util.BaiduBosUtils;
import com.gaopin.admin.model.vo.SysPictureVo;
import com.gaopin.admin.model.vo.SysSapVo;
import com.gaopin.admin.service.impl.SysSapServiceImpl;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
/**
* CSV操作(导出和导入)
*
* @author jone.cai
* @version 1.0 Jan 27, 2017 4:30:58 PM
*/
public class WordUtils {
private Document document;
private BaseFont bfChinese;
public BaseFont getBfChinese() {
return bfChinese;
}
public void setBfChinese(BaseFont bfChinese) {
this.bfChinese = bfChinese;
}
public Document getDocument() {
return document;
}
public void setDocument(Document document) {
this.document = document;
}
public WordUtils() {
this.document = new Document(PageSize.A4);
}
/**
* @param filePath
* 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
* @throws DocumentException
* @throws IOException
*/
public void openDocument(String filePath) throws DocumentException, IOException {
// 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中
RtfWriter2.getInstance(this.document, new FileOutputStream(filePath));
this.document.open();
// 设置中文字体
// this.bfChinese = BaseFont.createFont("STSongStd-Light",
// "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
}
/**
* @param imgUrl
* 图片路径
* @param imageAlign
* 显示位置
* @param height
* 显示高度
* @param weight
* 显示宽度
* @param percent
* 显示比例
* @param heightPercent
* 显示高度比例
* @param weightPercent
* 显示宽度比例
* @param rotation
* 显示图片旋转角度
* @throws MalformedURLException
* @throws IOException
* @throws DocumentException
*/
public void insertImg(SysSapVo sysSap, String imgUrl, int imageAlign, int height, int weight, int percent,
int heightPercent, int weightPercent, int rotation)
throws MalformedURLException, IOException, DocumentException {
// 添加图片
Image img = Image.getInstance(imgUrl);
if (img == null)
return;
img.setAbsolutePosition(0, 0);
img.setAlignment(imageAlign);
img.scaleAbsolute(height, weight);
img.scalePercent(percent);
img.scalePercent(heightPercent, weightPercent);
img.setRotation(rotation);
Table aTable = new Table(3, 4);
int width1[] = { 55, 10, 20 };
aTable.setWidths(width1);// 设置每列所占比例
aTable.setWidth(100); // 占页面宽度 90%
aTable.setAlignment(Element.ALIGN_LEFT);// 居中显示
aTable.setAlignment(Element.ALIGN_CENTER);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(0); // 边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色
Font font = new Font(bfChinese, 8 );
Cell cellEmailBB = new Cell(new Phrase("", font));
cellEmailBB.add(img);
cellEmailBB.setVerticalAlignment(Element.ALIGN_CENTER);
cellEmailBB.setHorizontalAlignment(Element.ALIGN_LEFT);
// cellEmailBB.setBorderWidthTop(0);
cellEmailBB.setRowspan(4);
cellEmailBB.setBorderColor(new Color(0, 0, 0));
cellEmailBB.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellEmailBB);
Cell cellEmailB = new Cell(new Phrase(" 合同编号:", font));
cellEmailB.setVerticalAlignment(Element.ALIGN_LEFT);
cellEmailB.setHorizontalAlignment(Element.ALIGN_LEFT);
cellEmailB.setBorderColor(new Color(0, 0, 0));
cellEmailB.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellEmailB);
Cell cellEmailBs = new Cell(new Phrase(" " + sysSap.getContractId(), font));
cellEmailBs.setVerticalAlignment(Element.ALIGN_LEFT);
cellEmailBs.setHorizontalAlignment(Element.ALIGN_LEFT);
cellEmailBs.setBorderColor(new Color(0, 0, 0));
cellEmailBs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellEmailBs);
Cell cellFaxA = new Cell(new Phrase(" 日期:", font));
cellFaxA.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxA.setHorizontalAlignment(Element.ALIGN_LEFT);
cellFaxA.setBorderColor(new Color(0, 0, 0));
cellFaxA.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxA);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 小写的mm表示的是分钟
Date time = sysSap.getContractDay();
String day = sdf.format(time);
Cell cellFaxAs = new Cell(new Phrase(" " + day, font));
cellFaxAs.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxAs.setHorizontalAlignment(Element.ALIGN_LEFT);
cellFaxAs.setBorderColor(new Color(0, 0, 0));
cellFaxAs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxAs);
Cell cellFaxB = new Cell(new Phrase(" 制单人:", font));
cellFaxB.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxB.setHorizontalAlignment(Element.ALIGN_LEFT);
// cellFaxB.setBorderWidthTop(0);
cellFaxB.setBorderColor(new Color(0, 0, 0));
cellFaxB.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxB);
Cell cellFaxBs = new Cell(new Phrase(" " + sysSap.getMakeUser(), font));
cellFaxBs.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxBs.setHorizontalAlignment(Element.ALIGN_LEFT);
// cellFaxBs.setBorderWidthTop(0);
cellFaxBs.setBorderColor(new Color(0, 0, 0));
cellFaxBs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxBs);
Cell cellFaxAA = new Cell(new Phrase(" 邮箱:", font));
cellFaxAA.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxAA.setHorizontalAlignment(Element.ALIGN_LEFT);
cellFaxAA.setBorderColor(new Color(0, 0, 0));
cellFaxAA.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxAA);
Cell cellFaxAAs = new Cell(new Phrase(" " + sysSap.getMakeUserEmail(), font));
cellFaxAAs.setVerticalAlignment(Element.ALIGN_LEFT);
cellFaxAAs.setHorizontalAlignment(Element.ALIGN_LEFT);
cellFaxAAs.setBorderColor(new Color(0, 0, 0));
cellFaxAAs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellFaxAAs);
document.add(aTable);
document.add(new Paragraph("\n"));
}
/**
* @param titleStr
* 标题
* @param fontsize
* 字体大小
* @param fontStyle
* 字体样式
* @param elementAlign
* 对齐方式
* @throws DocumentException
*/
public void insertTitle(String titleStr, int fontsize, int fontStyle, int elementAlign) throws DocumentException {
Font titleFont = new Font(this.bfChinese, fontsize, fontStyle);
Paragraph title = new Paragraph(titleStr);
// 设置标题格式对齐方式
title.setAlignment(elementAlign);
title.setFont(titleFont);
this.document.add(title);
}
/*
* 高品内容许可协议
*/
public void insertRiskTable(SysSapVo sysSap) throws DocumentException {
Table aTable = new Table(4, 7);
int width1[] = { 10, 40, 10, 40 };
aTable.setWidth(90); // 占页面宽度 90%
aTable.setWidths(width1);// 设置每列所占比例
aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示
aTable.setAlignment(Element.ALIGN_CENTER);// 纵向居中显示
aTable.setAutoFillEmptyCells(true); // 自动填满
aTable.setBorderWidth(0); // 边框宽度
aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色
Font fontChinese = new Font(bfChinese, 9, Font.BOLD);
Font font = new Font(bfChinese, 10);
Cell cellCompanyA = new Cell(new Phrase("甲方:", fontChinese));
cellCompanyA.setVerticalAlignment(Element.ALIGN_LEFT);
cellCompanyA.setHorizontalAlignment(Element.ALIGN_LEFT);
// 表格隐藏边框线
cellCompanyA.setBorderWidthTop(0);
cellCompanyA.setBorderColor(new Color(0, 0, 0));
cellCompanyA.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellCompanyA);
Cell cellCompanyAs = new Cell(new Phrase(sysSap.getCompanyAName(), font));
cellCompanyAs.setVerticalAlignment(Element.ALIGN_LEFT);
cellCompanyAs.setHorizontalAlignment(Element.ALIGN_LEFT);
// 表格隐藏边框线
cellCompanyAs.setBorderWidthTop(0);
cellCompanyAs.setBorderColor(new Color(0, 0, 0));
cellCompanyAs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellCompanyAs);
Cell cellCompanyB = new Cell(new Phrase("乙方:", fontChinese));
cellCompanyB.setVerticalAlignment(Element.ALIGN_LEFT);
cellCompanyB.setHorizontalAlignment(Element.ALIGN_LEFT);
cellCompanyB.setBorderWidthTop(0);
cellCompanyB.setBorderColor(new Color(0, 0, 0));
cellCompanyB.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellCompanyB);
Cell cellCompanyBs = new Cell(new Phrase("高品(北京)图像有限公司", font));
cellCompanyBs.setVerticalAlignment(Element.ALIGN_LEFT);
cellCompanyBs.setHorizontalAlignment(Element.ALIGN_LEFT);
cellCompanyBs.setBorderWidthTop(0);
cellCompanyBs.setBorderColor(new Color(0, 0, 0));
cellCompanyBs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellCompanyBs);
Cell cellAddressA = new Cell(new Phrase("地址:", fontChinese));
cellAddressA.setVerticalAlignment(Element.ALIGN_LEFT);
cellAddressA.setHorizontalAlignment(Element.ALIGN_LEFT);
cellAddressA.setBorderWidthTop(0);
cellAddressA.setBorderColor(new Color(0, 0, 0));
cellAddressA.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellAddressA);
Cell cellAddressAs = new Cell(new Phrase(sysSap.getAddressA(), font));
cellAddressAs.setVerticalAlignment(Element.ALIGN_LEFT);
cellAddressAs.setHorizontalAlignment(Element.ALIGN_LEFT);
cellAddressAs.setBorderWidthTop(0);
cellAddressAs.setBorderColor(new Color(0, 0, 0));
cellAddressAs.setBackgroundColor(new Color(255, 255, 255));
aTable.addCell(cellAddressAs);
Cell cellAddressB = new Cell(new Phrase("地址:", fontChinese));
cellAddressB.setVerticalAlignment(Element.ALIGN_LEFT);
cellAddressB.setHorizontalAlignment(Element.ALIGN_LEFT);
cellAddressB.setBorderWidthTop(0);
cellAddressB.setBorderColor(new Color(0, 0, 0));
cellAddressB.setBackgroundColor(new Color(255, 2