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