IText 生成PDF
程序员文章站
2022-03-16 15:11:23
...
public static writePDF(){
Document document = new Document();
FileOutputStream os = new FileOutputStream(new File(Const.FILE_IN + File.separator + foldPath + File.separator + fileName));
PdfWriter.getInstance(document, os);
document.open();
BaseFont bfChinese = BaseFont.createFont("STSong-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);//加入document: ;
Font FontChineseB = new Font(bfChinese, 12, Font.BOLD);//加入document:
Image png = Image.getInstance(Const.FILE_PROCECT + File.separator + "images" + File.separator + "pdf_head.jpg");//图片的地址
document.add(png);
Paragraph head = new Paragraph("预定通知单 订单号:" + String.valueOf(hotelChOrder.getId()), FontChineseB);
head.setAlignment(Element.ALIGN_CENTER);
document.add(head);
MemberDomain memberDomain = (MemberDomain)BeanFactory.getDomain(Const.MEMBER_DOMAIN);
memberDomain.initByMemberId(TypeParse.str2Long(vo.getHotelMemberId()));
UserDomain userDomain = (UserDomain)BeanFactory.getDomain(Const.USER_DOMAIN);
userDomain.initByMemberUserId(memberDomain.getMember().getManager());
document.add(new Paragraph("To:"+memberDomain.getMember().getMemberCn()+" 联系人:"+userDomain.getMemberUser().getUserName()+"("+userDomain.getMemberUser().getMobile()+") 预定时间:"+systemDate, FontChinese));
document.add(new Paragraph("Tel:"+memberDomain.getMember().getTel()+" Fax:"+memberDomain.getMember().getFax()+"", FontChinese));
Table pdfLine = new Table(1,1);
pdfLine.setBorderColor(new Color(0, 0, 0));
pdfLine.setBorderWidth(1);
pdfLine.setWidth(100);
Cell lineCell = new Cell(new Paragraph("", FontChinese));
pdfLine.addCell(lineCell);
document.add(pdfLine);
Table details = new Table(2,9);
details.setBorderColor(new Color(255, 255, 255));
details.setBorderWidth(0);
details.setBorder(0);
details.setWidth(80);
int width[] = {20,60};
details.setWidths(width);
details.setPadding(0);
Paragraph leftCellParagraph = new Paragraph("客人姓名:", FontChineseB);
leftCellParagraph.setAlignment(Element.ALIGN_RIGHT);
Cell detailsCell = new Cell(leftCellParagraph);
detailsCell.setBorderColor(new Color(255, 255, 255));
detailsCell.setBorder(0);
detailsCell.setBorderWidth(0);
details.addCell(detailsCell);
String usernames = hotelChOrder.getUserList().replaceAll(";", ",");
Paragraph rightCellParagraph = new Paragraph(usernames, FontChinese);
rightCellParagraph.setAlignment(Element.ALIGN_LEFT);
detailsCell = new Cell(rightCellParagraph);
detailsCell.setBorderColor(new Color(255, 255, 255));
detailsCell.setBorder(0);
detailsCell.setBorderWidth(0);
details.addCell(detailsCell);
leftCellParagraph = new Paragraph("住房日期:", FontChineseB);
leftCellParagraph.setAlignment(Element.ALIGN_RIGHT);
detailsCell = new Cell(leftCellParagraph);
detailsCell.setBorderColor(new Color(255, 255, 255));
detailsCell.setBorder(0);
detailsCell.setBorderWidth(0);
details.addCell(detailsCell);
rightCellParagraph = new Paragraph(vo.getCheckInDate() + " 至 "+vo.getCheckOutDate()+" 共"+vo.getDays()+"晚", FontChinese);
rightCellParagraph.setAlignment(Element.ALIGN_LEFT);
detailsCell = new Cell(rightCellParagraph);
detailsCell.setBorderColor(new Color(255, 255, 255));
detailsCell.setBorder(0);
detailsCell.setBorderWidth(0);
details.addCell(detailsCell);
leftCellParagraph = new Paragraph("预定客房:", FontChineseB);
leftCellParagraph.setAlignment(Element.ALIGN_RIGHT);
detailsCell = new Cell(leftCellParagraph);
detailsCell.setBorderColor(new Color(255, 255, 255));
detailsCell.setBorder(0);
detailsCell.setBorderWidth(0);
details.addCell(detailsCell);
document.add(details);
document.close();
}
上一篇: gef中的属性视图小结