中文字幕在线观看,亚洲а∨天堂久久精品9966,亚洲成a人片在线观看你懂的,亚洲av成人片无码网站,亚洲国产精品无码久久久五月天

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

2018-07-20    來源:open-open

容器云強(qiáng)勢(shì)上線!快速搭建集群,上萬Linux鏡像隨意使用

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

 

/**
 * 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) {

        // 創(chuàng)建一個(gè)Document對(duì)象
        Document document = new Document();

        try {

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

            /**
             * 新建一個(gè)字體,iText的方法 STSongStd-Light 是字體,在iTextAsian.jar 中以property為后綴
             * UniGB-UCS2-H 是編碼,在iTextAsian.jar 中以cmap為后綴 H 代表文字版式是 橫版, 相應(yīng)的 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);
            // 打開文檔,將要寫入內(nèi)容
            document.open();

            // 插入一個(gè)段落
            // 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 = { "選擇生命,選擇工作,選擇職業(yè),選擇家庭,",
                    "選擇可惡的大彩電,選擇洗衣機(jī)、汽車、雷射碟機(jī),", "選擇健康、低膽固醇和牙醫(yī)保險(xiǎn),選擇樓宇按揭,",
                    "選擇你的朋友,選擇套裝、便服和行李,選擇分期付款和三件套西裝,",
                    "選擇收看無聊的游戲節(jié)目,邊看邊吃零食……選擇你的未來,選擇生命……", "太多選擇,你選擇什么,我選擇不選擇。" };
            String[] Trainspotting2 = { "這是電影《猜火車》開頭的旁白。", "這是一個(gè)關(guān)于“選擇”的故事。" };
            String[] Benjamin1 = { "有些人就在河邊出生長大,", "有些人被閃電擊中,",
                    "有些人對(duì)音樂有著非凡的天賦,", "有些人是藝術(shù)家,", "有人會(huì)游泳,", "有人懂得做紐扣,",
                    "有人會(huì)背莎士比亞,", "而有些人。。。是母親,", "也有些人,可以翩翩起舞。",
                    "Goodnight  Daisy", "Goodnight  Benjamin" };
            String[] Benjamin2 = { "這是電影《本杰明傳奇》結(jié)尾的旁白。", "這是一個(gè)關(guān)于“錯(cuò)過”的故事。" };
            String[] text1 = { "我想說的是,", "我們選擇,同時(shí),我們錯(cuò)過。" };
            String[] text2 = { "拋去無可選擇的選擇,抑或不選擇的選擇,",
                    "很有趣的一件事:當(dāng)面臨(太多的)選擇,人們會(huì)如何選擇;", "同時(shí),人們又會(huì)如何看待錯(cuò)過。" };
            String[] text3 = { "在開始和結(jié)束之間,選擇了什么,又會(huì)錯(cuò)過什么,我還不知道。" };
            String[] text4 = { "你會(huì)知道么?" };
            //
            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);
            //
            // 定義一個(gè)圖片

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

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

標(biāo)簽: 代碼

版權(quán)申明:本站文章部分自網(wǎng)絡(luò),如有侵權(quán),請(qǐng)聯(lián)系:west999com@outlook.com
特別注意:本站所有轉(zhuǎn)載文章言論不代表本站觀點(diǎn)!
本站所提供的圖片等素材,版權(quán)歸原作者所有,如需使用,請(qǐng)與原作者聯(lián)系。

上一篇:Java String字符串補(bǔ)0或空格

下一篇:Java操作redis(增刪改查)