JAVA解析PDF之IText

本文演示了如何使用PDFWriter实例化文档对象,并通过设置严格图像序列属性,实现PDF中有序插入多个图像文件,包括'otsoe.jpg'、'getacro.gif'等。

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

public static void main(String[] args) {
        
        System.out.println("ImageSequence");
        
        // step 1: creation of a document-object
        Document document = new Document();
        
        try {
            // step 2:
            // we create a writer that listens to the document
            // and directs a PDF-stream to a file
            PdfWriter.getInstance(document, new FileOutputStream("notInSequence.pdf"));
            PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("inSequence.pdf"));
            writer.setStrictImageSequence(true);
            
            // step 3: we open the document
            document.open();
            
            // step 4:
            document.add(new Paragraph("1st image"));
            Image jpg = Image.getInstance("otsoe.jpg");
            document.add(jpg);
            document.add(new Paragraph("2nd image"));
            Image gif= Image.getInstance("getacro.gif");
            document.add(gif);
            document.add(new Paragraph("3rd image"));
            document.add(jpg);
            document.add(new Paragraph("4th image"));
            document.add(gif);
            document.add(new Paragraph("5th image"));
            document.add(jpg);
            document.add(new Paragraph("6th image"));
            document.add(gif);
            document.add(new Paragraph("7th image"));
            document.add(jpg);
        }
        catch(DocumentException de) {
            System.err.println(de.getMessage());
        }
        catch(IOException ioe) {
            System.err.println(ioe.getMessage());
        }
        
        // step 5: we close the document
        document.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值