阅读450 返回首页    go 阿里云 go 技术社区[云栖]


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

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


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




下载地址是:

https://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();




最后更新:2017-04-02 22:16:24

  上一篇:go java socket 客户端和服务器端互相通信(聊天)
  下一篇:go JXCELL实例学习与研究(四) 之 录入数据、绘制表格、修整线性表的颜色 密码的设置与破译