2019独角兽企业重金招聘Python工程师标准>>> hot3.png

package com.loongtao.report.util;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.HeaderFooter;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import com.lowagie.text.rtf.field.RtfPageNumber;
import com.lowagie.text.rtf.field.RtfTotalPageNumber;
import com.lowagie.text.rtf.style.RtfParagraphStyle;/*** iText导出Word工具类* * @author Mr.gao**/
public class WordUtil {public enum ExportPlan{One,TWO}/*** Word中常用标题级别* @author Mr.gao**/public enum FontLevel{ONE,TWO,THREE}/*** 导出Word* @param doc* @param path* @throws FileNotFoundException*/public void exportWord(Document doc,String path) throws FileNotFoundException{FileOutputStream fos = new FileOutputStream(path);RtfWriter2.getInstance(doc, fos);}/*** 设置Word页眉或者页脚内容及对齐方式* * @param headerContext*           页眉/页脚信息* @param alignment*          对齐方式,例如:Rectangle.ALIGN_CENTER(居中)* @return HeaderFooter*/public HeaderFooter setHeaderFooter(String context,int alignment){HeaderFooter hf = new HeaderFooter(new Phrase(context),false);hf.setAlignment(alignment);return hf;}/*** 设置Word中标题级别,查看FontLevel* @param level*            1-3级* @return RtfParagraphStyle*/public RtfParagraphStyle setFontLevel(FontLevel level,String fontName){RtfParagraphStyle r1 = null;switch (level) {case ONE:r1 = RtfParagraphStyle.STYLE_HEADING_1;r1.setStyle(Font.BOLD);r1.setSpacingAfter(5);break;case TWO:r1 = RtfParagraphStyle.STYLE_HEADING_2;r1.setStyle(Font.NORMAL);r1.setSpacingBefore(15);r1.setSpacingAfter(5);  break;case THREE:r1 = RtfParagraphStyle.STYLE_HEADING_3;r1.setStyle(Font.NORMAL);r1.setIndentLeft(25);r1.setSpacingAfter(15);break;}r1.setFontName(fontName);return r1;}/*** 设置Word基础字体* @param bf*             BaseFont* @param size*          字号* @param fontStyle*           字体样式,例如:com.lowagie.text.Font.BOLD* @return com.lowagie.text.Font*/public Font setFont(BaseFont bf,int size,int fontStyle){Font f = new Font(bf,size,fontStyle);return f;}public Paragraph setPageNumber(){RtfPageNumber number = new RtfPageNumber();Paragraph parafooter = new Paragraph();parafooter.add(new Phrase("第"));parafooter.add(number);parafooter.add(new Phrase("页 共"));parafooter.add(new RtfTotalPageNumber());parafooter.add(new Phrase("页"));return parafooter;}/*** 在Word中创建段落及格式设置* * 注意:<1>如果设置为标题,则将paragraphIdent,rowIndent设置为0即可,所创见的段落如果是一行,那么缩进量=paragraphIdent+rowIdent。* *       <2>其中paragraphIdent为段落缩进,指整个段落的所尽量,而rowIdent为第一行的缩进量* *         <3>建议缩进量加起来范围在【0f-500f】,一般情况建议范围【30f】为两个汉字的缩进量。* *         <4>超出500f则文字继续向后对齐方式的反方向推移,超出Word纸张所显示的范围。* * @param context*          页面内容* @param font*          字体* @param alignment*           对齐方式,例如:Element.ALIGN_CENTER* @param paragraphIdent*          设置段落缩进量* @param rowIdent*           设置行缩进量* @param spacingBefore*           段前距离,例如:50f* @param spacingAfter*             段后距离* * @return Paragraph*/public Paragraph createParagraph(String context,Font font,int alignment,float paragraphIndent,float rowIndent,float spacingBefore,float spacingAfter){Paragraph pg = new Paragraph(context,font);pg.setAlignment(alignment);pg.setIndentationLeft(paragraphIndent);  pg.setFirstLineIndent(rowIndent); pg.setSpacingBefore(spacingBefore);pg.setSpacingAfter(spacingAfter);  return pg;}/*** 添加指定路径图片* * 如需通过网络,或者其他方式添加,查看Image.getInstance();* @param filePath*          图片路径* @param x*             长度* @param y*           宽度* @param alignment*           对其方式,例如:Image.MIDDLE* @return* @throws IOException * @throws MalformedURLException * @throws BadElementException */public Image addImage(String filePath,float x,float y,int alignment) throws BadElementException, MalformedURLException, IOException{Image image = Image.getInstance(filePath);  image.scaleAbsolute(x, y);  image.setAlignment(alignment); return image;}/*** 创建表格* @param row*            行* @param column*           列* @param alignment*            对齐方式,例如:Table.ALIGN_CENTER* @param widths*            每个列的宽度,列数组长度根据所建列数相同。例如:3行2列,那么数组长度为2;6行8列,那么数组长度为8* @return Table* @throws DocumentException*/public Table createTable(int row,int column,int alignment,int []widths) throws DocumentException{Table table = new Table(column,row);  table.setBorderWidth(1f);  // table.setAbsWidth("120px");  table.setAlignment(alignment);  table.setWidths(widths);  return table;}/*** 创建列* @param e*            Word中的Element元素,例如:图片、Table等* @param headerFlag*          标题头部开关* @param rowSpan*             如开启,则需写跨行,否则可以写0* @param colSpan*             如开启,则需写跨列* @return* @throws BadElementException*/public Cell createCell(Element e,boolean headerFlag,int rowSpan,int colSpan) throws BadElementException{Cell cell = new Cell(e);  cell.setBorder(0);  if(headerFlag){cell.setHeader(true);  cell.setColspan(rowSpan);cell.setRowspan(colSpan);}return cell;}/*** 创建列* @param context*           列中填充的内容* @param headerFlag*             标题头部开关* @param rowSpan*             如开启,则需写跨行,否则可以写0* @param colSpan*             如开启,则需写跨列,否则可以写0* @return* @throws BadElementException*/public Cell createCell(String context,boolean headerFlag,int rowSpan,int colSpan) throws BadElementException{Cell cell = new Cell(context);  cell.setBorder(1);if(headerFlag){cell.setHeader(true);  cell.setColspan(colSpan);cell.setRowspan(rowSpan);}return cell;}public static void main(String[] args) throws DocumentException, IOException, com.lowagie.text.DocumentException{String filePath = "E:\\iText导出Word.doc";exportWord(filePath);}public static void exportWord(String filePath) throws DocumentException, IOException, com.lowagie.text.DocumentException{//创建Document实例Document document = new Document(PageSize.A4);WordUtil ew = new WordUtil();ew.exportWord(document,filePath);//打开文档document.open();document.setHeader(ew.setHeaderFooter("这是页眉", Rectangle.ALIGN_CENTER));document.setFooter(ew.setHeaderFooter("这是页脚",Rectangle.ALIGN_CENTER));//设置基础字体Font baseFont = ew.setFont(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,BaseFont.EMBEDDED), 24, Font.BOLD);//       BaseFont baseFont = BaseFont.createFont();
//      Font contextFont = new Font(baseFont,12,Font.NORMAL);
//      Font titleFont = new Font(baseFont,24,Font.BOLD);
//      Font font = new Font(baseFont,15,Font.BOLD);document.add(ew.createParagraph("Itext", baseFont, Element.ALIGN_CENTER, 0f, 0f, 100f, 100f));document.add(ew.createParagraph("这是新增的Paragraph", baseFont, Element.ALIGN_LEFT, 100f, 100f, 0f, 0f));//正文内容document.newPage();document.add(ew.createParagraph("一、iText简介",  ew.setFontLevel(FontLevel.ONE,"宋体"),Element.ALIGN_CENTER,200f, 200f, 100f, 100f));document.newPage();Paragraph p1 = ew.createParagraph("iText是著名的开放源码的站点sourceforge一个项目,是用于生产PDF文档的一个java类库。通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、HTML文件转化为PDF文件。" , ew.setFont(BaseFont.createFont(BaseFont.HELVETICA, BaseFont.WINANSI,BaseFont.EMBEDDED), 14, Font.NORMAL),Element.ALIGN_CENTER,30f,0f,0f,0f);//p1.setLeading(25f);document.add(p1);Table table = ew.createTable(6, 8, Table.ALIGN_CENTER, new int[]{50,50,50,50,50,50,50,50});table.addCell(ew.createCell("8", true, 0, 8));table.endHeaders();table.addCell(ew.createCell("这是第二个表格", false, 0, 0));table.addCell(ew.createCell("这是第三个表格", false, 0, 0));table.addCell(ew.createCell("这是第四个表格", false, 0, 0));document.add(table);document.close();}
}

转载于:https://my.oschina.net/u/1163293/blog/202627

iText操作Word工具类相关推荐

  1. itext操作word,设置页眉页脚,html转word

    这两天学习了itext操作word生成可用的docx文档,以下是我翻阅网上资料最后的结果及相关记录,如下: 我的maven项目导入必要的itext依赖 <dependency><gr ...

  2. java中文件操作的工具类

    代码: package com.lky.pojo;import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

  3. android文件读取工具类,Android 下读取Assets Properties操作封装工具类

    Android 下读取Assets Properties操作封装工具类 发布时间:2018-06-03作者:laosun阅读(2081) 为了方便使用,首先创建BaseApplication类,如下所 ...

  4. iText操作word

    用Itext操作word需要导入三个jar包:iText-2.1.7.jar.iText-rtf-2.1.7.jar.iTextAsian.jar private void execute(){doc ...

  5. 玩转JDBC打造数据库操作万能工具类JDBCUtil,加入了高效的数据库连接池,利用了参数绑定有效防止SQL注入

    转载请注明出处:http://blog.csdn.net/linglongxin24/article/details/53750584 本文出自[DylanAndroid的博客] 玩转JDBC打造数据 ...

  6. 自己封装的poi操作Excel工具类

    在上一篇文章<使用poi读写Excel>中分享了一下poi操作Excel的简单示例,这次要分享一下我封装的一个Excel操作的工具类. 该工具类主要完成的功能是:读取Excel.汇总Exc ...

  7. java操作svn工具类

    依赖包 <dependency><groupId>org.tmatesoft.svnkit</groupId><artifactId>svnkit< ...

  8. 第十三天 - 封装JDBC操作Hive工具类 - HWI配置与使用 - JavaWeb结合Hive

    第十三天 - 封装JDBC操作Hive工具类 - HWI配置与使用 - JavaWeb结合Hive 第十三天 - 封装JDBC操作Hive工具类 - HWI配置与使用 - JavaWeb结合Hive ...

  9. java 操作IP工具类(子网、地址等)

    分享一个操作IP工具类,包含:格式校验,二进制IP互相转换,网段转换,获取网段内IP,校验IP冲突等等 package com.algoblu.pts.boss.basic.utils.ipOpera ...

最新文章

  1. app把信息添加到mysql_如何将数据库表中的数据添加到ListView C#Xamarin Android App
  2. iOS6新特征:UICollectionView介绍
  3. 三点弯曲弹性模量怎么计算公式_怎么计算弯管的尺寸和弯管的张力
  4. 珠宝条码打印扫描解决方案
  5. Oracle取最大/最小值函数
  6. java8默认垃圾回收器,Java 8的默认垃圾收集器
  7. 服务器性能估算参考(硬件-应用服务器)
  8. mysql 1418 存储过程_MySQL自定义函数 1418报错
  9. DataTable中Compute计算函数
  10. oracle10g配置失败,求解决装oracle10g的时候EM配置失败问题
  11. docker的核心原理-cgroup
  12. 为啥yum源repolist为0?自己经常出错和常用的解决办法
  13. c语言网络字节序整数,c语言中网络字节序和主机字节序的转换
  14. ubuntu16.04 安装Anbox
  15. 复合材料在计算机硬件中的应用,碳纤维复合材料在笔记本电脑后盖中的应用研究...
  16. 怎么修复网站漏洞 骑士cms的漏洞修复方案
  17. android 壁纸再拿,Android获取当前桌面壁纸
  18. ES6 计算属性名快速上手
  19. Jackson配置大全
  20. XSS平台 XSS挑战之旅 解题记录 writeup

热门文章

  1. 7-35 情人节 (15 分)
  2. cocos2dx热更新tmx的一个坑
  3. Django Admin后台管理功能使用
  4. Fast Fourier Transform
  5. [读码][js,css3]能感知鼠标方向的图片遮罩效果
  6. C# 关闭主窗口后让所有线程都停止工作
  7. Android之播放一首简单的音乐
  8. 第一个 Web 程序
  9. 显示器接口VGA、DVI、HDMI、DP
  10. PostgreSQL客户端psql常用命令