java 到出word简历,表格中插入照片-------非常实用,copy下去就可以使用

import java.awt.Color;
import java.io.File;
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.FontFactory;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
public class TestDoc {
 public void createDocContext(String file) throws DocumentException, 
    IOException { 
    // 设置纸张大小  
    Document document = new Document(PageSize.A4); 
    // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中  
    RtfWriter2.getInstance(document, new FileOutputStream(file)); 
    document.open(); 
    // 设置中文字体  
    BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); 
    // 标题字体风格  
    Font titleFont = new Font(bfChinese, 12, Font.BOLD); 
    // 正文字体风格  
    Font contextFont = new Font(bfChinese, 10, Font.BOLD); 
     
    Paragraph title = new Paragraph("个人简历"); 
    // 设置标题格式对齐方式  
    title.setAlignment(Element.ALIGN_CENTER); 
    title.setFont(titleFont);    
    document.add(title); 
     
    // 设置 Table 表格  
    Table aTable = new Table(7); 
    int width[] = { 10,15,15,15,15,15,15 }; 
    aTable.setWidths(width);// 设置每列所占比例  
    aTable.setWidth(90); // 占页面宽度 90%  
    aTable.setAlignment(Element.ALIGN_CENTER);// 居中显示  
    aTable.setAutoFillEmptyCells(true); // 自动填满  
    aTable.setBorderWidth(1); // 边框宽度  
    aTable.setBorderColor(new Color(0, 125, 255)); // 边框颜色  
    aTable.setPadding(0);// 衬距  
    aTable.setSpacing(0);// 即单元格之间的间距  
    aTable.setBorder(2);// 边框  
     
    //  设置表头  
    Cell haderCell = new Cell(new Phrase("基本信息",contextFont)); 
    haderCell.setHeader(true); 
    haderCell.setColspan(7); 
    aTable.addCell(haderCell); 
    aTable.endHeaders(); 
     
    Font fontChinese = new Font(bfChinese, 10, Font.NORMAL); 
     
    aTable.addCell(new Phrase("姓名:", fontChinese)); 
    aTable.addCell(new Phrase("文志晖", fontChinese)); 
    aTable.addCell(new Phrase("性别:", fontChinese)); 
    aTable.addCell(new Phrase("本科", fontChinese)); 
    aTable.addCell(new Phrase("出生日期:", fontChinese)); 
    aTable.addCell(new Phrase("1987-3-4", fontChinese));               
        //添加图片--方式 1 
//    String path = this.getClass().getResource("/").getPath().replace("%20", " "); 
//    String path1=path.substring(1, path.lastIndexOf("/"));; 
//    String path2=path1.substring(0,path1.lastIndexOf("/")); 
//    String path3=path2.substring(0,path2.lastIndexOf("/")); 
//    String url=path3+"/images/432501198703047023.jpg";
//    System.out.println("path1:"+path1);
//    System.out.println("path2:"+path2);
//    System.out.println("path3:"+path3);
//    System.out.println("url:"+url);
//    Cell cell1_7  = null; 
//    if(new File(url).exists()){      
//        Image img = Image.getInstance(url); 
//        img.setAbsolutePosition(0, 0); 
//        img.setAlignment(Image.MIDDLE);//设置图片显示位置  
//        img.scaleAbsolute(70,90);//直接设定显示尺寸  
//        cell1_7 = new Cell(img); 
//    }else{ 
//        cell1_7 = new Cell(new Phrase("", fontChinese)); 
//    } 
//    cell1_7.setRowspan(5); 
//    aTable.addCell(cell1_7); 
   

 //添加图片--方式2     

 String url="http://127.0.0.1:8080/testDoc/images/432501198703047023.jpg";
    Cell cell1_7  = null; 
        Image img = Image.getInstance(url); 
        img.setAbsolutePosition(0, 0); 
        img.setAlignment(Image.MIDDLE);//设置图片显示位置  
        img.scaleAbsolute(70,90);//直接设定显示尺寸  
        cell1_7 = new Cell(img); 
   
    cell1_7.setRowspan(5); 
    aTable.addCell(cell1_7); 
    aTable.addCell(new Phrase("民族:", fontChinese)); 
    aTable.addCell(new Phrase("汉", fontChinese)); 
   
    aTable.addCell(new Phrase("户籍地:", fontChinese)); 
    aTable.addCell(new Phrase("娄底", fontChinese)); 
   
    aTable.addCell(new Phrase("籍贯:", fontChinese)); 
    aTable.addCell(new Phrase("湘", fontChinese));     
     
    aTable.addCell(new Phrase("身高:", fontChinese)); 
    aTable.addCell(new Phrase(" ", fontChinese));
   
    aTable.addCell(new Phrase("婚姻状况:", fontChinese)); 
    aTable.addCell(new Phrase("未婚", fontChinese));
    aTable.addCell(new Phrase("政治面貌:", fontChinese));
    aTable.addCell(new Phrase("团员", fontChinese));  
    aTable.addCell(new Phrase("最高学历:", fontChinese)); 
    aTable.addCell(new Phrase("本科", fontChinese));  
 
    aTable.addCell(new Phrase("外语水平:", fontChinese)); 
    aTable.addCell(new Phrase("四级", fontChinese));
   
    aTable.addCell(new Phrase("计算机水平:", fontChinese)); 
    aTable.addCell(new Phrase("软件工程师", fontChinese));   
     
    aTable.addCell(new Phrase("本人身份:", fontChinese)); 
    aTable.addCell(new Phrase("", fontChinese)); 
   
    aTable.addCell(new Phrase("是否服从调剂:", fontChinese)); 
    aTable.addCell(new Phrase("否", fontChinese));  


    aTable.addCell(new Phrase("参加工作时间:", fontChinese));  
      aTable.addCell(new Phrase("2010-6-26", fontChinese));
    aTable.addCell(new Phrase("手机号:", fontChinese)); 
    aTable.addCell(new Phrase("", fontChinese)); 
    aTable.addCell(new Phrase("身份证号:", fontChinese));    
    Cell cell6_4 = new Cell(new Phrase("", fontChinese)); 
    cell6_4.setColspan(2); 
    aTable.addCell(cell6_4);         
    aTable.addCell(new Phrase("电子邮箱:", fontChinese)); 
    aTable.addCell(new Phrase("wenzhihui32@126.com", fontChinese));  
     
    aTable.addCell(new Phrase("通讯地址:", fontChinese));        
    Cell cell7_2 = new Cell(new Phrase("虹口区", fontChinese)); 
    cell7_2.setColspan(3); 
    aTable.addCell(cell7_2);     
    aTable.addCell(new Phrase("邮编:", fontChinese)); 
    Cell cell7_6 = new Cell(new Phrase("211100", fontChinese)); 
    cell7_6.setColspan(2); 
    aTable.addCell(cell7_6);     
     
    Cell cell8_1 = new Cell(new Phrase("本科及以上学习经历(最高学历倒序填写)", fontChinese));  
 
    aTable.addCell(cell8_1); 
    aTable.addCell(new Phrase("起止时间", fontChinese)); 
    Cell cell8_3 = new Cell(new Phrase("学校", fontChinese)); 
    cell8_3.setColspan(2); 
    aTable.addCell(cell8_3);         
    aTable.addCell(new Phrase("专业", fontChinese)); 
    aTable.addCell(new Phrase("所获学位", fontChinese));     
    aTable.addCell(new Phrase("是否全日制", fontChinese));  
      
    Cell cell12_1 = new Cell(new Phrase("工作经验(不含实习)(近期经验倒序填写))", fontChinese));  
    aTable.addCell(cell12_1); 
    aTable.addCell(new Phrase("起止时间", fontChinese)); 
    Cell cell12_3 = new Cell(new Phrase("工作单位及内容", fontChinese)); 
    cell12_3.setColspan(2); 
    aTable.addCell(cell12_3);        
    Cell cell12_5 = new Cell(new Phrase("部门", fontChinese)); 
    cell12_5.setColspan(2); 
    aTable.addCell(cell12_5);    
    aTable.addCell(new Phrase("职务", fontChinese));    
    aTable.addCell(new Phrase("证书及职称", fontChinese));    
    Cell cell18_2 = new Cell(new Phrase("", fontChinese)); 
    cell18_2.setColspan(6); 
    aTable.addCell(cell18_2);    
     
    aTable.addCell(new Phrase("专业经验及特长(限两百字)", fontChinese));    
    Cell cell19_2 = new Cell(new Phrase("", fontChinese)); 
    cell19_2.setColspan(6); 
    aTable.addCell(cell19_2);    
     
    aTable.addCell(new Phrase("主要学术研究成果", fontChinese));     
    Cell cell20_2 = new Cell(new Phrase("", fontChinese)); 
    cell20_2.setColspan(6); 
    aTable.addCell(cell20_2);    
     
    aTable.addCell(new Phrase("曾获奖惩情况", fontChinese));   
    Cell cell21_2 = new Cell(new Phrase("", fontChinese)); 
    cell21_2.setColspan(6); 
    aTable.addCell(cell21_2); 
     
    Cell cell22_1 = new Cell(new Phrase("家庭主要成员", fontChinese));  
    aTable.addCell(cell22_1); 
    aTable.addCell(new Phrase("姓名", fontChinese)); 
    aTable.addCell(new Phrase("关系", fontChinese)); 
    Cell cell22_4 = new Cell(new Phrase("单位", fontChinese)); 
    cell22_4.setColspan(2); 
    aTable.addCell(cell22_4); 
    aTable.addCell(new Phrase("职务", fontChinese));   
    aTable.addCell(new Phrase("联系方式", fontChinese));      
    
    aTable.addCell(new Phrase("其他事项说明", fontChinese));   
    Cell cell27_2 = new Cell(new Phrase("", fontChinese)); 
    cell27_2.setColspan(6); 
    aTable.addCell(cell27_2); 
      
    document.add(aTable); 
    document.add(new Paragraph("\n")); 
    document.close(); 
  } 
 public static void main(String[] args){
  TestDoc td=new TestDoc();
     try {
      td.createDocContext("c:/demoJL.doc");
     } catch (DocumentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
  }

}

点击下面链接下载jar包:

需要jar包:iText-2.1.7.jar,iTextAsian.jar,itext-rtf-2.1.7.jar,jxl.jar,slf4j-api-1.6.4.jar五个jar包
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值