public static PdfPCell createCellTL(String value,
int align, Font font,int colspan) {//上左有边框的表格:1.表格内容;2.字体;3.对齐方式;4.横跨列数
PdfPCell cell = new PdfPCell()
//cell.setVerticalAlignment(Element.ALIGN_MIDDLE)
cell.setHorizontalAlignment(align) //水平对齐
cell.setColspan(colspan)
cell.setPhrase(new Phrase(value,textfont))//设置表格的短语
cell.setPadding(5.0f) //设置内间距
cell.disableBorderSide(2) //隐藏下右边框
cell.disableBorderSide(8)
return cell
}
public static void main(String[] args) throws Exception {// 创建word文档,并设置纸张的大小
Document document = new Document(PageSize.A4)
RtfWriter2.getInstance(document, new FileOutputStream(
"D:/test/word.doc"))
// 打开document
document.open()
// 设置字体,字号,加粗,颜色
Font font = new Font(Font.NORMAL, 20, Font.BOLD, new Color(255, 0, 0))
// 设置新的段落,使其字体为font
Paragraph p = new Paragraph("出口合同", font)
// 设置段落居中,其中1为居中对齐,2为右对齐,3为左对齐
p.setAlignment(1)
// 文档中加入该段落
document.add(p)
font = new Font(Font.NORMAL, 16, Font.NORMAL, new Color(0, 0, 0))
p = new Paragraph(
" 南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院南京大学软件学院",
font)
// 设置段落缩进
p.setIndentationLeft(10)
// 设置首行缩进
p.setFirstLineIndent(20f)
// 设置段后距和段前距
p.setSpacingAfter(10f)
p.setSpacingBefore(100f)
document.add(p)
p = new Paragraph("徐州审委会", font)
document.add(p)
// 创建有三列的表格
Table table = new Table(2, 3)
// 设置table的边框宽度为0
table.setBorderWidth(1f)
table.setAbsWidth("120px")
// 其中1为居中对齐,2为右对齐,3为左对齐
table.setAlignment(2)
// table.setPadding(0)
// table.setSpacing(0)
// 读取图片(参数为gif、jpg、png格式的图片都可以),设置图片大小
Image image = Image.getInstance("D:/test/1.jpg")
// Image img = Image.getInstance(new URL("http://xxx.com/logo.jpg)")
// 设置图片的绝对大小,宽和高
image.scaleAbsolute(50f, 50f)
// 设置图片居中显示
image.setAlignment(Image.MIDDLE)
// 创建单元格,并且将单元格内容设置为图片
Cell cell = new Cell(image)
// 设置单元格边框为0
cell.setBorder(0)
// cell.setHeader(true)
// cell.setColspan(3)// 设置表格为三列
// cell.setRowspan(3)// 设置表格为三行
table.addCell(cell)
// table.endHeaders()// 表头结束
table.addCell(cell)
table.addCell(cell)
cell = new Cell("我是田有朋")
// cell.setWidth("10px")
table.addCell(cell)
// cell.setBorder(1)
// 设置垂直居中
cell.setVerticalAlignment(1)
// 设置水平居中
cell.setHorizontalAlignment(1)
// document.add(new Paragraph("用java生成word文件"))
document.add(table)
// 关闭document
document.close()
}
public static void main(String[] args) throws Exception {// 创建word文档,并设置纸张的大小
Document document = new Document(PageSize.A4)
RtfWriter2.getInstance(document, new FileOutputStream(
"D:/test/word.doc"))
// 打开document
document.open()
// 设置字体,字号,加粗,颜色
Font font = new Font(Font.NORMAL, 20, Font.BOLD, new Color(255, 0, 0))
// 设置新的段落,使其字体为font
Paragraph p = new Paragraph("出口合同", font)
// 设置段落居中,其中1为居中对齐,2为右对齐,3为左对齐
p.setAlignment(1)
// 文档中加入该段落
document.add(p)
font = new Font(Font.NORMAL, 16, Font.NORMAL, new Color(0, 0, 0))
p = new Paragraph(
" 南京",
font)
// 设置段落缩进
p.setIndentationLeft(10)
// 设置首行缩进
p.setFirstLineIndent(20f)
// 设置段后距和段前距
p.setSpacingAfter(10f)
p.setSpacingBefore(100f)
document.add(p)
p = new Paragraph("徐州审委会", font)
document.add(p)
// 创建有三列的表格
Table table = new Table(2, 3)
// 设置table的边框宽度为0
table.setBorderWidth(1f)
table.setAbsWidth("120px")
// 其中1为居中对齐,2为右对齐,3为左对齐
table.setAlignment(2)
// table.setPadding(0)
// table.setSpacing(0)
// 读取图片(参数为gif、jpg、png格式的图片都可以),设置图片大小
Image image = Image.getInstance("D:/test/1.jpg")
// Image img = Image.getInstance(new URL("http://xxx.com/logo.jpg)")
// 设置图片的绝对大小,宽和高
image.scaleAbsolute(50f, 50f)
// 设置图片居中显示
image.setAlignment(Image.MIDDLE)
// 创建单元格,并且将单元格内容设置为图片
Cell cell = new Cell(image)
// 设置单元格边框为0
cell.setBorder(0)
// cell.setHeader(true)
// cell.setColspan(3)// 设置表格为三列
// cell.setRowspan(3)// 设置表格为三行
table.addCell(cell)
// table.endHeaders()// 表头结束
table.addCell(cell)
table.addCell(cell)
cell = new Cell("我是田有朋")
// cell.setWidth("10px")
table.addCell(cell)
// cell.setBorder(1)
// 设置垂直居中
cell.setVerticalAlignment(1)
// 设置水平居中
cell.setHorizontalAlignment(1)
// document.add(new Paragraph("用java生成word文件"))
document.add(table)
// 关闭document
document.close()
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)