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 }

转载于:https://www.cnblogs.com/daohangtaiqian/p/5067299.html

java导出word(带图片)相关推荐

  1. java freemarker导出word 带图片,文件导出后图片无法使用office正常打开

    问题记录:java freemark导出word 带图片,文件导出后图片无法使用office正常打开,解决之! 现象:wps打开正常,office如下 修改点: 图片${image1}标签前后保证无空 ...

  2. java导出excel带图片_JAVA的poi实现模版导出excel(带图片).doc

    JAVA的poi实现模版导出excel(带图片) 下面是本人使用java的poi实现使用模板到处excel,内容包含图片,使用两种不同的方式实现其到处excel.但是使用jxl实现到处excel只能到 ...

  3. vue 导出word带图片

    下载依赖 包 npm install docxtemplater npm install pizzip npm install jszip npm install jszip-utils npm in ...

  4. poi使用模板导出word带图片

    1.下面是我做的加载模板导出带图片的word的导出方式,使用之前请映入freemarker的jar包,可以从百度下载即可. 2.首先准备到你要导出的word模板,在要填充的模板中填入el表达式,如下图 ...

  5. 使用springboot和freemarker导出word带图片

    1.首先,要有一个模板word文件(.doc)  里面的样式要先调整好,需要放图片的地方加一个图片 占位  文字内容也要加一些内容占位 2.然后把word转成.ftl格式文件  很贱单  先把word ...

  6. java 导出word 带复选框和图片

    1.打开业务提供的word模板,里面需要填充的地方,设置好占位符:${},然后点击另存为Word XML文档,再将后缀名修改为.ftl文件 2.将ftl后缀的文件放到项目目录下 3.复选框处理: da ...

  7. freeMarker导出word带图片

    1.maven导入需要的jar包 <!-- 引入freeMarker的依赖包. --><dependency><groupId>org.springframewor ...

  8. java 导出word 带格式_java 导出数据为word文档(保持模板格式)

    导出数据到具体的word文档里面,word有一定的格式,需要保持不变 这里使用freemarker来实现: ①:设计好word文档格式,需要用数据填充的地方用便于识别的长字符串替换  如  aaaaa ...

  9. POI导出word带图片及本地测试没问题,在服务器上找不到模板的问题

    https://gitee.com/hffs/poi-tl 官网:http://deepoove.com/poi-tl/ 依赖 poi-tl需要poi版本高于4.1.1,但是poi版本4.1.1没有X ...

最新文章

  1. Codeforces 629D Babaei and Birthday Cake(树状数组优化dp)
  2. Nat. Mach. Intell. | 基于深度强化学习寻找网络中的关键节点
  3. Linus Torvalds 谈居家办公:不要在家中重新搞一个办公室
  4. tomcat常用的优化和配置
  5. 开发75条(写的不错) 选择自 churujianghu 的 Blog
  6. Java中使用Jedis连接Redis服务端时提示:JedisConnectionException: Failed connecting
  7. unicode 字符集环境下的mfc 读写 ini 配置文件的_WSL:在Windows下优雅地玩Linux
  8. Spark基础学习笔记19:RDD的依赖与Stage划分
  9. Go 语言基础(三)——基本数据类型
  10. GIS-python学习
  11. JDK 8.0 新特性——接口默认方法与静态方法
  12. 山东理工ACM 1603 Least Common Multiple
  13. 名人名言大全API 推荐
  14. c#中的反射的高级语法_Kubernetes第3部分中的seccomp新语法以及一些高级主题
  15. taobao.item.update.listing( 一口价商品上架 )
  16. 论文导读:Deep Attentive Learning for Stock Movement Prediction From Social Media Text and Company Correl
  17. opencv学习-高斯金字塔和拉普拉斯金字塔
  18. html无序列表透明圆点,html无序列表的类型type
  19. AD出现 “Net Tie failed verification”如何解决?
  20. 一些常用的「a」标签分享方法

热门文章

  1. 用Location对象和history对象修改页面url
  2. date样式找不到_涡轮+国VI排放,顶配售价不到12万,家用轿车看它准没错
  3. boost 递归锁_c++/boost互斥量与锁
  4. 循环结果添加到集合_Java Note-数据结构(4)集合
  5. php网站后台密码加密,thinkphp 后台登陆密码加密传入密钥
  6. mysql 南邮ctf_南邮ctf web
  7. yota3墨水屏设置_【YotaPhoneYOTA3评测】外观:没有全面屏但有墨水屏_YotaPhone YOTA 3_手机评测-中关村在线...
  8. php 原生多图上传,php 原生多图文件上传
  9. 设置eclipse默认用户名
  10. 一行代码值 200 万?雷军公开小米新 Logo 引吐槽