首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 程式設計入門

Java 100例:生成 PDF 檔案 (iText)

     編輯:Java 程式設計入門

使用 iText 庫生成 PDF 文件,示範頁面設定、中文字體 (STSong-Light)、水印、頁首頁尾、表格建立與中文內容填入。輸出檔案 tables.pdf。注意:iText 5+ 採 AGPL 授權,商業用途需購買授權或改用開源替代品 (如 OpenPDF)。

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import java.io.*;
import java.awt.Color;

public class PdfDemo {
    public void write() {
        try {
            Document doc = new Document(PageSize.A4, 50, 50, 100, 50);
            PdfWriter.getInstance(doc, new FileOutputStream("tables.pdf"));
            Rectangle page = doc.getPageSize();

            // 中文字體
            BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
            Font font = new Font(bf, 10, Font.NORMAL);
            Font boldFont = new Font(bf, 10, Font.BOLD, new Color(0,0,255));
            Font italicFont = new Font(bf, 10, Font.ITALIC, new Color(0,0,255));

            // 水印
            try {
                Image img = Image.getInstance("test.jpg");
                Watermark wm = new Watermark(img, page.getLeft()+50, page.getTop()-85);
                wm.scalePercent(50);
                doc.add(wm);
            } catch (Exception ignored) {}

            // 頁首/頁尾
            HeaderFooter header = new HeaderFooter(new Phrase("Java 實例一百例", font), false);
            header.setBorder(Rectangle.BOX); header.setAlignment(Element.ALIGN_RIGHT);
            doc.setHeader(header);
            HeaderFooter footer = new HeaderFooter(new Phrase("第 ", font), new Phrase(" 頁", font));
            footer.setAlignment(Element.ALIGN_CENTER); footer.setBorder(Rectangle.TOP);
            doc.setFooter(footer);

            doc.open();
            // 表格
            Table table = new Table(4);
            table.setWidths(new int[]{10,20,10,20});
            table.setWidth(100);
            // 主標題
            Cell main = new Cell(new Phrase("用戶資訊", boldFont));
            main.setColspan(4); main.setHorizontalAlignment(Element.ALIGN_CENTER);
            main.setBorder(Rectangle.NO_BORDER); main.setBackgroundColor(Color.LIGHT_GRAY);
            table.addCell(main);
            // 分組標題
            Cell left = new Cell(new Phrase("收貨人", italicFont));
            left.setColspan(2); left.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(left);
            Cell right = new Cell(new Phrase("訂貨人", italicFont));
            right.setColspan(2); right.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.addCell(right);
            // 內容
            String[][] data = {
                {"姓名","張三","姓名","李四"},
                {"電話","23456789","電話","9876543"},
                {"郵編","100002","郵編","200001"},
                {"地址","北京西城區XX路XX號","地址","上海陸家嘴區XX路XX號"},
                {"Email","zh_san@hotmail.com","Email","li_si@hotmail.com"}
            };
            for (String[] row : data)
                for (String cell : row) table.addCell(new Phrase(cell, font));
            doc.add(table);
            doc.close();
        } catch (Exception e) { e.printStackTrace(); }
    }
    public static void main(String[] args) { new PdfDemo().write(); }
}

現代替代:

  • OpenPDF (iText 4 分支,LGPL) — API 相容,無授權疑慮。
  • Apache PDFBox — 純 Java、功能完整。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved