[原]itext使用總結

一、什麼是iText?

以下從iText的官網(http://itextpdf.com/)摘錄介紹了iText:

What is iText?
If you look at PDF creation, you'll find that graphical designers use desktop applications such as Adobe Acrobat or Adobe InDesign to create a document in a manual or semi-manual process. In another context, PDF documents are created programmatically, using an API to produce PDFs directly from software applications, without or with minimal human intervention. Sometimes the document is created in an intermediary format first, and then converted to PDF. These different approaches demand different software products. The same goes for PDF manipulation. You can update a PDF manually in Adobe Acrobat, but there are also tools that allow forms to be filled out automatically based on information from a database.
iText is a tool that focuses on the automation side of things.

翻譯如下:

如果您關注PDF作品,您會發現圖形設計者用桌面應用程序,如Adobe Acrobat或Adobe InDesign,手工或半手工去生成一份文檔。在另一個場合,PDF文檔通過程序創建,直接從軟件應用調用API來生成PDF文檔,不需要或只需要很少的認爲介入。有時,文檔首先被創建爲中間格式,然後再轉換成PDF。不同的方式需要不同的軟件產品。同樣適用於PDF操作。你可以用Adobe Acrobat手工修改PDF,但也有一些工具允許將數據庫中的信息自動寫入到PDF中。iText就是這樣一個工具。

 

二、庫文件

從http://sourceforge.net/projects/itext/files/下載Java版(iText也支持C#)的JAR包:

iText-5.0.4.jar 核心庫

iTextAsian.jar 資源庫,包含亞洲字體資源。需要手工將該文件中的“com/lowagie/text”路徑改爲“com/itextpdf/text”

 

三、實例

import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfWriter;

public class ITextTest{
    public static final float FONT_SIZE = 12f;
    public static void main(String args[]) throws Exception{
        BaseFont baseFont = BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252,
                BaseFont.NOT_EMBEDDED);
        BaseFont simsunFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
                BaseFont.NOT_EMBEDDED);
   
        Document doc = new Document();
        PdfWriter writer = PdfWriter.getInstance(document,
                new FileOutputStream("c:/iTextTest.pdf"));
        document.open();
               
        PdfContentByte cb = writer.getDirectContent();
        cb.saveState();
       
        //......
       
        cb.restoreState();
       
        doc.close();
    }
   
    /**
     *
     * @param y
     * @return
     */
    public boolean checkOverflow(float y) {
        return y > CONTENT_BOTTOM;
    }

    /**
     *
     * @param doc
     * @param cb
     * @param statement
     * @param pageNum
     * @return
     * @throws Exception
     */
    private void dealWithOverflow(PdfContentByte cb)
            throws Exception {
        cb.restoreState();
        cb.getPdfDocument().newPage();
        cb.saveState();
    }

    /**
     * convert point to cm
     *
     * @param point
     * @return
     */
    public static float point2cm(float point) {
        return point / 72f * 2.54f;
    }

    /**
     * convert cm to point
     *
     * @param cm
     * @return
     */
    public static float cm2point(float cm) {
        return cm * 72f / 2.54f;
    }

    /**
     * convert a logic Y coordinate to a actual coordinate, unit is centimeter
     *
     * @param logic
     * @return
     */
    public static float toActualYCoordinate(float logic) {
        return PAGE_HEIGHT - logic;
    }

    /**
     * show text units of x and y are centimeter, fontSize's unit is point
     *
     * @param cb
     * @param text
     * @param x
     * @param y
     * @param font
     * @param fontSize
     */
    public static void showText(PdfContentByte cb, String text, float x,
            float y, BaseFont font, float fontSize) {
        cb.beginText();
        cb.setFontAndSize(font, fontSize);
        cb.moveText(cm2point(x), cm2point(toActualYCoordinate(y)));
        cb.showText(text);
        cb.endText();
    }

    /**
     * show text in indicated alignment units of x and y are centimeter,
     * fontSize's unit is point
     *
     * @param cb
     * @param alignment
     * @param text
     * @param x
     * @param y
     * @param font
     * @param fontSize
     */
    public static void showTextAligned(PdfContentByte cb, int alignment,
            String text, float x, float y, BaseFont font, float fontSize) {
        cb.beginText();
        cb.setFontAndSize(font, fontSize);
        cb.showTextAligned(alignment, text, cm2point(x),
                cm2point(toActualYCoordinate(y)), 0f);
        cb.endText();
    }

    /**
     * show line, units of fromX, fromY, toX and toY are centimeter, unit of
     * lineWidth is point
     *
     * @param cb
     * @param lineWidth
     * @param fromX
     * @param fromY
     * @param toX
     * @param toY
     */
    public static void showLine(PdfContentByte cb, float lineWidth,
            float fromX, float fromY, float toX, float toY) {
        cb.setLineWidth(lineWidth);
        cb.setLineDash(3, 0, 0);
        cb.moveTo(cm2point(fromX), cm2point(toActualYCoordinate(fromY)));
        cb.lineTo(cm2point(toX), cm2point(toActualYCoordinate(toY)));
        cb.closePathFillStroke();
    }

    /**
     * show dash line
     *
     * @param cb
     * @param lineWidth
     * @param unitsOn
     * @param unitsOff
     * @param fromX
     * @param fromY
     * @param toX
     * @param toY
     */
    public static void showDashLine(PdfContentByte cb, float lineWidth,
            float unitsOn, float unitsOff, float fromX, float fromY, float toX,
            float toY) {
        cb.setLineWidth(lineWidth);// 設置虛線大小
        cb.setLineDash(cm2point(unitsOn), cm2point(unitsOff), 0);

        cb.moveTo(cm2point(fromX), cm2point(fromY));
        cb.lineTo(cm2point(toX), cm2point(toY));

        cb.stroke();

    }

    /**
     *
     * @param d
     * @return
     */
    public static String double2String(double d) {
        String s = "0.00";

        DecimalFormat format = new DecimalFormat(NUMBER_FORMAT_PATTERN_1);
        s = format.format(d);

        return s;
    }

    /**
     *
     * @param month
     * @return
     */
    public static String month2String(Date month, String pattern) {
        String s = "";
        SimpleDateFormat format = new SimpleDateFormat(pattern);
        s = format.format(month);
        return s;
    }
}

發佈了68 篇原創文章 · 獲贊 1 · 訪問量 27萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章