java word 图片_java导出word(带图片)

1 public classCreateWordDemo {2 public void createDocContext(String file) throwsDocumentException,IOException {3 //设置纸张大小

4 Document document = newDocument(PageSize.A4);5 //建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file));

6 document.open();7 //设置中文字体

8 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);9 //标题字体风格

10 Font titleFont = new Font(bfChinese, 12, Font.BOLD);11 //正文字体风格

12 Font contextFont = new Font(bfChinese, 10, Font.NORMAL);13 Paragraph title = new Paragraph("标题");14 //设置标题格式对齐方式

15 title.setAlignment(Element.ALIGN_CENTER);16 title.setFont(titleFont);17 document.add(title);18 String contextString = "iText是一个能够快速产生PDF文件的java类库。"

19 + " \n"//换行 + "iText的java类对于那些要产生包含文本,"

20 + "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。"

21 + "使用iText与PDF能够使你正确的控制Servlet的输出。";22 Paragraph context = newParagraph(contextString);23 //正文格式左对齐

24 context.setAlignment(Element.ALIGN_LEFT);25 context.setFont(contextFont);26 //离上一段落(标题)空的行数

27 context.setSpacingBefore(5);28 //设置第一行空的列数

29 context.setFirstLineIndent(20);30 document.add(context);31 //利用类FontFactory结合Font和Color可以设置各种各样字体样式

32

33 Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255)));34 document.add(underline);35 //设置 Table 表格

36 Table aTable = new Table(3);37 int width[] = { 25, 25, 50};38 aTable.setWidths(width);//设置每列所占比例

39 aTable.setWidth(90); //占页面宽度 90%

40 aTable.setAlignment(Element.ALIGN_CENTER);//居中显示

41 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示

42 aTable.setAutoFillEmptyCells(true); //自动填满

43 aTable.setBorderWidth(1); //边框宽度

44 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色

45 aTable.setPadding(2);//衬距,看效果就知道什么意思了

46 aTable.setSpacing(3);//即单元格之间的间距

47 aTable.setBorder(2);//边框//设置表头

48

49 Cell haderCell = new Cell("表格表头");50 haderCell.setHeader(true);51 haderCell.setColspan(3);52 aTable.addCell(haderCell);53 aTable.endHeaders();54 Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN);55 Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese)); cell.setVerticalAlignment(Element.ALIGN_TOP);56 cell.setBorderColor(new Color(255, 0, 0));57 cell.setRowspan(2);58 aTable.addCell(cell);59 aTable.addCell(new Cell("#1"));60 aTable.addCell(new Cell("#2"));61 aTable.addCell(new Cell("#3"));62 aTable.addCell(new Cell("#4"));63 Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese));64 cell3.setColspan(3);65 cell3.setVerticalAlignment(Element.ALIGN_CENTER);66 aTable.addCell(cell3);67 document.add(aTable);68 document.add(new Paragraph("\n"));69 //添加图片 Image.getInstance即可以放路径又可以放二进制字节流

70 Image img = Image.getInstance("d:\\img01800.jpg");71 img.setAbsolutePosition(0, 0);72 img.setAlignment(Image.RIGHT);//设置图片显示位置

73 img.scaleAbsolute(60, 60);//直接设定显示尺寸74 //img.scalePercent(50);//表示显示的大小为原尺寸的50%75 //img.scalePercent(25, 12);//图像高宽的显示比例76 //img.setRotation(30);//图像旋转一定角度

77 document.add(img);78 document.close();79 }80

81 public static voidmain(String[] args) {82 CreateWordDemo word = newCreateWordDemo();83 String file = "d:/demo1.doc";84 try{85 word.createDocContext(file);86 } catch(DocumentException e) {87 e.printStackTrace();88 } catch(IOException e) {89 e.printStackTrace();90 }91 }92 }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值