package com.zhaipuhong.j2se.pdf;
import
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import java.awt.Color;
public class PdfServlet extends HttpServlet {
private static final long serialVersionUID = -6033026500372479591L;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// step 1 建立文档对象
Document document = new Document();
try {
//设置文档相应类型
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3 打开文档
document.open();
//支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);
// step 4 向文档中添加内容
document.add(pragraph);
document.add(new Paragraph(" Hello World !"));
document.add(new Paragraph("Date 时间"+new Date().toString()));
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph(new Date().toString()));
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
cell = new PdfPCell(new Paragraph("cell test1"));
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("cell test2"));
cell.setColspan(2);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
document.add(table);
}catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: 关闭文档对象
document.close();
}
//支持中文
public Paragraph getChineseString(String chineseString){
Paragraph pragraph=null;
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
12, com.lowagie.text.Font.NORMAL);
pragraph = new Paragraph(chineseString, FontChinese);
}
catch (Exception ex) {
ex.printStackTrace();
}
return pragraph;
}
}
package com.zhaipuhong.j2se.pdf;
import
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import java.awt.Color;
public class PdfServlet extends HttpServlet {
private static final long serialVersionUID = -6033026500372479591L;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// step 1 建立文档对象
Document document = new Document();
try {
//设置文档相应类型
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3 打开文档
document.open();
//支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);
// step 4 向文档中添加内容
document.add(pragraph);
document.add(new Paragraph(" Hello World !"));
document.add(new Paragraph("Date 时间"+new Date().toString()));
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph(new Date().toString()));
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
cell = new PdfPCell(new Paragraph("cell test1"));
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("cell test2"));
cell.setColspan(2);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
document.add(table);
}catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: 关闭文档对象
document.close();
}
//支持中文
public Paragraph getChineseString(String chineseString){
Paragraph pragraph=null;
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
12, com.lowagie.text.Font.NORMAL);
pragraph = new Paragraph(chineseString, FontChinese);
}
catch (Exception ex) {
ex.printStackTrace();
}
return pragraph;
}
}
import
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import java.awt.Color;
public class PdfServlet extends HttpServlet {
private static final long serialVersionUID = -6033026500372479591L;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// step 1 建立文档对象
Document document = new Document();
try {
//设置文档相应类型
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3 打开文档
document.open();
//支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);
// step 4 向文档中添加内容
document.add(pragraph);
document.add(new Paragraph(" Hello World !"));
document.add(new Paragraph("Date 时间"+new Date().toString()));
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph(new Date().toString()));
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
cell = new PdfPCell(new Paragraph("cell test1"));
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("cell test2"));
cell.setColspan(2);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
document.add(table);
}catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: 关闭文档对象
document.close();
}
//支持中文
public Paragraph getChineseString(String chineseString){
Paragraph pragraph=null;
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
12, com.lowagie.text.Font.NORMAL);
pragraph = new Paragraph(chineseString, FontChinese);
}
catch (Exception ex) {
ex.printStackTrace();
}
return pragraph;
}
}
package com.zhaipuhong.j2se.pdf;
import
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfPCell;
import java.awt.Color;
public class PdfServlet extends HttpServlet {
private static final long serialVersionUID = -6033026500372479591L;
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
// step 1 建立文档对象
Document document = new Document();
try {
//设置文档相应类型
response.setContentType("application/pdf");
PdfWriter.getInstance(document, response.getOutputStream());
// step 3 打开文档
document.open();
//支持中文
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);
// step 4 向文档中添加内容
document.add(pragraph);
document.add(new Paragraph(" Hello World !"));
document.add(new Paragraph("Date 时间"+new Date().toString()));
document.add(new Paragraph(new Date().toString()));
document.add(new Paragraph(new Date().toString()));
PdfPTable table = new PdfPTable(3);
PdfPCell cell = new PdfPCell(new Paragraph("header with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
table.addCell("1.1");
table.addCell("2.1");
table.addCell("3.1");
table.addCell("1.2");
table.addCell("2.2");
table.addCell("3.2");
cell = new PdfPCell(new Paragraph("cell test1"));
cell.setBorderColor(new Color(255, 0, 0));
table.addCell(cell);
cell = new PdfPCell(new Paragraph("cell test2"));
cell.setColspan(2);
cell.setBackgroundColor(new Color(0xC0, 0xC0, 0xC0));
table.addCell(cell);
document.add(table);
}catch(DocumentException de) {
de.printStackTrace();
System.err.println("document: " + de.getMessage());
}
// step 5: 关闭文档对象
document.close();
}
//支持中文
public Paragraph getChineseString(String chineseString){
Paragraph pragraph=null;
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese,
12, com.lowagie.text.Font.NORMAL);
pragraph = new Paragraph(chineseString, FontChinese);
}
catch (Exception ex) {
ex.printStackTrace();
}
return pragraph;
}
}