67,513
社区成员
发帖
与我相关
我的任务
分享
cb.beginText();
cb.setFontAndSize(bf_helv, 12);
String text = "Sample text for alignment";
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, y_line1, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, y_line2, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, y_line3, 0);
cb.endText();
private boolean createPdfCont()
{
try {
/* 获取当日文件的存放路径,若不存在则创建文件路径 */
if(!getFilePath())
{
return false;
}
/* 创建一个文件,并获得它的写入流,将其打开 */
Document tDoc = new Document(PageSize.A4, 50, 50, 50, 50);
PdfWriter tWriter = PdfWriter.getInstance(tDoc, new FileOutputStream(mFileFullPath.toString()));
tDoc.open();
/* 获得文本写入器 */
PdfContentByte tPcd = tWriter.getDirectContent();
/* 创建合同需要的所有基本字体 */
BaseFont tBFChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED);
mBoldConFont = new Font(tBFChinese, 10, Font.BOLD);
mConFont = new Font(tBFChinese, 10);
/*******************************************开始处理合同第一页**************************************/
/* 加载带有logo的图片 */
Image tImgCompany = Image.getInstance(mImgPath+"logo.jpg");
/* 设置logo的位置 */
tImgCompany.setAbsolutePosition(50, 500);
/* 设置logo的大小 */
tImgCompany.scaleAbsolute(200, 50);
/* 加载印有'保险合同'字样的图片 */
Image tImgCont = Image.getInstance(mImgPath+"cont.jpg");
/* 设置图片的位置*/
tImgCont.setAbsolutePosition(50, 430);
/* 设置图片的大小 */
tImgCont.scaleAbsolute(280, 70);
/* 将以上图片写入文件 */
tDoc.add(tImgCompany);
tDoc.add(tImgCont);
/* 设置第一页的文字显示信息 */
tPcd.beginText();
tPcd.setFontAndSize(tBFChinese, 10);
/* 显示产品名称 */
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT, this.mContPlanGrpName , 60, 420, 0);
/* 显示合同号 */
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保险合同号:"+this.mContNo, 420, 387, 0);
/* 显示投保人名字 */
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"投 保 人:"+this.mName, 420, 367, 0);
/* 显示页数 */
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"第一页,共五页", 280, 50, 0);
tPcd.endText();
/* 换页 */
tDoc.newPage();
System.out.println("第一页写入完成");
/*****************************************开始处理合同第二页**************************************/
/* 插入顶头图片 */
Image tImgComName = Image.getInstance(mImgPath+"companyname.jpg");
tImgComName.setAbsolutePosition(147, 750);
tImgComName.scaleAbsolute(300, 50);
tDoc.add(tImgComName);
tPcd.beginText();
/* 写入保险单大标题 */
tPcd.setFontAndSize(tBFChinese, 20);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保 险 单", 250, 710, 0);
/* 设置正文行间距 */
int tHeight = 16;
/* 设置正文最高像素 */
int tStartHeight = 700;
/* 设置正文左边距*/
int tLeftWidth = 50;
/*设置一行有两列的第二列左边距*/
int tRigthWidth = 370;
/* 更改成小字体,开始写正文 */
tPcd.setFontAndSize(tBFChinese, 10);
/* PDF中显示的正文文字信息 */
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"币值单位:人民币元", tLeftWidth, tStartHeight-tHeight*1, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*2, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"保险合同号:"+this.mContNo, tLeftWidth, tStartHeight-tHeight*3, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"合同生效日:"+this.mStartDate.substring(0,4)+"年"+this.mStartDate.substring(5,7)+"月"+this.mStartDate.substring(8,10)+"日0时",tRigthWidth, tStartHeight-tHeight*3, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*4, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"投保人", tLeftWidth, tStartHeight-tHeight*5, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"姓名:"+this.mName, tLeftWidth, tStartHeight-tHeight*6, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"性别:"+this.mSex, tRigthWidth, tStartHeight-tHeight*6, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"出生日期:"+this.mBirtth.substring(0,4)+"年"+this.mBirtth.substring(5,7)+"月"+this.mBirtth.substring(8,10)+"日", tLeftWidth, tStartHeight-tHeight*7, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"证件号码:"+this.mIDNO, tRigthWidth, tStartHeight-tHeight*7, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"通讯地址:"+this.mAddress, tLeftWidth, tStartHeight-tHeight*8, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"邮政编码:"+this.mZipCode, tLeftWidth, tStartHeight-tHeight*9, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"电子邮箱:"+this.mEmail, tRigthWidth, tStartHeight-tHeight*9, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*10, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"被保险人为投保人本人", tLeftWidth, tStartHeight-tHeight*11, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*12, 0);
this.mBnfNo = mSLCBnfSet.size();
for(int i=1;i<=this.mSLCBnfSet.size();i++)
{
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"身故受益人:"+mSLCBnfSet.get(i).getName(), tLeftWidth, tStartHeight-tHeight*(12+i), 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"证件号码:"+mSLCBnfSet.get(i).getIDNo(), 160, tStartHeight-tHeight*(12+i), 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"受益顺序:"+i, 300, tStartHeight-tHeight*(12+i), 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"受益份额:"+mSLCBnfSet.get(i).getBnfLot()+"%",tRigthWidth,tStartHeight-tHeight*(12+i), 0);
}
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, tStartHeight-tHeight*(13+this.mBnfNo), 0);
tPcd.setFontAndSize(tBFChinese, 10);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"君龙守护保障计划", tLeftWidth, tStartHeight-tHeight*(14+this.mBnfNo), 0); --(这里想弄成粗体)
mTable = this.getItemTable();
/* 按指定位置写入表格 */
mTable.writeSelectedRows(0, -1,tLeftWidth ,tStartHeight-tHeight*(14.2f+this.mBnfNo),tPcd);
/* 准备写入合同尾部信息 */
tPcd.beginText();
tPcd.setFontAndSize(tBFChinese, 10);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,this.mPrem, tLeftWidth, tStartHeight-tHeight*(16.5f+this.mBnfNo+this.mItemRow), 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"具体保险责任以条款所载内容为准。", tLeftWidth, tStartHeight-tHeight*(17.5f+this.mBnfNo+this.mItemRow), 0);
if((tStartHeight-tHeight*(17.5f+this.mBnfNo+this.mItemRow))<= 150 + tHeight*3)
{
System.out.println("警告:此页信息有可能因为篇幅问题而发生重叠!");
}
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"-------------------------------------------------------------------------------------------------------------------------------------------------", tLeftWidth, 150 + tHeight*3, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"签发机构:"+this.mComCode, tLeftWidth, 150 + tHeight*2, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"合同成立日期:"+this.mStartDate.substring(0,4)+"年"+this.mStartDate.substring(5,7)+"月"+this.mStartDate.substring(8,10)+"日", tRigthWidth, 150 + tHeight*2, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"服务地址:"+this.mServiceAddr, tLeftWidth, 150 + tHeight*1, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"邮政编码:"+this.mSerAddZipCode, tRigthWidth, 150 + tHeight*1, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"全国服务电话:400-666-0123", tLeftWidth, 150, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"网址:www.kdlins.com.cn", tRigthWidth, 150, 0);
tPcd.showTextAligned(PdfContentByte.ALIGN_LEFT,"第二页,共五页", 280, 50, 0);
tPcd.endText();
/* 插入图章图片 */
Image tImgStamp = Image.getInstance(mImgPath+"stamp.jpg");
tImgStamp.setAbsolutePosition(380, 70);
tImgStamp.scaleAbsolute(130, 120);
tDoc.add(tImgStamp);
tDoc.newPage();
System.out.println("第2页写入完成");
public class Demo {
public static void main (String[] args) {
// creation of the document with a certain size and certain margins
// may want to use PageSize.LETTER instead
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
// creation of the different writers
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("etc/demo.pdf"));
writer.setBoxSize("art", new Rectangle(36, 54, 559, 788));
HeaderFooter event = new HeaderFooter();
writer.setPageEvent(event);
// various fonts
BaseFont bf_helv = BaseFont.createFont(BaseFont.HELVETICA, "Cp1252", false);
BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, "Cp1252", false);
BaseFont bf_courier = BaseFont.createFont(BaseFont.COURIER, "Cp1252", false);
BaseFont bf_symbol = BaseFont.createFont(BaseFont.SYMBOL, "Cp1252", false);
int y_line1 = 650;
int y_line2 = y_line1 - 50;
int y_line3 = y_line2 - 50;
document.open();
PdfContentByte cb = writer.getDirectContent();
cb.setLineWidth(0f);
cb.moveTo(250, y_line3 - 100);
cb.lineTo(250, y_line1 + 100);
cb.moveTo(50, y_line1);
cb.lineTo(400, y_line1);
cb.moveTo(50, y_line2);
cb.lineTo(400, y_line2);
cb.moveTo(50, y_line3);
cb.lineTo(400, y_line3);
cb.stroke();
cb.beginText();
cb.setFontAndSize(bf_helv, 12);
String text = "Sample text for alignment";
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, text + " Center", 250, y_line1, 0);
cb.showTextAligned(PdfContentByte.ALIGN_RIGHT, text + " Right", 250, y_line2, 0);
cb.showTextAligned(PdfContentByte.ALIGN_LEFT, text + " Left", 250, y_line3, 0);
cb.endText();
document.newPage();
// add text in two paragraphs from top to bottom
Paragraph par = new Paragraph("bold paragraph");
par.getFont().setStyle(Font.BOLD);
document.add(par);
par = new Paragraph("italic paragraph");
par.getFont().setStyle(Font.ITALIC);
// par.getFont().setStyle(Font.BOLD);
document.add(par);
par = new Paragraph("underlined and strike-through paragraph");
par.getFont().setStyle(Font.UNDERLINE | Font.STRIKETHRU);
document.add(par);
// demonstrate some table features
PdfPTable table = new PdfPTable(3);
table.setSpacingBefore(20);
table.getDefaultCell().setPadding(5);
PdfPCell cell = new PdfPCell(new Phrase("header"));
cell.setPadding(5);
cell.setColspan(3);
table.addCell(cell);
table.setHeaderRows(1);
cell = new PdfPCell(new Phrase("example cell with rowspan 2 and red border"));
cell.setPadding(5);
cell.setRowspan(2);
cell.setBorderColor(new BaseColor(new Color(255, 0, 0)));
table.addCell(cell);
table.addCell("中文");
table.addCell("2.1");
table.addCell("1.2");
table.addCell("2.2");
cell = new PdfPCell(new Phrase("align center"));
cell.setPadding(5);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
table.addCell(cell);
cell = new PdfPCell(new Phrase("rotated cell"));
cell.setPadding(5);
cell.setRowspan(2);
cell.setColspan(2);
// PdfPCell content can be rotated
cell.setRotation(90);
table.addCell(cell);
cell = new PdfPCell(new Phrase("align right"));
cell.setPadding(5);
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
document.add(table);
// add text at an absolute position
cb.beginText();
cb.setFontAndSize(bf_times, 14);
cb.setTextMatrix(100, 300);
cb.showText("Text at position 100, 300.");
cb.endText();
// rotated text at an absolute position
PdfTemplate template = cb.createTemplate(300, 300);
template.beginText();
template.setFontAndSize(bf_times, 14);
template.showText("Rotated text at position 400, 200.");
template.endText();
float rotate = 90;
float x = 400;
float y = 200;
float angle = (float) (-rotate * (Math.PI / 180));
float xScale = (float) Math.cos(angle);
float yScale = (float) Math.cos(angle);
float xRot = (float) -Math.sin(angle);
float yRot = (float) Math.sin(angle);
cb.addTemplate(template, xScale, xRot, yRot, yScale, x, y);
document.close();
} catch (Exception ex) {
System.err.println(ex.getMessage());
}
}
/** Inner class to add a header and a footer. */
static class HeaderFooter extends PdfPageEventHelper {
public void onEndPage (PdfWriter writer, Document document) {
Rectangle rect = writer.getBoxSize("art");
switch(writer.getPageNumber() % 2) {
case 0:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_RIGHT, new Phrase("even header"),
rect.getRight(), rect.getTop(), 0);
break;
case 1:
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_LEFT, new Phrase("odd header"),
rect.getLeft(), rect.getTop(), 0);
break;
}
ColumnText.showTextAligned(writer.getDirectContent(),
Element.ALIGN_CENTER, new Phrase(String.format("page %d", writer.getPageNumber())),
(rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
}
}
}