import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.PdfEncryption;
public class testIText extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try{
//设置图片大小 页面为A4
Document document = new Document(PageSize.A4, 110, 110, 120, 140);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
PdfWriter.getInstance(document, bos);
//1,设置此PDF文件的权限,只有写上的是允许的。这里只允许打印,读取和保存,不允许修改等。
int intPermissions = PdfWriter.AllowPrinting |PdfWriter.AllowScreenReaders;
//2,若要实现其他权限如修改等 则需要写入密码,这里是设置密码加密标准或加密类型。
int intEncryptionType = PdfEncryption.STANDARD_ENCRYPTION_40;
//3,要是用这个方法需要引入一个jar包(bcprov-jdk15-137.jar)。第一个参数:打开时需要的密码;第二个参数:实用其他其他权限时使用的密码;第三个参数:可使用的权限;第四个参数:密码类型
pdfWriter.setEncryption(null, "hello".getBytes(), intPermissions, intEncryptionType);
//注意:以上 1,2,3 若不写也不引入bcprov-jdk15-137.jar这个包则默认拥有全部权限
String path = "D:/webroot/image";
//设置背景图片
Image jpeg = Image.getInstance(path + "/bakeGround.JPG");
//设置图片的位置,参数Image.UNDERLYING是作为文字的背景显示。
jpeg.setAlignment(Image.UNDERLYING);
//设置图片的绝对位置
jpeg.setAbsolutePosition(0, 0);
//使用指定的字体构成了一个基础字体对象。载入日文字库,字体样式类型由MSGOTHIC.TTC给出
BaseFont bfJapanese = BaseFont.createFont(path + "/MSGOTHIC.TTC,1", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
/*字体编码方式,样式直接给出 注:若要显示亚洲国家的字体要引入iTextAsian.jar包。
BaseFont bfJapanese = BaseFont.createFont("HeiseiMin-W3", "UniJIS-UCS2-HW-H", BaseFont.NOT_EMBEDDED);//日语
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-HW-H", BaseFont.NOT_EMBEDDED);//汉语
*/
Font FontJapanese1 = new Font(bfJapanese,38, Font.BOLD);
Font FontJapanese2 = new Font(bfJapanese,25, Font.NORMAL);
String title1 = "";
Paragraph t1 = new Paragraph(title1, FontJapanese1);
/*
setAlignment(int)是设置标签的对齐方式,有三种对齐方式--左,中,右
setAlignment(Label.LEFT)或 setAlignment(Label.CENTER),setAlignment(Label.RIGHT).
Label.LEFT,Label.CENTER,Label.RIGHT分别为常数0,1,2。 默认是0,靠左对齐。
*/
t1.setAlignment(1);
String title2 = "";
Paragraph t2 = new Paragraph(title2 , FontJapanese2);
//设置上端的空白距离
t2.setSpacingBefore(15);
//设置下端的空白距离
t2.setSpacingAfter(15);
//设置段落间离
t2.setLeading(35);
String strPar1 = "";
Paragraph t3 = new Paragraph(strPar1 , FontJapanese2);
//设置缩排
//setIndentationLeft()距左边的距离;
//setIndentationRight() 为距右边的距离
t3.setIndentationLeft(20);
t3.setSpacingAfter(8);
document.open();
document.add(jpeg);
document.add(t1);
document.add(t2);
document.add(t3);
document.close();
response.setContentType("application/pdf");
response.setContentLength(bos.size());
ServletOutputStream out = response.getOutputStream();
bos.writeTo(out);
out.flush();
}catch(Exception e){
}
}
}
/*备注: 一共引入 三个包 1. iTextAsian.jar 2. itext-2.0.5.jar 3.bcprov-jdk15-137.jar 其中iTextAsian.jar是要显示亚洲国家字体时要求引入的包;而itext.jar是iText的类库包; 第三个专为设置权限引入*/