iText实战--使用iText基本构建块

文章详细介绍了使用iTextPDF库在Java中创建PDF文档的过程,包括添加Chunk、Phrase、Paragraph和List对象,以及设置字体、颜色、间距和链接等格式。

2.2 添加块(Chunk)、短句(Phrase)、段落(Paragraph)和列表(List)对象

Chunk:一个字符串、一个字体、一些属性

两行之间的空间:引导(Leading)

setInitialLeading(16);
setTextRise()--设置上升高度

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Writes a list of countries to a PDF file.
 */
public class CountryChunks {
    
    /** The resulting PDF file. */
    public static final String RESULT
        = "D:/data/iText/inAction/chapter02/country_chunks.pdf";
    
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
        new CountryChunks().createPdf(RESULT);
    }
    
    /**
     * Creates a PDF document.
     * @param filename the path to the new PDF document
     * @throws    DocumentException 
     * @throws    IOException 
     * @throws    SQLException
     */
    public void createPdf(String filename)
        throws IOException, DocumentException, SQLException{
    	// step 1
        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(filename))
            .setInitialLeading(16);
        // step 3
        document.open();
        // step 4
        document.add(new Chunk("Argentina "));
        Font font = new Font(FontFamily
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值