itext操作pdf,创建时选择页添加数据

public class demo {
    public static void put(int pagecount,int index,int location)  {
        Rectangle rectangle = new Rectangle(PageSize.A4);
        rectangle.setBackgroundColor(Color.LIGHT_GRAY);
        rectangle.setBorder(Rectangle.BOX);
        rectangle.setBorderColor(Color.DARK_GRAY);
        rectangle.setBorderWidth(5);
        Document doc =new Document(rectangle);
        try {
            PdfReader reader =new PdfReader("E://test.pdf");
            PdfWriter write =PdfWriter.getInstance(doc,new FileOutputStream("E://test.pdf"));
            BaseFont bfChinese =BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            Font fontchinese = new Font(bfChinese, 16, Font.NORMAL);
            doc.open();
            PdfContentByte cb = write.getDirectContent();

            cb.beginText();
            cb.setFontAndSize(bfChinese, 12);
            cb.showTextAligned(0, "text" + "This text is centered", 250, 700, 0);
            cb.endText();

            doc.add(new Chunk("Chapter 1").setLocalDestination("1"));
            for(int i=2;i<=pagecount;i++){
                doc.newPage();
                doc.add(new Chunk("Chapter "+i).setLocalDestination(i+""));
                if(i==index){
                    Paragraph p1= new Paragraph("在"+index+"页加入内容",fontchinese);
                    p1.setAlignment(location);
                    // center 1;left 0;right 2;bottom 6;
                    doc.add(p1);
                    Paragraph p2= new Paragraph("在"+index+"页加入内容",fontchinese);
                    p2.setAlignment(location);
                    // center 1;left 0;right 2;bottom 6;
                    doc.add(p2);
                    Paragraph p3= new Paragraph("在"+index+"页加入内容",fontchinese);
                    p2.setAlignment(location);
                    // center 1;left 0;right 2;bottom 6;
                    doc.add(p3);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            doc.close();
        }
    }

测试数据

  public static void main(String[] args) {
        demo.put(5,2,1);
//int pagecount,int index,int location
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值