Java生成pdf文件,解決中文亂碼問題

pkiek23 9年前發布 | 27K 次閱讀 Java

如下代碼使用itext生成pdf文件,通過設置中文字體避免亂碼。

 

/**

  • AsianTest.java */

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

import com.lowagie.text.*; import com.lowagie.text.pdf.PdfWriter; import com.lowagie.text.pdf.BaseFont; import com.lowagie.text.Font; import java.awt.Color;

public class AsianTest {

public static void main(String[] args) {

    // 創建一個Document對象
    Document document = new Document();

    try {

        // 生成名為 AsianTest.pdf 的文檔
        PdfWriter.getInstance(document, new FileOutputStream(
                "c://AsianTest.pdf"));

        /**
         * 新建一個字體,iText的方法 STSongStd-Light 是字體,在iTextAsian.jar 中以property為后綴
         * UniGB-UCS2-H 是編碼,在iTextAsian.jar 中以cmap為后綴 H 代表文字版式是 橫版, 相應的 V
         * 代表豎版
         */
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light",
                "UniGB-UCS2-H", false);

        Font bold_fontChinese = new Font(bfChinese, 12, Font.BOLD,
                Color.BLACK);
        Font italic_fontChinese = new Font(bfChinese, 12, Font.ITALIC,
                Color.BLACK);
        Font impressFont = new Font(bfChinese, 16, Font.BOLDITALIC,
                Color.BLACK);
        // 打開文檔,將要寫入內容
        document.open();

        // 插入一個段落
        // Paragraph par = new Paragraph("我們", fontChinese);

        // document.add(par);
        //
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        String[] Trainspotting1 = { "選擇生命,選擇工作,選擇職業,選擇家庭,",
                "選擇可惡的大彩電,選擇洗衣機、汽車、雷射碟機,", "選擇健康、低膽固醇和牙醫保險,選擇樓宇按揭,",
                "選擇你的朋友,選擇套裝、便服和行李,選擇分期付款和三件套西裝,",
                "選擇收看無聊的游戲節目,邊看邊吃零食……選擇你的未來,選擇生命……", "太多選擇,你選擇什么,我選擇不選擇。" };
        String[] Trainspotting2 = { "這是電影《猜火車》開頭的旁白。", "這是一個關于“選擇”的故事。" };
        String[] Benjamin1 = { "有些人就在河邊出生長大,", "有些人被閃電擊中,",
                "有些人對音樂有著非凡的天賦,", "有些人是藝術家,", "有人會游泳,", "有人懂得做紐扣,",
                "有人會背莎士比亞,", "而有些人。。。是母親,", "也有些人,可以翩翩起舞。",
                "Goodnight  Daisy", "Goodnight  Benjamin" };
        String[] Benjamin2 = { "這是電影《本杰明傳奇》結尾的旁白。", "這是一個關于“錯過”的故事。" };
        String[] text1 = { "我想說的是,", "我們選擇,同時,我們錯過。" };
        String[] text2 = { "拋去無可選擇的選擇,抑或不選擇的選擇,",
                "很有趣的一件事:當面臨(太多的)選擇,人們會如何選擇;", "同時,人們又會如何看待錯過。" };
        String[] text3 = { "在開始和結束之間,選擇了什么,又會錯過什么,我還不知道。" };
        String[] text4 = { "你會知道么?" };
        //
        for (String s : Trainspotting1) {
            document.add(new Paragraph(s, italic_fontChinese));
            document.add(new Paragraph(" ", italic_fontChinese));
        }
        for (String s : Trainspotting2) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        for (String s : Benjamin1) {
            document.add(new Paragraph(s, italic_fontChinese));
            document.add(new Paragraph(" ", italic_fontChinese));
        }
        for (String s : Benjamin2) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        document.add(new Paragraph(" ", bold_fontChinese));
        for (String s : text1) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        for (String s : text2) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        for (String s : text3) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        for (String s : text4) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(" ", bold_fontChinese));
        //
        String[] end = { "Some people were born to sit by a river...",
                "Some get struck by light...",
                "Some have an ear for music...", "Some are artists...",
                "Some swim...", "Some know buttons...",
                "Some know Shakespeare...", "Some are mothers...",
                "And some people can dance..." };
        for (String s : end) {
            document.add(new Paragraph(s, bold_fontChinese));
        }
        document.add(new Paragraph(
                "by the way, some people can write code.你", impressFont));

        // Chapter
        Paragraph title1 = new Paragraph("Chapter 1", italic_fontChinese);
        Chapter chapter1 = new Chapter(title1, 1);
        chapter1.setNumberDepth(0);
        Paragraph title11 = new Paragraph(
                "This is Section 1 in Chapter 1中文", italic_fontChinese);
        Section section1 = chapter1.addSection(title11);
        Paragraph someSectionText = new Paragraph(
                "This text comes as part of section 1 of chapter 1.");
        section1.add(someSectionText);
        someSectionText = new Paragraph("Following is a 3 X 2 table.");
        section1.add(someSectionText);
        //
        document.add(chapter1);
        //
        // 定義一個圖片

        Image jpeg = Image.getInstance("E:/01.jpg");

        // 圖片居中
        jpeg.setAlignment(Image.ALIGN_CENTER);
        document.add(jpeg);
    } catch (DocumentException de) {
        System.err.println(de.getMessage());
    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    }

    // 關閉打開的文檔
    document.close();
}

}</pre>

 本文由用戶 pkiek23 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!