itext 设置字体问题

济南大飞哥 2011-08-26 10:56:50
实用itext生成pdf文档,向里面写入汉字很多种方式,目前我采用PdfContentByte tPcd = tWriter.getDirectContent();
用PdfContentByte的showTextAligned方法写入,它在写入之前要求设置字体,问题是字体必须是BaseFont,而我的基础字体是
BaseFont tBFChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.EMBEDDED); 如果想要设置粗体咋办?两外单元格cell的字体又改如何设置??
...全文
7242 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
rerenmen1 2012-01-05
  • 打赏
  • 举报
回复

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();



这段是绝对定位的演示代码,不过要注意,itext的坐标原点,也就是0,0是在左下角。
风尘中国 2011-08-29
  • 打赏
  • 举报
回复

那你想要怎么“绝对定位”呢?

锚点我没有做过
[Quote=引用 7 楼 flyfeifei66 的回复:]

Paragraph par = new Paragraph("bold paragraph");
par.getFont().setStyle(Font.BOLD);
document.add(par);

使用Paragraph 当然可以方便的设置字体,但是无法绝对定位。。
[/Quote]
济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复



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页写入完成");

济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
Paragraph par = new Paragraph("bold paragraph");
par.getFont().setStyle(Font.BOLD);
document.add(par);

使用Paragraph 当然可以方便的设置字体,但是无法绝对定位。。
济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
还有如何向table里添加锚点啊?晕,单元格不允许加链接锚点,而要求又必须加载表格中。。
风尘中国 2011-08-29
  • 打赏
  • 举报
回复
咱们用的版本不一样,我用的是iText5,这里给你贴一个Demo设置加粗倾斜什么的都有,你可以下载iText5和Asian.jar搭建iText5的环境运行下
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);
}
}
}

济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
自己顶~~~~~~~~~~~
济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
问题都基本得到解决,感谢熊猫大侠的大力支持,结贴。
济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
绝对定位就是指定 具体的位置啊(X,Y),用具体的坐标值控制。。
大部分字体我用的是基本字体,只有黑体用的windows的,这么取的。
BaseFont tBFChinese= BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
BaseFont tBFHei = BaseFont.createFont("c:\\windows\\fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
BaseFont tBFXinWei = BaseFont.createFont("c:\\windows\\fonts\\STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

后两个基本字体分别时黑体和华文新魏。

绝对定位table是因为我的这页内容上下都是固定死的。中间的table部分是从数据库里读的,行数是变的。。
风尘中国 2011-08-29
  • 打赏
  • 举报
回复
无法跟你交流,什么叫做“绝对定位文字”,你自己自言自语还上CSDN发帖干啥呢?
你的绝对定位文字也不过是用windows的楷体、仿宋体的意思,干嘛说什么“绝对定位”,接下来的什么绝对定位table更不知道了,你自己弄吧,代码狂人,哈哈
济南大飞哥 2011-08-29
  • 打赏
  • 举报
回复
问题一:绝对定位文字时如何设置字体?已经解决,办法是取windows的字体,感觉也不是很好。。
问题二:table中如何添加链接?已经解决,办法就是直接加,可能原来用的对象不是一个,反正现在能加了。
问题三:如何绝对定位table?没有解决。现在的问题简单的说就是pdfptable和table的使用问题。普通table
貌似无法定位,没有writeSelectedRows方法。pdfptable有,但是又没有setRowspan方法,只能通过嵌套,
因为目前绝对定位输出合适的样式是硬性要求,选择了pdfptable,通过嵌套实现rowspan。
这种方法的弊端就是会多出一些线条。然后我通过判断第行个和最后一行,然后将其上下左右边框设为0的办法,
也基本实现了。问题就是嵌套的表格的线没有和外面的表格的线接上,有点空隙。见图。。。

济南大飞哥 2011-08-26
  • 打赏
  • 举报
回复
iText-2.0.8.jar
itext.jar
iTextAsian.jar

⊙﹏⊙b汗一下,lib下有两个,也不知道引的哪一个,
基版设置好的
风尘中国 2011-08-26
  • 打赏
  • 举报
回复
你用的iText版本是多少?
济南大飞哥 2011-08-26
  • 打赏
  • 举报
回复
另外如何设置table的绝对位置和行高?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧