一,工具类
package com.javajy.util;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.collections.CollectionUtils;
/**
* pdf 需要的jar包
* 1.itextpdf-5.5.13.jar
* 2.itext-asian-5.2.0.jar——支持中文jar包
* @author Administrator
*
*/
public class PDFUtil {
private static class Header extends PdfPageEventHelper {
// 页眉
@Override
public void onStartPage(PdfWriter pdfWriter, Document document) {
ColumnText.showTextAligned(pdfWriter.getDirectContent(), Element.ALIGN_CENTER,
new Phrase(new SimpleDateFormat("yyyy-MM-dd").format(new Date())), 65, document.top() + 10, 0);
}
}
public static void generatePdf(String title, String imgTitle, Map<String, String> data1, Map<String, String> data2,
List<String> imagData, String destination) {
Font font_normal = FontFactory.getFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 12,
Font.NORMAL, BaseColor.BLACK);
Font font_bold = FontFactory.getFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED, 20, Font.BOLD,
BaseColor.BLACK);
Document document = new Document();
try {
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(destination));
document.open();
// 添加页眉
Header header = new Header();
writer.setPageEvent(header);
document.open();
// 标题
Paragraph ptitle = new Paragraph(title, font_bold);
ptitle.setAlignment(Element.ALIGN_LEFT);
document.add(ptitle);
// 标题下划线样式————开始
PdfPTable titleTable = new PdfPTable(3); // 3 columns.
titleTable.setWidthPercentage(100); // Width 100%
titleTable.setSpacingBefore(3f); // Space before table
titleTable.setSpacingAfter(10f); // Space after table
float[] columnWidths2 = { 1f, 1f, 1f };
titleTable.setWidths(columnWidths2);
PdfPCell cellT1 = new PdfPCell(new Paragraph(""));
cellT1.setBorder(0);
cellT1.setMinimumHeight(3F);
cellT1.setBackgroundColor(new BaseColor(200, 200, 200));
PdfPCell cellT2 = new PdfPCell(new Paragraph(""));
cellT2.setBorder(0);
cellT2.setMinimumHeight(3F);
cellT2.setBackgroundColor(new BaseColor(200, 200, 200));
PdfPCell cellT3 = new PdfPCell(new Paragraph(""));
cellT3.setBorder(0);
cellT3.setMinimumHeight(3F);
cellT3.setBackgroundColor(new BaseColor(200, 200, 200));
titleTable.addCell(cellT1);
titleTable.addCell(cellT2);
titleTable.addCell(cellT3);
document.add(titleTable);
// 标题下划线样式————结束
// 第一个表格————开始
// 表格数据——开始
PdfPTable tablea = new PdfPTable(2); // 3 columns.
tablea.setWidthPercentage(80); // Width 100%
tablea.setSpacingBefore(10f); // Space before table
tablea.setSpacingAfter(0f); // Space after table
// Set Column widths
float[] columnWidths = { 1f, 1f };
tablea.setWidths(columnWidths);
Iterator entries = data1.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
PdfPCell cell1 = new PdfPCell(new Paragraph(key, font_normal));
cell1.setBorder(0);
cell1.setPaddingLeft(10);
cell1.setPaddingBottom(10);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell3 = new PdfPCell(new Paragraph(value, font_normal));
cell3.setBorder(0);
cell3.setPaddingLeft(10);
cell3.setPaddingBottom(10);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
tablea.addCell(cell1);
tablea.addCell(cell3);
}
document.add(tablea);
// 表格数据——结束
// 表格下划线样式————开始
PdfPTable titleTable11 = new PdfPTable(3); // 3 columns.
titleTable11.setWidthPercentage(80); // Width 100%
titleTable11.setSpacingBefore(0f); // Space before table
titleTable11.setSpacingAfter(10f); // Space after table
// Set Column widths
float[] columnWidths11 = { 1f, 1f, 1f };
titleTable11.setWidths(columnWidths11);
PdfPCell cellT11 = new PdfPCell(new Paragraph(""));
cellT11.setBorder(0);
cellT11.setMinimumHeight(8F);
cellT11.setBackgroundColor(new BaseColor(230, 230, 230));
PdfPCell cellT22 = new PdfPCell(new Paragraph(""));
cellT22.setBorder(0);
cellT22.setMinimumHeight(8F);
cellT22.setBackgroundColor(new BaseColor(230, 230, 230));
PdfPCell cellT33 = new PdfPCell(new Paragraph(""));
cellT33.setBorder(0);
cellT33.setMinimumHeight(8F);
cellT33.setBackgroundColor(new BaseColor(230, 230, 230));
titleTable11.addCell(cellT11);
titleTable11.addCell(cellT22);
titleTable11.addCell(cellT33);
document.add(titleTable11);
// 表格下划线样式————结束
// 第一个表格————结束
// 第二个表格————开始
// 表格数据——开始
PdfPTable table = new PdfPTable(2); // 3 columns.
table.setWidthPercentage(80); // Width 100%
table.setSpacingBefore(10f); // Space before table
table.setSpacingAfter(0f); // Space after table
// Set Column widths
float[] columnWidths22 = { 1f, 1f };
table.setWidths(columnWidths22);
Iterator entries2 = data2.entrySet().iterator();
while (entries2.hasNext()) {
Map.Entry entry = (Map.Entry) entries2.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
PdfPCell cell1 = new PdfPCell(new Paragraph(key, font_normal));
cell1.setBorder(0);
cell1.setPaddingLeft(10);
cell1.setPaddingBottom(10);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);
cell1.setVerticalAlignment(Element.ALIGN_MIDDLE);
PdfPCell cell3 = new PdfPCell(new Paragraph(value, font_normal));
cell3.setBorder(0);
cell3.setPaddingLeft(10);
cell3.setPaddingBottom(10);
cell3.setHorizontalAlignment(Element.ALIGN_LEFT);
cell3.setVerticalAlignment(Element.ALIGN_MIDDLE);
table.addCell(cell1);
table.addCell(cell3);
}
document.add(table);
// 表格数据——结束
// 表格下划线样式————开始
PdfPTable titleTable111 = new PdfPTable(3); // 3 columns.
titleTable111.setWidthPercentage(80); // Width 100%
titleTable111.setSpacingBefore(0f); // Space before table
titleTable111.setSpacingAfter(10f); // Space after table
// Set Column widths
float[] columnWidths111 = { 1f, 1f, 1f };
titleTable111.setWidths(columnWidths111);
PdfPCell cellT111 = new PdfPCell(new Paragraph(""));
cellT111.setBorder(0);
cellT111.setMinimumHeight(8F);
cellT111.setBackgroundColor(new BaseColor(230, 230, 230));
PdfPCell cellT222 = new PdfPCell(new Paragraph(""));
cellT222.setBorder(0);
cellT222.setMinimumHeight(8F);
cellT222.setBackgroundColor(new BaseColor(230, 230, 230));
PdfPCell cellT333 = new PdfPCell(new Paragraph(""));
cellT333.setBorder(0);
cellT333.setMinimumHeight(8F);
cellT333.setBackgroundColor(new BaseColor(230, 230, 230));
titleTable111.addCell(cellT111);
titleTable111.addCell(cellT222);
titleTable111.addCell(cellT333);
document.add(titleTable111);
// 表格下划线样式————结束
// 第二个表格————开始
// 图片——开始
Paragraph imgpTitle = new Paragraph(imgTitle, font_normal);
imgpTitle.setFirstLineIndent(62F);
imgpTitle.setAlignment(Element.ALIGN_LEFT);
document.add(imgpTitle);
try{
if (CollectionUtils.isNotEmpty(imagData)) {
PdfPTable imageTable11 = new PdfPTable(4); // 4columns.
imageTable11.setWidthPercentage(80); // Width 100%
imageTable11.setSpacingBefore(15f); // Space before table
imageTable11.setSpacingAfter(10f); // Space after table
float[] columnImageWidth = { 1f, 5f, 1f, 5f };
imageTable11.setWidths(columnImageWidth);
PdfPCell imageCell1 = new PdfPCell(new Paragraph("X", font_normal));
imageCell1.setBorder(0);
imageCell1.setMinimumHeight(8F);
imageCell1.setHorizontalAlignment(Element.ALIGN_CENTER);
imageTable11.addCell(imageCell1);
System.out.println("_______________________________"+imagData.get(0));
Image image1 = Image.getInstance(imagData.get(0));
image1.scaleAbsolute(150, 150);
PdfPCell imageCell2 = new PdfPCell(image1);
imageCell2.setHorizontalAlignment(Element.ALIGN_CENTER);
imageCell2.setBorder(0);
imageCell2.setMinimumHeight(8F);
imageTable11.addCell(imageCell2);
// }
if (imagData.size() > 1) {
PdfPCell imageCell3 = new PdfPCell(new Paragraph("Y", font_normal));
imageCell3.setBorder(0);
imageCell3.setMinimumHeight(8F);
imageCell3.setHorizontalAlignment(Element.ALIGN_CENTER);
imageTable11.addCell(imageCell3);
System.out.println("_______________________________"+imagData.get(1));
Image image2 = Image.getInstance(imagData.get(1));
image2.scaleAbsolute(150, 150);
PdfPCell imageCell4 = new PdfPCell(image2);
imageCell4.setHorizontalAlignment(Element.ALIGN_CENTER);
imageCell4.setBorder(0);
imageCell4.setMinimumHeight(8F);
imageTable11.addCell(imageCell4);
// }
}
document.add(imageTable11);
}
}catch (Exception e){
e.printStackTrace();
}
// 图片——结束
String rootPath = "/home/rjjapp/apache-tomcat-8.5.30/webapps/ROOT/file/";
Runtime.getRuntime().exec("chmod 777 -R "+rootPath);
document.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
二,调用方法
public static void main(String[] args) {
String title = "报告";
String imgTitle = "图片";
Map<String, String> data1 = new LinkedHashMap<>();
data1.put("1.fdsaf", "sdafgrdsgds");
data1.put("2.fdafdsfads", "fdsafdasfads");
Map<String, String> data2 = new LinkedHashMap<>();
data2.put("1.fdsaf", "sdafgrdsgds");
data2.put("2.fdafdsfads", "fdsafdasfads");
List<String> imagData = new ArrayList<>();
imagData.add("http://221.2.164.77:7089//files/2018-11-30/cedb8eb2-2b62-46a4-b3e3-67c37f453ad8.bmp");
imagData.add("http://221.2.164.77:7089//files/2018-11-30/77ae057b-8939-45d1-807b-bef72db86194.bmp");
// imagData.add("http://52lianbei.com/uploads/image/goods/20171118/c3c7f8c70e7ceef1f937fe4786385066.png");
imagData.add("http://52lianbei.com/uploads/image/goods/20171118/c3c7f8c70e7ceef1f937fe4786385066.png");
imagData.add("http://52lianbei.com/uploads/image/goods/20171118/c3c7f8c70e7ceef1f937fe4786385066.png");
generatePdf(title, imgTitle, data1, data2, imagData, "D:\\HelloWorld.pdf");
}
}