做项目时要用到itext,从网上去查,好多都没法用了,我就把自己做的有关itext的整理了一下,希望能给大家一些帮助
1 首先现在好多上面的资料显示的有关itext的导入的架包的版本是itextpdf2的jar包,所以你去查资料的时候会发现很多像这样的,只是版本的问题,
import com.lowagie.text.Document;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
现在itext的jar包的版本是itextpdf-5.2.0.jar,大家可以去http://www.itextpdf.com/进行下载
2 典型的五步创建一个Helloword.pdf
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfTest {
public static void main(String[] args) throws FileNotFoundException, DocumentException {
Document doc=new Document();//第一步创建文档
PdfWriter.getInstance(doc, new FileOutputStream("WebRoot/Helloword.pdf"))//;第二步创建实例,在WebRoot
里面创建一个Helloword.pdf
doc.open();//第三步开启文档
doc.add(new Paragraph("hello"));//在文档内添加信息
doc.close();//关闭文档
}
}
这里有个实例可以帮助大家更深入的了解:
在index.jsp中我们写了这样的一个jsp:
<body>
<form action="pdf.php" method="post">//这里的pdf.php其实是servlet,也就在配置路径的时候将其改为pdf.php
标题: <input type="text" name="title"/><br>
作者:<input type="text" name="author"/><br>
内容:<textarea name="content"></textarea><br>
<input type="submit" value="生成pdf"/>
</form>
</body>
我们创建一个servlet名为PdfServlet:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
// 定义中文字体使其以中文的格式显示
BaseFont bfChinese = null;
try {
bfChinese = BaseFont.createFont("c:\\windows\\fonts\\simsun.ttc,1",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
} catch (Exception e) {
e.printStackTrace();
}
Font fontCN = new Font(bfChinese, 12,
Font.NORMAL,BaseColor.BLUE);
//接收jsp页面的信息
String title=request.getParameter("title");
String author=request.getParameter("author");
String center=request.getParameter("content");
//设置格式为pdf类型的
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment;filename=\"data.pdf\"");//filename是将其以pdf导出时的名字
ServletOutputStream out = response.getOutputStream();
Document doc=new Document();//创建文档
try {
PdfWriter.getInstance(doc, out);//创建实例
doc.open();//开启文档
doc.addAuthor(author);//document里面自带的有添加Author
doc.addTitle(title);//document里面自带的有添加Title
Paragraph pa=new Paragraph(author,fontCN);//设置以段落的格式输出
pa.setAlignment(Element.ALIGN_RIGHT);//设置其排版的格式靠右
Paragraph pt=new Paragraph(title,fontCN);//设置以段落的格式输出
pt.setAlignment(Element.ALIGN_CENTER);//设置其排版的格式居中
Paragraph pc=new Paragraph(center,fontCN);
//将段落添加进文档
doc.add(pa);
doc.add(pt);
doc.add(pc);
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
doc.close();//关闭文档
}
}
这样就可以保存为pdf
有关itext的表格:
import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class MyFirstTable {
/** The resulting PDF file. */
public static final String RESULT
= "WebRoot/first_table.pdf";
/**
* Main method.
* @param args no arguments needed
* @throws DocumentException
* @throws IOException
*/
public static void main(String[] args)
throws IOException, DocumentException {
new MyFirstTable().createPdf(RESULT);
}
/**
* Creates a PDF with information about the movies
* @param filename the name of the PDF file that will be created.
* @throws DocumentException
* @throws IOException
*/
public void createPdf(String filename)
throws IOException, DocumentException {
// step 1
Document document = new Document();
// step 2
PdfWriter.getInstance(document, new FileOutputStream(filename));
// step 3
document.open();
// step 4
document.add(createFirstTable());
// step 5
document.close();
}
/**
* Creates our first table
* @return our first table
*/
public static PdfPTable createFirstTable() {
// a table with three columns
PdfPTable table = new PdfPTable(3);
// the cell object
PdfPCell cell;
// we add a cell with colspan 3
cell = new PdfPCell(new Phrase("Cell with colspan 3"));
cell.setColspan(3);
table.addCell(cell);
// now we add a cell with rowspan 2
cell = new PdfPCell(new Phrase("Cell with rowspan 2"));
cell.setRowspan(2);
table.addCell(cell);
// we add the four remaining cells with addCell()
table.addCell("row 1; cell 1");
table.addCell("row 1; cell 2");
table.addCell("row 2; cell 1");
table.addCell("row 2; cell 2");
return table;
}
}