itext 创建pdf

本文介绍了一个使用Java和iText库生成PDF文件的例子。该示例展示了如何将图片添加到PDF表格中,并根据不同条件调整表格布局。此外,还介绍了如何控制表格边框样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.slin.utils; /**
 * Created by Administrator on 2015/5/4.
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
public class T {
    public static void main(String[] args) throws Exception{
        Document pdfDoc = new Document();
        FileOutputStream pdfFile =
                new FileOutputStream(new File("D:/Temp/firstPdf.pdf"));

        Image image = Image.getInstance("D:/Temp/1.jpg");


        PdfWriter.getInstance(pdfDoc, pdfFile);
        pdfDoc.open();
        PdfPTable pdfPTable=null;
         int index=2;
         int a=index%5;
         int b=index/5;
        System.out.println("a==="+a);
        System.out.println("b==="+b);
        if(index<5){
            pdfPTable=new PdfPTable(index);
            pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
            for(int i=0;i<index;i++){
                pdfPTable.addCell(image);
                pdfDoc.add(pdfPTable);
            }

        }else {
            for(int i=0;i<index;i++){

                if(i%5==0){
                    pdfPTable=new PdfPTable(5);
                    pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
                }
                if(b*5==i){
                    System.out.println("i="+i);
                    pdfPTable=new PdfPTable(a);
                    pdfPTable.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
                }


                pdfPTable.addCell(image);
                pdfDoc.add(pdfPTable);
            }
        }
        
        pdfDoc.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值