整理最全的PDF分页工具

首先使用的到的jar包:itextpdf-5.5.11.jar;itext-asian-5.2.0.jar;itext-xtra-5.5.11.jar;itext-2.1.7.jar

 

原理介绍:

需要写一个分页事件继承PdfPageEventHelper ,重写以下方法

public void onOpenDocument(PdfWriter writer, Document document); 
public void onStartPage(PdfWriter writer, Document document); 
public void onEndPage(PdfWriter writer, Document document); 
public void onCloseDocument(PdfWriter writer, Document document); 

分页事件主要在onEndPage方法中实现。

特别注意的是:给 PdfWriter 对象添加事件时要在open方法之前,例:

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file));
 writer.setPageEvent(new HeaderAndFooter());

document.open();

...

 

document.close();

 

以下是本人整理的工具类,直接上代码

public class PdfUtils {

    // 不保存待定的更改。
    public static final int wdDoNotSaveChanges = 0;
    // PDF 格式
    public static final int wdFormatPDF = 17;

    // 基本字体和样式
    public static BaseFont bfChinese;
    public static Font fontChinese;
    // 设置字体大小
    public static Font headFont;
    public static Font keyFont;
    public static Font infoFont;
    public static Font textFont;
    public static Font spaceFont;

    static {
        try {
            // 使用iTextAsian.jar中的字体
            bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
            // 基础字体
            fontChinese = new Font(bfChinese, 14, Font.NORMAL);
            // 设置字体大小
            headFont = new Font(bfChinese, 22, Font.BOLD);
            keyFont = new Font(bfChinese, 16, Font.BOLD);
            infoFont = new Font(bfChinese, 14, Font.BOLD);
            textFont = new Font(bfChinese, 10, Font.NORMAL);
            spaceFont = new Font(bfChinese, 3, Font.NORMAL);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 无参构造方法
     */
    public PdfUtils() {
    }

    /**
     * 添加标题
     * 
     * @param document
     * @param title
     * @param font
     * @throws DocumentException
     */
    public static void addTitle(Document document, String title, Font font) throws DocumentException {
        Paragraph t = new Paragraph(title, font);
        // 居中显示
        t.setAlignment(1);
        document.add(t);
    }

    /**
     * 添加小标题
     * 
     * @param document
     * @param title
     * @param font
     * @throws DocumentException
     */
    public static void addSerTitle(Document document, String title, Font font) throws DocumentException {
        document.add(new Paragraph(title, font));
    }

    /**
     * 添加内容
     * 
     * @param document
     * @param content
     * @param font
     * @throws DocumentException
     */
    public static void addDocument(Document document, String content, Font font) throws DocumentException {
        document.add(new Paragraph(content, font));
    }

    /**
     * 添加段落
     * 
     * @param document
     * @param content
     * @param font
     */
    public static void addParagraph(Document document, String content, Font font) throws DocumentException {
        Paragraph paragraph = new Paragraph(content, font);
        // 首行缩进
        paragraph.setFirstLineIndent(24f);
        // 设置段段落居中
        paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
        // 行间距
        paragraph.setLeading(20f);
        // 设置段落前后间距
        paragraph.setSpacingBefore(5f);
        paragraph.setSpacingAfter(10f);
        // 设置段落连续
        paragraph.setKeepTogether(true);
        document.add(paragraph);
    }

    private static int totalWidth = 330;

    /**
     * 创建PDF表格 (一)
     * 
     * @param colNumber
     * @return
     */
    public static PdfPTable createTable(int colNumber) {
        PdfPTable table = new PdfPTable(colNumber);
        try {
            table.setTotalWidth(totalWidth); // 设置表格的总宽度
            table.setLockedWidth(true); // 锁定宽度
            table.setHorizontalAlignment(Element.ALIGN_CENTER); // 对齐方式,左对齐
            table.getDefaultCell().setBorder(1); // 默认单元格的border为1
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建PDF表格 (二)
     * 
     * @param widths
     * @return
     */
    public static PdfPTable createTable(float[] widths) {
        PdfPTable table = new PdfPTable(widths);
        try {
            table.setTotalWidth(totalWidth);
            table.setLockedWidth(true);
            table.setHorizontalAlignment(Element.ALIGN_CENTER);
            table.getDefaultCell().setBorder(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return table;
    }

    /**
     * 创建单元格,可调整对其的格式
     * 
     * @param value
     * @param font
     * @param align
     * @return
     */
    public static PdfPCell createCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        return cell;
    }

    /**
     * 创建无边框的单元格,可调整对其的格式
     * 
     * @param value
     * @param font
     * @param align
     * @return
     */
    public static PdfPCell createNoBorderCell(String value, Font font, int align) {
        PdfPCell cell = new PdfPCell();
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        cell.setHorizontalAlignment(align);
        cell.setPhrase(new Phrase(value, font));
        // 设置无边框
  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
itextpdf是一个用于创建和操作PDF文档的开源Java库。它提供了丰富的功能和灵活性,可以满足各种生成和编辑PDF文件的需求。 在itextpdf中,生成PDF分页是通过设置页面布局和页面大小实现的。可以使用Rectangle类来创建页面大小,并使用Document类设置页面布局。要分页,可以使用Chunk或Phrase类来添加内容,并使用add()方法将它们添加到文档中。 首先,你需要创建一个Document对象,并使用PdfWriter类将其与PDF文件关联起来。然后,你可以通过设置页面大小和页面边距来调整文档的布局。接下来,你可以使用Font类和Paragraph类来设置内容的样式和格式。 在添加内容时,你可以使用Chunk或Phrase类来创建段落,并使用add()方法将它们添加到文档中。在每个页面的末尾,你可以使用newPage()方法创建新的页面,并在新页面上继续添加内容。 最后,你需要使用close()方法关闭文档,将其保存为PDF文件。 总结起来,itextpdf生成PDF分页的步骤如下: 1. 创建一个Document对象,并与PDF文件关联。 2. 设置页面大小和页面边距。 3. 使用Font类和Paragraph类设置内容的样式和格式。 4. 使用Chunk或Phrase类创建段落,并使用add()方法将它们添加到文档中。 5. 在每个页面的末尾,使用newPage()方法创建新的页面。 6. 使用close()方法关闭文档,将其保存为PDF文件。 希望这些信息对你有帮助。如果你需要更具体的实例或代码示例,可以参考引用提供的资料。<span class="em">1</span>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值