itextpdf 文件操作

该博客展示了如何使用iTextPDF库在Java中对PDF文件进行操作,包括添加页码、创建文本注释、合并PDF文件以及移动页面。fun方法添加了页码,fun2方法添加了注释,fun3方法实现了文件合并,而fun4方法则演示了如何移动PDF页面。
摘要由CSDN通过智能技术生成

itextpdf 文件操作

                 

                          

****************

示例

           

public class Test5 {

    private static final String source = "./source/静夜思.pdf";
    private static final String source2 = "./source/春晓.pdf";

    private static final String dest = "./dest/pageNumber.pdf";
    private static final String dest2 = "./dest/annotation.pdf";
    private static final String dest3 = "./dest/merge.pdf";
    private static final String dest4 = "./dest/move.pdf";

    public static void fun() throws Exception{  //添加页码
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(source), new PdfWriter(dest));
        Document document = new Document(pdfDocument);

        int numberOfPages = pdfDocument.getNumberOfPages();
        for (int i=1; i<=numberOfPages;i++){
            document.showTextAligned(new Paragraph(String.format("%s/%s", i, numberOfPages))
                            .setFontColor(ColorConstants.ORANGE).setBold(),
                    559, 806, i, TextAlignment.CENTER, VerticalAlignment.TOP, 0);
        }

        document.close();
    }

    public static void fun2() throws Exception{  //添加注释
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(source),new PdfWriter(dest2));

        Rectangle rectangle = new Rectangle(400, 600, 200, 200);
        PdfAnnotation annotation = new PdfTextAnnotation(rectangle)
                //.setTitle(new PdfString("title"))
                .setText(new PdfString("annotation 注释"))
                .setContents(new PdfString("this is an annotation"))
                .setColor(ColorConstants.GREEN);

        pdfDocument.getFirstPage().addAnnotation(annotation);
        pdfDocument.close();
    }

    public static void fun3() throws Exception{  //文件合并
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(source), new PdfWriter(dest3));
        PdfDocument pdfDocument2 = new PdfDocument(new PdfReader(source2));

        pdfDocument2.copyPagesTo(1,1,pdfDocument,1);
        pdfDocument.close();
        pdfDocument2.close();
    }

    public static void fun4() throws Exception{  //文件移动
        PdfDocument pdfDocument = new PdfDocument(new PdfReader(dest3), new PdfWriter(dest4));
        pdfDocument.movePage(2,1);
        pdfDocument.close();
    }

    public static void main(String[] args) throws Exception{
        fun();
        fun2();
        fun3();
        fun4();
    }
}

             

源文件:静夜思.pdf、春晓.pdf

                           

                            

                     

fun:添加页码

                            

                   

fun2:添加注释

                            

                    

fun3:文件合并

                            

                             

                 

fun4:文件移动

                             

                             

                         

                             

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值