PDF文件導(dǎo)出
一、添加項(xiàng)目依賴
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.6</version>
</dependency>
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.2.0</version>
</dependency>
主要使用itextpdf自己進(jìn)行封裝
二、編寫(xiě)工具欄調(diào)用方法
/**
*
* @param dataList 需要導(dǎo)出的數(shù)據(jù)
* @param out 輸出流
* @throws Exception
*/
public static void exportPdf(List<Record> dataList, ServletOutputStream out) throws Exception {
Document document = new Document(PageSize.A4, 36, 36, 24, 36);
//建立一個(gè)書(shū)寫(xiě)器
PdfWriter writer = PdfWriter.getInstance(document, out);
//必須要打開(kāi)文件
document.open();
document.add(createPdfTable(dataList, 2));
//關(guān)閉文檔
document.close();
//關(guān)閉書(shū)寫(xiě)器
writer.close();
}
三、編寫(xiě)表格
/**
* 創(chuàng)建圖片單元格
* @param dataList
* @param colSpan
* @return
*/
public static PdfPTable createPdfTable(List<Record> dataList, int colSpan) throws DocumentException {
PdfPTable table = new PdfPTable(colSpan);
table.setWidthPercentage(100); // 寬度100%填充
float[] columWidths = new float[colSpan];
columWidths[0] = 0.6f;
columWidths[1] = 0.4f;
table.setWidths(columWidths);
PdfPCell titleCell1 = createPdfStringPCell("圖片", 14, true, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
PdfPCell titleCell2 = createPdfStringPCell("信息描述", 14, true, Element.ALIGN_CENTER, Element.ALIGN_CENTER);
table.addCell(titleCell1);
table.addCell(titleCell2);
for (Record data : dataList) {
PdfPCell imageCell = createPdfImageCell(data.getStr("imagePath"), 10);
table.addCell(imageCell);
PdfPCell stringCell = createPdfStringPCell(data.getStr("text"), 11, false, Element.ALIGN_LEFT, Element.ALIGN_MIDDLE);
table.addCell(stringCell);
}
return table;
}
四、編寫(xiě)普通單元格
/**
* 創(chuàng)建字符串單元格
* @param text (自定義換行需要在文本添加換行符)
* @param fontSize 字體大小
* @param horizontalAlignment 水平樣式
* @param verticalAlignment 垂直樣式
* @return
*/
public static PdfPCell createPdfStringPCell(String text, int fontSize, boolean isBold, int horizontalAlignment, int verticalAlignment) {
Font font = getFont();
font.setSize(fontSize);
if (isBold) {
font.setStyle(Font.BOLD);
}
PdfPCell stringCell = new PdfPCell(new Paragraph(text, font));//單元格內(nèi)容
stringCell.setPaddingLeft(10);//左填充20
stringCell.setHorizontalAlignment(horizontalAlignment);//水平居中Element.ALIGN_CENTER
stringCell.setVerticalAlignment(verticalAlignment);//垂直居中Element.ALIGN_MIDDLE
stringCell.setFixedHeight(20f);
return stringCell;
}
五、編寫(xiě)字符串單元格
/**
* 創(chuàng)建圖片單元格
* @param imagePath 圖片路徑
* @param hightLength 高度
* @return
*/
public static PdfPCell createPdfImageCell(String imagePath, int hightLength) {
try {
Image image = Image.getInstance(ImageCache.getImage(imagePath));
image.setAlignment(Image.ALIGN_CENTER);
image.scaleAbsolute(290, 199);//自定義大小,自己根據(jù)實(shí)際需要調(diào)整大小
PdfPCell imageCell = new PdfPCell(image, false);//image如果自定義了大小,則fit參數(shù)必須為false,否則不生效
imageCell.setVerticalAlignment(Element.ALIGN_CENTER);
imageCell.setHorizontalAlignment(Element.ALIGN_CENTER);
imageCell.setFixedHeight(hightLength * 20f);//相當(dāng)于設(shè)置與此單元格同行的行高度
return imageCell;
} catch (BadElementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return new PdfPCell();
}
六、支持中文的字體樣式
/**
* 支持中文字體
* @return font
*/
public static 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) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Font font = new Font(Font.FontFamily.UNDEFINED);
return font;
}
最后貼出參考鏈接:
簡(jiǎn)書(shū):https://www.jianshu.com/p/20d4905383b4
demo網(wǎng)址:https://www.cnblogs.com/dragonKings/p/11880635.html







暫無(wú)評(píng)論,快來(lái)評(píng)論吧!