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();




目录
相关文章
|
6月前
|
XML 测试技术 数据格式
如何使用 ABAP 代码解析 XML 文件试读版
如何使用 ABAP 代码解析 XML 文件试读版
56 0
如何使用 ABAP 代码解析 XML 文件试读版
Java使用itext运用非模板方式生成PDF文件1
Java使用itext运用非模板方式生成PDF文件1
Java使用itext非模板方式生成PDF表格文件2
Java使用itext非模板方式生成PDF表格文件2
Java使用itext按模板生成PDF文件
Java使用itext按模板生成PDF文件
分享一个超牛逼的 Java 文件在线预览项目(附源码)
分享一个超牛逼的 Java 文件在线预览项目(附源码)
388 0
分享一个超牛逼的 Java 文件在线预览项目(附源码)
|
Java Maven Android开发
关于如何通过Maven仓库安装Spire.PDF for Java的示范教程
关于如何通过Maven仓库安装Spire.PDF for Java的示范教程 Spire系列库中已发布的Java产品目前有三个,即Spire.PDF for Java、Spire.Presentation for Java、Spire.Barcode for Java。
6654 0
|
Android开发
Eclipse插件开发_学习_02_GEF入门实例
一、前言 这一节,我们将会创建一个GEF入门实例     二、新建RCP项目 1. New 一个 Plug-in Project     2.输入项目名 项目名:com.ray.gef.
1744 0
|
Java 机器人 测试技术
Robot Framework 中文用户指南 | 第一章第三节
1.3 安装说明 本章的说明涵盖了所有Robot Framework安装和卸载的方法,以及在不同操作系统上安装的前提条件。如果你本地安装有pip,那么就很容易安装Robot Framework。
1470 0