itext生成pdf文件
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try{
PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//Sample1
// document.add(new Paragraph("Hello World"));
// document.add(new Paragraph("First page of the document."));
// document.add(new Paragraph("Some more text on the first page with different color and font type.",
// FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
//Sample2
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16,Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This ext comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
Table t = new Table(3,2);
t.setBorderColor(new Color(220, 255, 100));
t.setPadding(5);
t.setSpacing(5);
t.setBorderWidth(1);
Cell c1 = new Cell("header1");
c1.setHeader(true);
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
t.endHeaders();
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
List l = new List(true, false, 10);
l.add(new ListItem("First item of list"));
l.add(new ListItem("Second item of list"));
section1.add(l);
document.add(chapter1);
}catch(DocumentException de){
de.printStackTrace();
System.err.println("A Document error:" +de.getMessage());
}
document.close();
response.setContentType("application/pdf");
//添加下面一段,点击提示“另存为”,否则直接在浏览器中打开
response.setHeader("Content-Disposition", "attachment;filename=\"HelloWord.pdf\"");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
function getPDF()
{
document.location.href="servlet/GetPDF";
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Document document = new Document(PageSize.A4, 36,36,36,36);
ByteArrayOutputStream ba = new ByteArrayOutputStream();
try{
PdfWriter writer = PdfWriter.getInstance(document, ba);
document.open();
//Sample1
// document.add(new Paragraph("Hello World"));
// document.add(new Paragraph("First page of the document."));
// document.add(new Paragraph("Some more text on the first page with different color and font type.",
// FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));
//Sample2
Paragraph title1 = new Paragraph("Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 18, Font.BOLDITALIC, new Color(0, 0, 255)));
Chapter chapter1 = new Chapter(title1, 1);
chapter1.setNumberDepth(0);
Paragraph title11 = new Paragraph("This is Section 1 in Chapter 1",
FontFactory.getFont(FontFactory.HELVETICA, 16,Font.BOLD, new Color(255, 0, 0)));
Section section1 = chapter1.addSection(title11);
Paragraph someSectionText = new Paragraph("This ext comes as part of section 1 of chapter 1.");
section1.add(someSectionText);
someSectionText = new Paragraph("Following is a 3 X 2 table.");
section1.add(someSectionText);
Table t = new Table(3,2);
t.setBorderColor(new Color(220, 255, 100));
t.setPadding(5);
t.setSpacing(5);
t.setBorderWidth(1);
Cell c1 = new Cell("header1");
c1.setHeader(true);
t.addCell(c1);
c1 = new Cell("Header2");
t.addCell(c1);
c1 = new Cell("Header3");
t.addCell(c1);
t.endHeaders();
t.addCell("1.1");
t.addCell("1.2");
t.addCell("1.3");
section1.add(t);
List l = new List(true, false, 10);
l.add(new ListItem("First item of list"));
l.add(new ListItem("Second item of list"));
section1.add(l);
document.add(chapter1);
}catch(DocumentException de){
de.printStackTrace();
System.err.println("A Document error:" +de.getMessage());
}
document.close();
response.setContentType("application/pdf");
//添加下面一段,点击提示“另存为”,否则直接在浏览器中打开
response.setHeader("Content-Disposition", "attachment;filename=\"HelloWord.pdf\"");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setDateHeader("Expires", (System.currentTimeMillis() + 1000));
response.setContentLength(ba.size());
ServletOutputStream out = response.getOutputStream();
ba.writeTo(out);
out.flush();
}
function getPDF()
{
document.location.href="servlet/GetPDF";
}