Java操作Word及PDF文档

以下操作WORD文档的代码测试通过。但操作PDF文档的代码,无法显示中文,有待修改。

本文介绍的是itext生成rtf文件并保存格式为word 此方案本人已实践过 并已在项目中使用

 
 
  1. package com.rye.test;  
  2. import java.awt.Color;  
  3. import java.io.FileNotFoundException;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.  
  7. import com.lowagie.text.Cell;  
  8. import com.lowagie.text.Document;  
  9. import com.lowagie.text.DocumentException;  
  10. import com.lowagie.text.Font;  
  11. import com.lowagie.text.PageSize;  
  12. import com.lowagie.text.Paragraph;  
  13. import com.lowagie.text.Table;  
  14. import com.lowagie.text.rtf.RtfWriter2;  
  15. /**  
  16.  * 创建word文档 步骤:   
  17.  * 1,建立文档   
  18.  * 2,创建一个书写器   
  19.  * 3,打开文档   
  20.  * 4,向文档中写入数据   
  21.  * 5,关闭文档  
  22.  */ 
  23. public class WordDemo {  
  24.  
  25.  public WordDemo() {  
  26.  }  
  27.  
  28.  /**  
  29.   * @param args  
  30.   */ 
  31.  public static void main(String[] args) {  
  32. // 创建word文档,并设置纸张的大小
  33.   Document document = new Document(PageSize.A4); 
  34.   try {  
  35.    RtfWriter2.getInstance(document,
  36. new FileOutputStream("E:/word.doc"));  
  37.  
  38.    document.open();  
  39.      
  40.    //设置合同头  
  41.      
  42.    Paragraph ph = new Paragraph();  
  43.    Font f  = new Font();  
  44.      
  45.    Paragraph p = new Paragraph("出口合同"
  46. new Font(Font.NORMAL, 18, Font.BOLDITALIC, new Color(000)) );  
  47.    p.setAlignment(1);  
  48.    document.add(p);  
  49.    ph.setFont(f);  
  50.  
  51.    // 设置中文字体  
  52.    // BaseFont bfFont =  
  53.    // BaseFont.createFont("STSongStd-Light",
  54. "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);  
  55.    // Font chinaFont = new Font();  
  56.    /*  
  57.     * 创建有三列的表格  
  58.     */ 
  59.    Table table = new Table(4);  
  60.    document.add(new Paragraph("生成表格"));  
  61.    table.setBorderWidth(1);  
  62.    table.setBorderColor(Color.BLACK);  
  63.    table.setPadding(0);  
  64.    table.setSpacing(0);  
  65.      
  66.    /*  
  67.     * 添加表头的元素  
  68.     */ 
  69.    Cell cell = new Cell("表头");//单元格  
  70.    cell.setHeader(true);  
  71.    cell.setColspan(3);//设置表格为三列  
  72.    cell.setRowspan(3);//设置表格为三行  
  73.    table.addCell(cell);  
  74.    table.endHeaders();// 表头结束  
  75.  
  76.    // 表格的主体  
  77.    cell = new Cell("Example cell 2");  
  78.    cell.setRowspan(2);//当前单元格占两行,纵向跨度  
  79.    table.addCell(cell);  
  80.    table.addCell("1,1");  
  81.    table.addCell("1,2");  
  82.    table.addCell("1,3");  
  83.    table.addCell("1,4");  
  84.    table.addCell("1,5");  
  85.    table.addCell(new Paragraph("用java生成的表格1"));  
  86.    table.addCell(new Paragraph("用java生成的表格2"));  
  87.    table.addCell(new Paragraph("用java生成的表格3"));  
  88.    table.addCell(new Paragraph("用java生成的表格4"));  
  89.    document.add(new Paragraph("用java生成word文件"));  
  90.    document.add(table);  
  91.    document.close();  
  92.   } catch (FileNotFoundException e) {  
  93.    e.printStackTrace();  
  94.   } catch (DocumentException e) {  
  95.    e.printStackTrace();  
  96.   } catch (IOException e) {  
  97.    e.printStackTrace();  
  98.   }  
  99.  }  
  100.  

 

相关jar包:

iText_API帮助手册.rar

iText-2.1.5.jar

iText-rtf-2.1.4.jar

iTextAsian.jar

 

以下是操作PDF文档的代码。

package src;

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.Font;
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.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfWriter;

 

public class MainClass {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  method1();
 }
 
 public static void method1(){
  /** 
   * 创建pdf文档 步骤:  
   * 1,建立文档  
   * 2,创建一个书写器  
   * 3,打开文档  
   * 4,向文档中写入数据  
   * 5,关闭文档 
   */
  // 创建word文档,并设置纸张的大小  标准A4纸张
    Document document = new Document(PageSize.A4);
  
  //以下直接指定纸张大小.每一点约为 0.35mm
  //Rectangle rec= new Rectangle(0,0,371,200);  
    //Document document = new Document(rec);
   
    try { 
     PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("D:/word.pdf")); 
     //PdfContentByte cb = writer.DirectContent;
     PdfContentByte cb = new PdfContentByte(writer);
     document.open(); 
      
     //设置合同头 
      
     Paragraph ph = new Paragraph(); 
     BaseFont bf = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
     Font FontChinese = new Font(bf, 12, Font.NORMAL,new Color(0,0,0));
     Font f  = new Font(); 
      
     Paragraph p = new Paragraph("出口合同", FontChinese ); 
     p.setAlignment(1); 
     document.add(p); 
     ph.setFont(f); 
  
     // 设置中文字体 
     // BaseFont bfFont = 
     // BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED); 
     // Font chinaFont = new Font(); 
     /* 
      * 创建有三列的表格 
      */
     Table table = new Table(4); 
     document.add(new Paragraph("生成表格")); 
     table.setBorderWidth(1); 
     table.setBorderColor(Color.BLACK); 
     table.setPadding(0); 
     table.setSpacing(0); 
      
     /* 
      * 添加表头的元素 
      */
     Cell cell = new Cell("表头");//单元格 
     cell.setHeader(true); 
     cell.setColspan(3);//设置表格为三列 
     cell.setRowspan(3);//设置表格为三行 
     table.addCell(cell); 
     table.endHeaders();// 表头结束 
  
     // 表格的主体 
     cell = new Cell("Example cell 2"); 
     cell.setRowspan(2);//当前单元格占两行,纵向跨度 
     table.addCell(cell); 
     table.addCell("1,1"); 
     table.addCell("1,2"); 
     table.addCell("1,3"); 
     table.addCell("1,4"); 
     table.addCell("1,5"); 
     table.addCell(new Paragraph("用java生成的表格1")); 
     table.addCell(new Paragraph("用java生成的表格2")); 
     table.addCell(new Paragraph("用java生成的表格3")); 
     table.addCell(new Paragraph("用java生成的表格4")); 
     document.add(new Paragraph("用java生成word文件")); 
     document.add(table); 
     document.newPage();
     document.add(new Paragraph("换一页"));
    
     //在指定位置插入文本
    
     cb.beginText();
     cb.setFontAndSize(bf, 12);
     cb.showTextAligned(PdfContentByte.ALIGN_CENTER, "This text is centered", 250, 700, 0);
     cb.endText();
    
     document.close(); 
     System.out.println("done");
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (DocumentException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
 }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值