Java 寫入pdf文件


import java.awt.Color;
import java.io.File;
import java.io.FileOutputStream;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Chapter;
import com.lowagie.text.Element;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class PDFDemo {
    private Color black = new Color(0, 0, 0); // 黑色
    private Color red = new Color(255, 0, 0); // 紅色
    private Color blue = new Color(0, 0, 255); // 藍色
    private int bold = Font.BOLD; // 粗體
    private int normal = Font.NORMAL; // 正常字體
    private int italic = Font.ITALIC; // 斜體
    private int boldItalic = Font.BOLDITALIC; // 粗斜體
    private float setting = 100; // 首行縮進參數
    public Document createDoc(String filename) throws Exception {
        // 新建document對象
        // 第一個參數是頁面大小。接下來的參數分別是左、右、上和下頁邊距。
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        // 建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁盤中。
        // 創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。
        PdfWriter.getInstance(document, new FileOutputStream(filename));
        return document;
    }
    public void writePdf(String filename, String imgPath) throws Exception {
        Document document = createDoc(filename); // 打開文檔
        document.open(); // 文檔里寫入
        document.add(convertParToChinese("紅色字體", 20, bold, red));
        document.add(new Paragraph("\n"));
        document.add(convertParToChinese("黑色", 18, boldItalic, black));
        document.add(new Paragraph("\n"));
        document.add(convertParToChinese("藍色", 14, normal, blue));
        document.add(new Paragraph("\n"));
        // 文檔寫入圖片
        if (checkFile(imgPath)) {
            Image image = writeImg(imgPath);
            document.add(image);
            document.add(new Paragraph("\n"));
        }
        document.add(new Paragraph("\n"));
        document.add(new Paragraph("\n")); // 生成三列表格
        PdfPTable table = new PdfPTable(3); // 設置表格具體寬度
        table.setTotalWidth(90); // 設置每一列所占的長度
        table.setWidths(new float[] { 50f, 15f, 25f });
        PdfPCell cell1 = new PdfPCell();
        Paragraph para = new Paragraph("aaaaa");
        cell1.setPhrase(para);
        table.addCell(cell1);
        table.addCell(new PdfPCell(new Phrase("IText")));
        table.addCell(new PdfPCell(new Phrase("IText")));
        document.add(table);
        document.add(new Paragraph("\n"));
        document.add(new Paragraph("\n")); // PDF同行顯示
        Paragraph par = new Paragraph();
        Chunk chunk1 = new Chunk(convertChunkByChinese("考試分數:", 20, bold, black));
        Chunk chunk2 = new Chunk(convertChunkByChinese("93", 20, bold, red));
        par.add(chunk1);
        par.add(chunk2); // 設置整體縮進
        par.setFirstLineIndent(setting); // 居中
        Paragraph centerPar = convertParToChinese("劇中測試", 16, italic, black);
        centerPar.setAlignment(Element.ALIGN_CENTER);
        document.add(par); // 新建章節
        // //節標題
        Paragraph chapterTitle = new Paragraph(convertParToChinese("章節標題", 18, boldItalic, blue));
        Chapter chapter1 = new Chapter(chapterTitle, 1);
        chapter1.setNumberDepth(0);
        for (int i = 0; i < 20; i++)
        {
            Paragraph p = new Paragraph((i + 1) + "test!!!!!");
            chapter1.add(p);
        }
        document.add(chapter1); // 5.關閉文檔
        document.close();
    }
    public Image writeImg(String imgPath) throws Exception {
        Image img = Image.getInstance(imgPath); // 控制圖片大小
        img.scaleAbsolute(100, 100);
        return img;
    }
    public boolean checkFile(String path) {
        File file = new File(path);
        if (file.exists()) {
            return true;
        }
        return false;
    }
    public static Paragraph convertParToChinese(String text, int fontsize, int fontStyle, Color color)
            throws Exception {
        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
        Paragraph graph = new Paragraph(text, fontChinese);
        return graph;
    }
    public Chunk convertChunkByChinese(String text, int fontsize, int fontStyle, Color color) throws Exception {
        BaseFont baseFontChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
        Font fontChinese = new Font(baseFontChinese, fontsize, fontStyle, color);
        Chunk chunk = new Chunk(text, fontChinese);
        return chunk;
    }
    public static void main(String[] args) throws Exception {
        PDFDemo pdfDemo = new PDFDemo();
        pdfDemo.writePdf("F:\\大數據\\test.pdf",
                "C:\\Users\\lenovo\\Pictures\\心2.png");
    }

}

 

轉載自:http://blog.sina.com.cn/s/blog_c4ffa28f0102wfeq.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM