ITEXT实例学习与研究(一) 之 HELLOWORLD的实现,解决中文问题,ITEXT框架

以下是我总结的所有ITEXT核心的资料,内容有:
ITEX全部核心资料,JAR包,中文文档,API,百个实例!


我找了很久,这些文件都是我自己整理出来的。尤其是API和中文教程,以及中文教程中的百个实例,都是特别有价值的。这些实例,可以迅速让你在两三天内掌握ITEXT的核心技术。




下载地址是:

http://download.csdn.net/detail/opzoonzhuzhengke/4069316



首先说HELLOWORLD:

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Chap0101 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 1: Hello World");
        
        // 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("Chap0101.pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add a paragraph to the document
            document.add(new Paragraph("Hello World"));
            
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        // step 5: we close the document
        document.close();
    }
}

修改一下代码,可以处理中英文问题:

import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.PdfWriter;

public class Chap0101 {
    
    public static void main(String[] args) {
        
        System.out.println("Chapter 1 example 1: Hello World");
        
        // 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("Chap0101中文版.pdf"));
            
            // step 3: we open the document
            document.open();
            
            // step 4: we add a paragraph to the document
            document.add(new Paragraph("你好中文版",ChineseFont()));
            
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        // step 5: we close the document
        document.close();
    }
//pdf文档中文字符处理  
     public static Font ChineseFont() 
     {  
         BaseFont baseFont=null; 
         try { 
            baseFont=BaseFont.createFont("STSong-Light","UniGB-UCS2-H", true); 
         } catch (DocumentException e) { 
             e.printStackTrace(); 
          } catch (IOException e) { 
           e.printStackTrace(); 
        } 
        Font chineseFont=new Font(baseFont,8,Font.NORMAL,Color.BLUE); 
        return chineseFont; 
     }  

}



处理中文问题的部分就哦了,多么简单!


补充:

创建一个Document

利用iText五步创建一个PDF文件:helloword。

第一步,创建一个iTextSharp.text.Document对象的实例:

Document document = new Document();

第二步,为该Document创建一个Writer实例:

PdfWriter.getInstance(document, newFileStream("Chap0101.pdf", FileMode.Create));

第三步,打开当前Document

document.Open();

第四步,为当前Document添加内容:

document.Add(new Paragraph("HelloWorld"));

第五步,关闭Document

document.Close();




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值