JAVA导出PDF文件
1、 导出pdf文件,有许多方法,这里采用的是使用itext.jar。中文还需要使用iTextAsian.jar
2、 引用itext.jar包。本次我使用的是itext-1.3.1.jar。注意部署工程的时候须将此包放到工程对应的lib目录下面或者isap平台的lib目录。
3、 相关代码如下:
此方法目前我是写在web工程的ExlPrintReportBean.java类中
publicvoid createPdf(Map fileNameMap,HttpServletResponse response) throws Exception {
response.setContentType("application/vnd.ms-excel");
try {
// 创建PDF
String sheetTitle = URLEncoder.encode(epr.getSheetTitle() + ".pdf","UTF-8");
sheetTitle=sheetTitle.replace("+", "_");
response.setHeader("Content-Disposition", "attachment;filename="
+ sheetTitle);
Document document = new Document(PageSize.A1, 50, 50, 50, 50);
PdfWriter.getInstance(document,response.getOutputStream());
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//设置中文字体,使之数据包含中文时候能导出为pdf.
Font headFont = new Font(bfChinese, 18, Font.BOLDITALIC);//设置字体大小
Font headFont1 = new Font(bfChinese, 16, Font.BOLD);//设置字体大小
Font headFont2 = new Font(bfChinese, 12, Font.BOLD);//设置字体大小
String tmpDir = System.getProperty("java.io.tmpdir");
if(!tmpDir.endsWith(File.separator))
{
tmpDir+=File.separator;
}
//导出图表
if (fileNameMap.get("barChart") != null
|| fileNameMap.get("lineChart") != null
|| fileNameMap.get("pie3DChart") != null)
{
float[] widths = {4f};
PdfPTable table = new PdfPTable(widths);
String fileName =null;
if (fileNameMap.get("barChart")!=null)
{
fileName = tmpDir+fileNameMap.get("barChart");
Image image = Image.getInstance(fileName);
table.addCell(image);
}
if(fileNameMap.get("lineChart")!=null)
{
fileName = tmpDir+fileNameMap.get("lineChart");
Image image = Image.getInstance(fileName);
table.addCell(image);
}
if(fileNameMap.get("pie3DChart")!=null)
{
fileName = tmpDir+fileNameMap.get("pie3DChart");
Image image = Image.getInstance(fileName);
table.addCell(image);
}
document.add(table);
}
//导出表格
else
{
//写标题 epr.getSheetTitle()
Paragraph title = new Paragraph(epr.getSheetTitle(), headFont);
document.add(title);
//写多级标题
Paragraph desc = new Paragraph(epr.getDesc(),headFont1);
document.add(desc);
List list = epr.getAllData();
PdfPTable table = new PdfPTable(colsLen);//建立一个pdf表格
table.setSpacingBefore(130f);//设置表格上面空白宽度
// table.setTotalWidth(535);//设置表格的宽度
table.setLockedWidth(false);//设置表格的宽度固定
table.setWidthPercentage(100); // percentage
table.getDefaultCell().setPadding(3);
table.getDefaultCell().setBorderWidth(2);
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER);
// t.s
//定义一个表格
// int headerwidths[] = { 9, 4, 8, 10, 8, 11, 9, 7, 9, 10, 4, 10 }; // percentage
// table.setWidths(headerwidths);
PdfPCell cell =null;
//添加表格头
for(int i= 0 ;i <colsLen ;i++)
{
ExlPrintColumns cols = this.exlcols[i];
String tableHeadName = cols.getShowName();
cell = new PdfPCell(new Paragraph(tableHeadName,headFont2));
//table.addCell(tableHeadName);
table.addCell(cell);
}
table.setHeaderRows(1); // this is the end of the table header
table.getDefaultCell().setBorderWidth(1);
//循环添加表格数据
if (list != null && epr.isGet() && list.size() > 0)
{
list.remove(0);
}
Iterator it = list.iterator();
int j=0;
while (it.hasNext())
{
j++;
if (j % 2 == 1)
{
table.getDefaultCell().setGrayFill(0.9f);
}
HashMap mapdata = (HashMap) it.next();
for (int i = 0; i < this.colsLen; i++)
{
ExlPrintColumns cols = this.exlcols[i];
String colsName = cols.getName().toUpperCase();
String colData = mapdata.get(colsName)!=null?mapdata.get(colsName).toString():null;
if (colData != null)
{
colData = colData.trim();
}
table.addCell(colData);
}
if (j % 2 == 1)
{
table.getDefaultCell().setGrayFill(1);
}
}
document.add(table);
}
document.close();
}
catch (Exception e)
{
logFacility.log(Priority.ERROR, e.getMessage(), e);
e.printStackTrace();
}
}
4、 在aciton类中调用方法。下面举例Report03AAction
在此类中的exportExcel()方法中,通过jsp页面添加select选项,供用户进行导出类型选择,相关代码如下,
<input type="hidden" name="exportType" id="exportType" value=""/>
function transType()
{
var type=document.getElementById("excel").value;
document.getElementById("exportType").value=type;
document.getElementById("exportForm").submit();
}
String exportType = request.getParameter("exportType");
if(null !=exportType)
{
if(exportType.equals("Excel"))
{
exlPrint.createExl(fileNameMap, response);
}
elseif(exportType.equals("PDF"))
{
exlPrint.createPdf2(fileNameMap, response);
}
else
{
exlPrint.createExl(fileNameMap, response);
}
}
以上即导出pdf文件的相关指导说明。
详细可以参考文档相关代码进行实现。,,