java匯出word(帶圖片),java匯出word圖片

來源:互聯網
上載者:User

java匯出word(帶圖片),java匯出word圖片

 1 public class CreateWordDemo {    2  public void createDocContext(String file) throws DocumentException,IOException {  3        // 設定紙張大小       4   Document document = new Document(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 = new Paragraph(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 void main(String[] args) {       82       CreateWordDemo word = new CreateWordDemo();       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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.