zoukankan      html  css  js  c++  java
  • 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(); } } } }
  • 相关阅读:
    ES6新特性概览
    JavaScript一些不常用的写法
    使用HTML5的十大原因
    利用HTML5开发Android(7)---HTML5本地存储之Database Storage
    利用HTML5开发Android(6)---构建HTML5离线应用
    利用HTML5开发Android(5)---HTML5地理位置服务在Android中的应用
    利用HTML5开发Android(4)---HTML5本地存储之Web Storage
    利用HTML5开发Android(3)---Android中的调试
    利用HTML5开发Android(2)---Android中构建HTML5应用
    javascript中的prototype和constructor
  • 原文地址:https://www.cnblogs.com/sjzxs/p/14068392.html
Copyright © 2011-2022 走看看