|
|
@@ -15,7 +15,6 @@ |
|
|
|
*/
|
|
|
|
package org.jeecgframework.poi.pdf.export;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.OutputStream;
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
import java.util.ArrayList;
|
|
|
@@ -25,9 +24,11 @@ import java.util.List; |
|
|
|
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
import org.jeecgframework.poi.excel.annotation.ExcelTarget;
|
|
|
|
import org.jeecgframework.poi.excel.entity.ExportParams;
|
|
|
|
import org.jeecgframework.poi.excel.entity.params.ExcelExportEntity;
|
|
|
|
import org.jeecgframework.poi.excel.export.base.ExportBase;
|
|
|
|
import org.jeecgframework.poi.pdf.entity.PdfExportParams;
|
|
|
|
import org.jeecgframework.poi.pdf.styler.IPdfExportStyler;
|
|
|
|
import org.jeecgframework.poi.pdf.styler.PdfExportStylerDefaultImpl;
|
|
|
|
import org.jeecgframework.poi.util.PoiPublicUtil;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@@ -35,13 +36,8 @@ import org.slf4j.LoggerFactory; |
|
|
|
import com.itextpdf.text.Document;
|
|
|
|
import com.itextpdf.text.DocumentException;
|
|
|
|
import com.itextpdf.text.Element;
|
|
|
|
import com.itextpdf.text.Font;
|
|
|
|
import com.itextpdf.text.Font.FontFamily;
|
|
|
|
import com.itextpdf.text.PageSize;
|
|
|
|
import com.itextpdf.text.Phrase;
|
|
|
|
import com.itextpdf.text.pdf.BaseFont;
|
|
|
|
import com.itextpdf.text.pdf.PdfPCell;
|
|
|
|
import com.itextpdf.text.pdf.PdfPRow;
|
|
|
|
import com.itextpdf.text.pdf.PdfPTable;
|
|
|
|
import com.itextpdf.text.pdf.PdfWriter;
|
|
|
|
|
|
|
@@ -52,13 +48,18 @@ import com.itextpdf.text.pdf.PdfWriter; |
|
|
|
*/
|
|
|
|
public class PdfExportServer extends ExportBase {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(PdfExportServer.class);
|
|
|
|
|
|
|
|
private Document document;
|
|
|
|
private IPdfExportStyler styler;
|
|
|
|
|
|
|
|
public PdfExportServer(OutputStream outStream) {
|
|
|
|
private boolean isListData = false;
|
|
|
|
|
|
|
|
public PdfExportServer(OutputStream outStream, PdfExportParams entity) {
|
|
|
|
try {
|
|
|
|
document = new Document(PageSize.A4, 36, 36, 24, 36);
|
|
|
|
styler = entity.getStyler() == null ? new PdfExportStylerDefaultImpl()
|
|
|
|
: entity.getStyler();
|
|
|
|
document = styler.getDocument();
|
|
|
|
PdfWriter.getInstance(document, outStream);
|
|
|
|
document.open();
|
|
|
|
} catch (Exception e) {
|
|
|
@@ -73,11 +74,11 @@ public class PdfExportServer extends ExportBase { |
|
|
|
* @param dataSet
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public Document createPdf(ExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
|
|
|
|
public Document createPdf(PdfExportParams entity, Class<?> pojoClass, Collection<?> dataSet) {
|
|
|
|
try {
|
|
|
|
List<ExcelExportEntity> excelParams = new ArrayList<ExcelExportEntity>();
|
|
|
|
if (entity.isAddIndex()) {
|
|
|
|
excelParams.add(indexExcelEntity(entity));
|
|
|
|
//excelParams.add(indexExcelEntity(entity));
|
|
|
|
}
|
|
|
|
// 得到所有字段
|
|
|
|
Field fileds[] = PoiPublicUtil.getClassFields(pojoClass);
|
|
|
@@ -94,11 +95,16 @@ public class PdfExportServer extends ExportBase { |
|
|
|
return document;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Document createPdfByExportEntity(ExportParams entity,
|
|
|
|
public Document createPdfByExportEntity(PdfExportParams entity,
|
|
|
|
List<ExcelExportEntity> excelParams,
|
|
|
|
Collection<?> dataSet) {
|
|
|
|
try {
|
|
|
|
sortAllParams(excelParams);
|
|
|
|
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
|
|
|
|
if (excelParams.get(k).getList() != null) {
|
|
|
|
isListData = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//设置各个列的宽度
|
|
|
|
float[] widths = getCellWidths(excelParams);
|
|
|
|
PdfPTable table = new PdfPTable(widths.length);
|
|
|
@@ -106,7 +112,7 @@ public class PdfExportServer extends ExportBase { |
|
|
|
//table.setLockedWidth(true);
|
|
|
|
//设置表头
|
|
|
|
createHeaderAndTitle(entity, table, excelParams);
|
|
|
|
short rowHeight = getRowHeight(excelParams);
|
|
|
|
int rowHeight = getRowHeight(excelParams) / 50;
|
|
|
|
Iterator<?> its = dataSet.iterator();
|
|
|
|
while (its.hasNext()) {
|
|
|
|
Object t = its.next();
|
|
|
@@ -122,29 +128,21 @@ public class PdfExportServer extends ExportBase { |
|
|
|
}
|
|
|
|
|
|
|
|
private void createCells(PdfPTable table, Object t, List<ExcelExportEntity> excelParams,
|
|
|
|
short rowHeight) throws Exception {
|
|
|
|
int rowHeight) throws Exception {
|
|
|
|
ExcelExportEntity entity;
|
|
|
|
int maxHeight = 1, cellNum = 0;
|
|
|
|
//cellNum += indexKey;
|
|
|
|
int maxHeight = getThisMaxHeight(t, excelParams);
|
|
|
|
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
|
|
|
|
entity = excelParams.get(k);
|
|
|
|
if (entity.getList() != null) {
|
|
|
|
Collection<?> list = getListCellValue(entity, t);
|
|
|
|
int listC = 0;
|
|
|
|
for (Object obj : list) {
|
|
|
|
//createListCells(patriarch, index + listC, cellNum, obj, entity.getList(), sheet,
|
|
|
|
// workbook);
|
|
|
|
listC++;
|
|
|
|
}
|
|
|
|
cellNum += entity.getList().size();
|
|
|
|
if (list != null && list.size() > maxHeight) {
|
|
|
|
maxHeight = list.size();
|
|
|
|
createListCells(table, obj, entity.getList(), rowHeight);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Object value = getCellValue(entity, t);
|
|
|
|
if (entity.getType() == 1) {
|
|
|
|
createStringCell(table, value == null ? "" : value.toString(),
|
|
|
|
(int) (entity.getHeight() * 2.5));
|
|
|
|
createStringCell(table, value == null ? "" : value.toString(), entity,
|
|
|
|
rowHeight, 1, maxHeight);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
@@ -152,6 +150,56 @@ public class PdfExportServer extends ExportBase { |
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 创建集合对象
|
|
|
|
* @param table
|
|
|
|
* @param obj
|
|
|
|
* @param rowHeight
|
|
|
|
* @param list
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
private void createListCells(PdfPTable table, Object obj, List<ExcelExportEntity> excelParams,
|
|
|
|
int rowHeight) throws Exception {
|
|
|
|
ExcelExportEntity entity;
|
|
|
|
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
|
|
|
|
entity = excelParams.get(k);
|
|
|
|
Object value = getCellValue(entity, obj);
|
|
|
|
if (entity.getType() == 1) {
|
|
|
|
createStringCell(table, value == null ? "" : value.toString(), entity, rowHeight);
|
|
|
|
} else {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取这一列的高度
|
|
|
|
* @param t 对象
|
|
|
|
* @param excelParams 属性列表
|
|
|
|
* @return
|
|
|
|
* @throws Exception 通过反射过去值得异常
|
|
|
|
*/
|
|
|
|
private int getThisMaxHeight(Object t, List<ExcelExportEntity> excelParams) throws Exception {
|
|
|
|
if (isListData) {
|
|
|
|
ExcelExportEntity entity;
|
|
|
|
int maxHeight = 1;
|
|
|
|
for (int k = 0, paramSize = excelParams.size(); k < paramSize; k++) {
|
|
|
|
entity = excelParams.get(k);
|
|
|
|
if (entity.getList() != null) {
|
|
|
|
Collection<?> list = getListCellValue(entity, t);
|
|
|
|
maxHeight = (list == null || maxHeight > list.size()) ? maxHeight : list.size();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return maxHeight;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取Cells的宽度数组
|
|
|
|
* @param excelParams
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
private float[] getCellWidths(List<ExcelExportEntity> excelParams) {
|
|
|
|
List<Float> widths = new ArrayList<Float>();
|
|
|
|
for (int i = 0; i < excelParams.size(); i++) {
|
|
|
@@ -171,7 +219,7 @@ public class PdfExportServer extends ExportBase { |
|
|
|
return widthArr;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createHeaderAndTitle(ExportParams entity, PdfPTable table,
|
|
|
|
private void createHeaderAndTitle(PdfExportParams entity, PdfPTable table,
|
|
|
|
List<ExcelExportEntity> excelParams) throws DocumentException {
|
|
|
|
int feildWidth = getFieldLength(excelParams);
|
|
|
|
if (entity.getTitle() != null) {
|
|
|
@@ -186,78 +234,52 @@ public class PdfExportServer extends ExportBase { |
|
|
|
* @param title
|
|
|
|
* @param index
|
|
|
|
*/
|
|
|
|
private int createTitleRow(ExportParams title, PdfPTable table,
|
|
|
|
private int createTitleRow(PdfExportParams title, PdfPTable table,
|
|
|
|
List<ExcelExportEntity> excelParams) {
|
|
|
|
int rows = getRowNums(excelParams);
|
|
|
|
int cellIndex = 0;
|
|
|
|
PdfPCell iCell = null;
|
|
|
|
int currentRows = table.getLastCompletedRowIndex(),
|
|
|
|
filedLength = getFieldLength(excelParams);
|
|
|
|
//先创建空白的Cell 然后去循环
|
|
|
|
for (int i = 0; i < rows; i++) {
|
|
|
|
for (int j = 0; j <= filedLength; j++) {
|
|
|
|
createStringCell(table, "", 25);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
PdfPRow listRow = null;
|
|
|
|
if (rows == 2) {
|
|
|
|
listRow = table.getRow(currentRows + 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
PdfPRow row = table.getRow(currentRows + 1);
|
|
|
|
|
|
|
|
for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++)
|
|
|
|
|
|
|
|
{
|
|
|
|
for (int i = 0, exportFieldTitleSize = excelParams.size(); i < exportFieldTitleSize; i++) {
|
|
|
|
ExcelExportEntity entity = excelParams.get(i);
|
|
|
|
if (StringUtils.isNotBlank(entity.getName())) {
|
|
|
|
iCell = setStringCell(row.getCells()[cellIndex], entity.getName(),
|
|
|
|
(int) entity.getHeight());
|
|
|
|
}
|
|
|
|
if (entity.getList() != null) {
|
|
|
|
List<ExcelExportEntity> sTitel = entity.getList();
|
|
|
|
if (StringUtils.isNotBlank(entity.getName())) {
|
|
|
|
iCell.setColspan(sTitel.size());
|
|
|
|
createStringCell(table, entity.getName(), entity, 10, entity.getList().size(),
|
|
|
|
1);
|
|
|
|
}
|
|
|
|
List<ExcelExportEntity> sTitel = entity.getList();
|
|
|
|
for (int j = 0, size = sTitel.size(); j < size; j++) {
|
|
|
|
setStringCell(
|
|
|
|
rows == 2 ? listRow.getCells()[cellIndex] : row.getCells()[cellIndex],
|
|
|
|
sTitel.get(j).getName(), (int) sTitel.get(j).getHeight());
|
|
|
|
cellIndex++;
|
|
|
|
createStringCell(table, sTitel.get(j).getName(), sTitel.get(j), 10);
|
|
|
|
}
|
|
|
|
cellIndex--;
|
|
|
|
} else if (rows == 2) {
|
|
|
|
iCell.setRowspan(2);
|
|
|
|
//把合并后的边框去掉
|
|
|
|
iCell.setBorderWidthBottom(0);
|
|
|
|
listRow.getCells()[cellIndex].setBorderWidthTop(0);
|
|
|
|
} else {
|
|
|
|
createStringCell(table, entity.getName(), entity, 10, 1, rows == 2 ? 2 : 1);
|
|
|
|
}
|
|
|
|
cellIndex++;
|
|
|
|
}
|
|
|
|
return rows;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private void createHeaderRow(ExportParams entity, PdfPTable table, int feildLength) {
|
|
|
|
|
|
|
|
createStringCell(table, entity.getTitle(), entity.getTitleHeight() / 20, feildLength + 1,
|
|
|
|
1);
|
|
|
|
private void createHeaderRow(PdfExportParams entity, PdfPTable table, int feildLength) {
|
|
|
|
PdfPCell iCell = new PdfPCell(
|
|
|
|
new Phrase(entity.getTitle(), styler.getFont(null, entity.getTitle())));
|
|
|
|
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
iCell.setVerticalAlignment(Element.ALIGN_CENTER);
|
|
|
|
iCell.setFixedHeight(entity.getTitleHeight());
|
|
|
|
iCell.setColspan(feildLength + 1);
|
|
|
|
table.addCell(iCell);
|
|
|
|
if (entity.getSecondTitle() != null) {
|
|
|
|
PdfPCell iCell = new PdfPCell(new Phrase(entity.getSecondTitle(), getFont()));
|
|
|
|
iCell = new PdfPCell(
|
|
|
|
new Phrase(entity.getSecondTitle(), styler.getFont(null, entity.getSecondTitle())));
|
|
|
|
iCell.setHorizontalAlignment(Element.ALIGN_RIGHT);
|
|
|
|
iCell.setVerticalAlignment(Element.ALIGN_CENTER);
|
|
|
|
iCell.setFixedHeight(entity.getSecondTitleHeight() / 20);
|
|
|
|
iCell.setFixedHeight(entity.getSecondTitleHeight());
|
|
|
|
iCell.setColspan(feildLength + 1);
|
|
|
|
table.addCell(iCell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private PdfPCell createStringCell(PdfPTable table, String value, int height, int colspan,
|
|
|
|
int rowspan) {
|
|
|
|
PdfPCell iCell = new PdfPCell(new Phrase(value, getFont()));
|
|
|
|
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
|
iCell.setFixedHeight(height);
|
|
|
|
private PdfPCell createStringCell(PdfPTable table, String text, ExcelExportEntity entity,
|
|
|
|
int rowHeight, int colspan, int rowspan) {
|
|
|
|
PdfPCell iCell = new PdfPCell(new Phrase(text, styler.getFont(entity, text)));
|
|
|
|
styler.setCellStyler(iCell, entity, text);
|
|
|
|
iCell.setFixedHeight((int) (rowHeight * 2.5));
|
|
|
|
if (colspan > 1) {
|
|
|
|
iCell.setColspan(colspan);
|
|
|
|
}
|
|
|
@@ -268,35 +290,13 @@ public class PdfExportServer extends ExportBase { |
|
|
|
return iCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PdfPCell createStringCell(PdfPTable table, String value, int height) {
|
|
|
|
PdfPCell iCell = new PdfPCell(new Phrase(value, getFont()));
|
|
|
|
iCell.setHorizontalAlignment(Element.ALIGN_CENTER);
|
|
|
|
iCell.setVerticalAlignment(Element.ALIGN_MIDDLE);
|
|
|
|
iCell.setFixedHeight(height);
|
|
|
|
private PdfPCell createStringCell(PdfPTable table, String text, ExcelExportEntity entity,
|
|
|
|
int rowHeight) {
|
|
|
|
PdfPCell iCell = new PdfPCell(new Phrase(text, styler.getFont(entity, text)));
|
|
|
|
styler.setCellStyler(iCell, entity, text);
|
|
|
|
iCell.setFixedHeight((int) (rowHeight * 2.5));
|
|
|
|
table.addCell(iCell);
|
|
|
|
return iCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
private PdfPCell setStringCell(PdfPCell iCell, String value, int height) {
|
|
|
|
iCell.setPhrase(new Phrase(value, getFont()));
|
|
|
|
iCell.setFixedHeight(height);
|
|
|
|
return iCell;
|
|
|
|
}
|
|
|
|
|
|
|
|
private Font getFont() {
|
|
|
|
try {
|
|
|
|
//用以支持中文
|
|
|
|
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
|
|
|
|
BaseFont.NOT_EMBEDDED);
|
|
|
|
Font font = new Font(bfChinese);
|
|
|
|
return font;
|
|
|
|
} catch (DocumentException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
Font font = new Font(FontFamily.UNDEFINED);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|