iText操作word
用Itext操作word需要導(dǎo)入三個(gè)jar包:iText-2.1.7.jar、iText-rtf-2.1.7.jar、iTextAsian.jar
private void execute(){document = new Document(PageSize.A4);try {RtfWriter2.getInstance(document, new FileOutputStream(file));document.open();//設(shè)置中文字體BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//標(biāo)題字體風(fēng)格Font titleFont = new Font(bfChinese,16, Font.BOLD);//正文字體風(fēng)格 Font contextFont = new Font(bfChinese,10,Font.NORMAL);//標(biāo)題Paragraph title = new Paragraph("文章標(biāo)題",titleFont); title.setAlignment(Element.ALIGN_CENTER);//設(shè)置標(biāo)題格式對(duì)齊方式 // title.setFont(titleFont);//設(shè)置字體 document.add(title);Paragraph p = new Paragraph("文書制作"); //設(shè)置段落居中,其中1為居中對(duì)齊,2為右對(duì)齊,3為左對(duì)齊 p.setAlignment(1); // 設(shè)置字體,字號(hào),加粗,顏色 // p.setFont(new Font(bfChinese, 20, Font.BOLD, new Color(255, 0, 0)));document.add(p); //調(diào)用系統(tǒng)的“楷體”字體,設(shè)置該段落時(shí)楷體 BaseFont bf = BaseFont.createFont("C:\\Windows\\Fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);p = new Paragraph( " itext可以實(shí)現(xiàn)插入段落,可以設(shè)置段落的縮進(jìn),居中,首行縮進(jìn),段前距,段后距。可以設(shè)置字體,字號(hào),格式。功能比較齊全。", new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0))); // 設(shè)置段落縮進(jìn) p.setIndentationLeft(20); // 設(shè)置首行縮進(jìn) p.setFirstLineIndent(30f); // 設(shè)置段后距和段前距 p.setSpacingAfter(10f); p.setSpacingBefore(100f); // p.setFont(new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));document.add(p); p = new Paragraph("itext可以插入表格,設(shè)置表格的行列數(shù),可以設(shè)置表格邊框,可以設(shè)置表格位置,可以設(shè)置表格總寬度和每一列的寬度。單元格的插入和內(nèi)容可控", new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0))); // p.setFont(new Font(bf, 16, Font.NORMAL, new Color(0, 0, 0)));document.add(p); // 創(chuàng)建有三列的表格 Table table = new Table(3,3);// 設(shè)置table的邊框?qū)挾葹? table.setBorderWidth(1f); // 設(shè)置表格右對(duì)齊,其中1為居中對(duì)齊,2為右對(duì)齊,3為左對(duì)齊 table.setAlignment(2); // 設(shè)置各列的寬度 int[] widths = { 25, 25, 50 }; table.setWidths(widths); table.setPadding(0); table.setSpacing(0); // 讀取圖片(參數(shù)為gif、jpg、png格式的圖片都可以),設(shè)置圖片大小 Image image = Image.getInstance("E:/timg.jpg"); // 設(shè)置圖片的絕對(duì)大小,寬和高 image.scaleAbsolute(50f, 50f); // 設(shè)置圖片居中顯示 image.setAlignment(Image.MIDDLE); // 創(chuàng)建單元格,并且將單元格內(nèi)容設(shè)置為圖片 Cell cell = new Cell(image); // 設(shè)置單元格邊框?yàn)? cell.setBorder(0); // cell.setHeader(true); // cell.setColspan(3);// 設(shè)置表格為三列 // cell.setRowspan(3);// 設(shè)置表格為三行 table.addCell(cell); cell = new Cell("該單元格的長度是200"); cell.setBorder(0); table.addCell(cell); cell = new Cell("該單元格的長度是100"); // cell.setWidth("10px"); table.addCell(cell); // cell.setBorder(1); // 設(shè)置垂直居中 cell.setVerticalAlignment(1); // 設(shè)置水平居中 cell.setHorizontalAlignment(1); // document.add(new Paragraph("用java生成word文件")); document.add(table); Table table2 = new Table(3); int width[] = {25,25,50};//設(shè)置每列寬度比例 table2.setWidths(width); table2.setWidth(90);//占頁面寬度比例 table2.setAlignment(Element.ALIGN_CENTER);//居中 table2.setAlignment(Element.ALIGN_MIDDLE);//垂直居中 table2.setAutoFillEmptyCells(true);//自動(dòng)填滿 table2.setBorderWidth(1);//邊框?qū)挾? //設(shè)置表頭 Cell haderCell = new Cell("表格表頭"); haderCell.setHeader(true); haderCell.setColspan(3); //合并3個(gè)單元格 table2.addCell(haderCell); table2.endHeaders(); Font fontChinese = new Font(bfChinese,12,Font.NORMAL,Color.GREEN); Cell cell2 = new Cell(new Paragraph("這是一個(gè)3*3測(cè)試表格數(shù)據(jù)",fontChinese)); cell.setVerticalAlignment(Element.ALIGN_MIDDLE); table2.addCell(cell2); table2.addCell(new Cell("#1")); // table2.addCell(new Cell("#2")); // table2.addCell(new Cell("#3")); document.add(table2); // 關(guān)閉document document.close(); } catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (DocumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}?需要導(dǎo)入的引用:
import java.awt.Color;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
文檔截圖:
?
參考:https://www.cnblogs.com/xiaoSY-learning/p/5805577.html
總結(jié)
以上是生活随笔為你收集整理的iText操作word的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Win10安装WSL-Ubuntu18.
- 下一篇: 2019小程序发展趋势