📄 pdfaction.java
字号:
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.accphr.web.action;
import java.awt.Color;
import java.io.ByteArrayOutputStream;
import java.util.Iterator;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.accphr.biz.IStandardsBiz;
import com.accphr.entity.Standards;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PdfAction extends DispatchAction {
private IStandardsBiz standardsBiz;
public void setStandardsBiz(IStandardsBiz standardsBiz) {
this.standardsBiz = standardsBiz;
}
public ActionForward toShow(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return mapping.findForward("toshow");
}
//在浏览器打开
public ActionForward doShow(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/pdf;charset=UTF-8");
response.setHeader("Content-Disposition",
"inline; filename=report.pdf");
Document document = new Document();
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter.getInstance(document, ba);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
document.add(new Paragraph("薪酬标准信息列表", new Font(bfChinese, 30,
Font.BOLD)));
document.add(new Paragraph(" ", new Font(bfChinese, 8, Font.BOLD)));
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
PdfPCell cell = new PdfPCell();
cell.setBackgroundColor(new Color(213, 141, 69));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setPhrase(new Paragraph("编号", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("名称", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("注册人", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("总额", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
Iterator it = this.standardsBiz.findAll().iterator();
while (it.hasNext()) {
Standards standard=(Standards)it.next();
PdfPCell newcell = new PdfPCell();
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getStandardId()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getStandardName()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getRegister()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getSalarySum()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
}
document.add(table);
document.close();
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//保存到本地
public ActionForward doSave(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("application/pdf;charset=UTF-8");
response.setHeader("Content-Disposition",
"attachment; filename=report.pdf");
Document document = new Document();
try {
ByteArrayOutputStream ba = new ByteArrayOutputStream();
PdfWriter.getInstance(document, ba);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
document.add(new Paragraph("薪酬标准信息列表", new Font(bfChinese, 30,
Font.BOLD)));
document.add(new Paragraph(" ", new Font(bfChinese, 8, Font.BOLD)));
PdfPTable table = new PdfPTable(4);
table.setWidthPercentage(100);
table.setHorizontalAlignment(PdfPTable.ALIGN_CENTER);
PdfPCell cell = new PdfPCell();
cell.setBackgroundColor(new Color(213, 141, 69));
cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
cell.setPhrase(new Paragraph("编号", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("名称", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("注册人", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
cell.setPhrase(new Paragraph("总额", new Font(bfChinese, 8,
Font.NORMAL)));
table.addCell(cell);
Iterator it = this.standardsBiz.findAll().iterator();
while (it.hasNext()) {
Standards standard=(Standards)it.next();
PdfPCell newcell = new PdfPCell();
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getStandardId()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getStandardName()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getRegister()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER);
newcell.setPhrase(new Paragraph(String.valueOf(standard.getSalarySum()),
new Font(bfChinese, 8, Font.NORMAL)));
table.addCell(newcell);
}
document.add(table);
document.close();
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -