itext创建PDF

到官方网站去下载itext:

http://itextpdf.com/

 

简单使用示例如下:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

public class TextToPDF{
  public static void main(String arg[]){
    try{
      InputStreamReader in= new InputStreamReader(new FileInputStream(new File("D:\\test\\File\\开启任务.txt")));
      BufferedReader bin= new BufferedReader(in);
      System.out.println("Enter text:");
      
        Document document = new Document(PageSize.A4, 36, 72, 108, 180);
        
        //指定字符格式
        BaseFont bfChinese = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);   
        Font FontChinese = new Font(bfChinese, 12, Font.NORMAL);
        
		
        PdfWriter.getInstance(document,new FileOutputStream(new File("D:\\test\\File\\开启任务.pdf")));
        document.open();
        String text=null;
        while((text=bin.readLine()) != null){
            document.add(new Paragraph(text,FontChinese));
        }
        System.out.println("Text is inserted into pdf file");
        document.close();
    }catch(Exception e){}
  }
}

 

 

它对中文好像支持不够,会将识别不了的中文直接过滤掉,对于编码方式可以有如下选择(转自itext输出中文的三种选择

 

 

http://blog.youkuaiyun.com/hangke/archive/2007/10/23/1839687.aspx):

1、使用iTextAsian.jar中的字体
    BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
2、使用Windows系统字体(TrueType)
        BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);    
3、使用资源字体(ClassPath)
    BaseFont.createFont("/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);   

第一种方式

ByteArrayOutputStream baos = new ByteArrayOutputStream(OUTPUT_BYTE_ARRAY_INITIAL_SIZE);
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, baos);
    writer.setViewerPreferences(PdfWriter.AllowPrinting  | PdfWriter.PageLayoutSinglePage);
    BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
    Font font = new Font(bf, 12, Font.NORMAL);
    document.open();
    Paragraph p = new Paragraph("你好", font); 
    document.add(p);
    document.add(new Paragraph("Test2"));
    Table table = new Table(2, 3);
    table.addCell(new Phrase("我好", font));
    table.addCell("C2R1");
    table.addCell("C1R2");
    table.addCell("C2R2");
    Cell c = (Cell) table.getElement(0, 0);
    c.setVerticalAlignment("Middle");
    c.setBackgroundColor(new Color(255, 0, 0));
    c.setHorizontalAlignment("Center");
    document.add(table);
    document.close();
    baos.writeTo(new FileOutputStream("F:\\test.pdf"));     

 

这种方式可能遇到的问题是adober的版本不同造成中文不能显示,可以用超星等其他浏览器查看效果

第二种方式 

 

public static void main(String[] args) {
  // TODO Auto-generated method stub
        // step 1: creation of a document-object
        Document document = new Document();        
        try {
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter.getInstance(document, new FileOutputStream("D:\\ChinesePDF005_"+new java.util.Date().getTime()+".pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add content to the document
            //楷体字
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\simkai.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //方正舒体
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\FZSTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //方正姚体
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\FZYTK.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //仿宋体
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SIMFANG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //黑体
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SIMHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文彩云
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STCAIYUN.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文仿宋
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STFANGSO.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文细黑
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STXIHEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文新魏
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STXINWEI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文行楷
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STXINGKA.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //华文中宋
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\STZHONGS.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //隶书
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SIMLI.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //宋体&新宋体    (这种字体的输出不了.有问题)
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SIMSUN.TTC", BaseFont.NOT_EMBEDDED, BaseFont.NOT_EMBEDDED);
            //宋体-方正超大字符集
            //BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SURSONG.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
            //幼圆
            BaseFont bfComic = BaseFont.createFont("c:\\windows\\fonts\\SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            Font font = new Font(bfComic, 14);
            String text1 = " 幼圆幼圆幼圆  This is the quite popular True Type font (繁體字測試VS简体字测试) ==>"+new java.util.Date();
            document.add(new Paragraph(text1, font));
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }        
        // step 5: we close the document
        document.close();
        System.out.println(">>> Export : "+"D:\\ChinesePDF005__.pdf");
 }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值