pdf加图片的工具类,条形码图片放到pdf文件上

pdf加图片的工具类完美案例

 package com.app.jsonaction;

import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

public class addSealDemo {

    public static void main(String[] args) throws IOException {
        addSeal(3, "E:/before.pdf", "E:/S2.png", "E:/after.pdf");
    }

    /**
     * 给PDF上添加图片
     * @param 指定要添加图片的pdf页
     * @param savePdf原PDF路径
     * @param savePng原图片路径
     * @param sealPdf新生成PDF路径
     * @throws IOException
     */
    public static void addSeal(int page, String savePdf, String savePng,
            String sealPdf) throws IOException {
        int marginLeft = 30;// 左边距
        int marginBottom = 100;// 底边距
        PdfReader pdfreader = new PdfReader(savePdf);
        // 获得PDF总页数
        int pdfPage = pdfreader.getNumberOfPages();
        if (page <= 0 || page > pdfPage) {
            System.out.println("pdf文件无当前页");
        }
        // 获取指定页的宽和高
        Document document = new Document(pdfreader.getPageSize(page));
        // 获取页面宽度
        float width = document.getPageSize().getWidth();
        // 获取页面高度
        float height = document.getPageSize().getHeight();
        if (pdfreader != null)
            pdfreader.close();
        if (document != null)
            document.close();
        System.out.println("pdfPage=" + pdfPage + ",width = " + width
                + ", height = " + height);
        PdfReader pdf = new PdfReader(savePdf);
        PdfStamper stamper = null;
        try {
            stamper = new PdfStamper(pdf, new FileOutputStream(sealPdf));// 生成的PDF
            PdfContentByte overContent = stamper.getOverContent(page);
            Image image = Image.getInstance(savePng);// 图片名称
            image.scaleAbsolute(180, 40);//重新设置宽高 
        //下面三个方式改变图片大小会导致条形码不能识别
             //  image.scaleToFit(200, 40);//自定义大小
             //image.scalePercent(percent);//表示是原来图像的比例 ;
            //image.setAbsolutePosition((width-width2/3)/2, 750);// 左边距、底边距
               image.scaleAbsolute(160, 40);//重新设置宽高    
            image.setAbsolutePosition((width-width2)/2, 790);// 左边距、底边距

           // image.setAbsolutePosition(marginLeft, marginBottom);// 左边距、底边距
            overContent.addImage(image);
            overContent.stroke();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (null != stamper) {
                    stamper.close();
                }
                if (pdf != null) {
                    pdf.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

}

如果感觉好用,记得关注点赞一下

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值