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

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.HELVETICA, 6, Font.BOLD, BaseColor.WHITE);
        Chunk id = new Chunk("AR", font);
        // with a background color
        id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
        // and a text rise
        id.setTextRise(6);
        document.add(id);
        document.add(Chunk.NEWLINE);
        document.add(new Chunk("Australia "));
        id = new Chunk("AU", font);
        // with a background color
        id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
        // and a text rise
        id.setTextRise(6);
        document.add(id);
        document.add(Chunk.NEWLINE);
        document.add(new Chunk("Belgium "));
        id = new Chunk("BE", font);
        // with a background color
        id.setBackground(BaseColor.BLACK, 1f, 0.5f, 1f, 1.5f);
        // and a text rise
        id.setTextRise(6);
        document.add(id);
        document.add(Chunk.NEWLINE);
        // step 5
        document.close();
    }
}

Phrase:一系列的Chunk

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

/**
 * Writes a list of directors to a PDF file.
 */
public class DirectorPhrases1 {
    
    /** The resulting PDF file. */
    public static final String RESULT = "D:/data/iText/inAction/chapter02/director_phrases_1.pdf";

    /** A font that will be used in our PDF. */
    public static final Font BOLD_UNDERLINED =
        new Font(FontFamily.TIMES_ROMAN, 12, Font.BOLD | Font.UNDERLINE);
    /** A font that will be used in our PDF. */
    public static final Font NORMAL =
        new Font(FontFamily.TIMES_ROMAN, 12);
    
    /**
     * Creates a Phrase with the name and given name of a director using different fonts.
     * @param    rs    the ResultSet containing director records.
     */
    public Phrase createDirectorPhrase(final String name, final String givenName)
        throws UnsupportedEncodingException, SQLException {
        Phrase director = new Phrase();
        director.add(
            new Chunk(name, BOLD_UNDERLINED));
        director.add(new Chunk(",", BOLD_UNDERLINED));
        director.add(new Chunk(" ", NORMAL));
        director.add(new Chunk(givenName, NORMAL));
        return director;
    }
    
    /**
     * Creates a PDF file with director names.
     * @param    filename    the name of the PDF file that needs to be created.
     * @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));
        // step 3
        document.open();
        // step 4
        document.add(createDirectorPhrase("Affleck", "Ben"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Aronofsky", "Darren"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Becker", "Wolfgang"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Beineix", "Jean-Jacques"));
        document.add(Chunk.NEWLINE);
        // step 5
        document.close();
    }
    
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
        new DirectorPhrases1().createPdf(RESULT);
    }
}

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

/**
 * Writes a list of directors to a PDF file.
 */
public class DirectorPhrases2 {
    
    /** The resulting PDF file. */
    public static final String RESULT = "D:/data/iText/inAction/chapter02/director_phrases_2.pdf";

    /** A font that will be used in our PDF. */
    public static final Font BOLD;
    /** A font that will be used in our PDF. */
    public static final Font NORMAL;
    
    static {
        BaseFont timesbd = null;
        BaseFont times = null;
        try {
            // create a font that will be embedded
            timesbd = BaseFont.createFont(
                "c:/windows/fonts/timesbd.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
            // create a font that will be embedded
            times = BaseFont.createFont(
                "c:/windows/fonts/times.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED);
        } catch (DocumentException e) {
            e.printStackTrace();
            System.exit(1);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(1);
        }
        BOLD = new Font(timesbd, 12);
        NORMAL = new Font(times, 12);
    }
    
    /**
     * Creates a Phrase with the name and given name of a director using different fonts.
     * @param    rs    the ResultSet containing director records.
     */
    public Phrase createDirectorPhrase(final String nameStr, final String givenName)
        throws UnsupportedEncodingException, SQLException {
        Phrase director = new Phrase();
        Chunk name = new Chunk(nameStr, BOLD);
        name.setUnderline(0.2f, -2f);
        director.add(name);
        director.add(new Chunk(",", BOLD));
        director.add(new Chunk(" ", NORMAL));
        director.add(new Chunk(givenName, NORMAL));
        director.setLeading(24);
        return director;
    }
    
    /**
     * Creates a PDF file with director names.
     * @param    filename    the name of the PDF file that needs to be created.
     * @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));
        // step 3
        document.open();
        // step 4
        document.add(createDirectorPhrase("Berri", "Claude"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Bielinsky", "Fab"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Cameron", "Gaston"));
        document.add(Chunk.NEWLINE);
        document.add(createDirectorPhrase("Beineix", "Jean-Jacques"));
        document.add(Chunk.NEWLINE);
        // step 5
        document.close();
    }
    /**
     * Main method.
     *
     * @param    args    no arguments needed
     * @throws DocumentException 
     * @throws IOException 
     * @throws SQLException
     */
    public static void main(String[] args)
        throws IOException, DocumentException, SQLException {
        new DirectorPhrases2().createPdf(RESULT);
    }
}

Paragraph:一个Phrase带额外属性和新行

将文本分布在不同行上

列表:一系列的ListItem

DrawInterface:垂直位置标记、分隔符和选项卡

2.3 添加锚点(Anchor)、图像(Image)、章节(Chapter)和剖面(Section)对象

Anchor:内部何外部的链接

Chapter 和 Section:免费的书签

Image:添加光栅格式插图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值