pdf添加logo

@Override
   public ByteArrayInputStream addLogo(ByteArrayOutputStream out)  {
      ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray());

        PdfReader reader = null;
      try {
         reader = new PdfReader(bais);
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        PdfStamper stamper = null;
      try {
         stamper = new PdfStamper(reader, os);
      } catch (DocumentException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }

        ByteArrayInputStream result = null;
        try {
            //获取印章图片
           Image image = Image.getInstance(this.getClass().getClassLoader().getResource(fileName));  


            //获取盖章位置
            float startX = 0;
            float startY = 0;
            int pageNum = 1;

            //转换坐标
            Rectangle pageRect = reader.getPageSize(pageNum);
            float pageHeight = pageRect.getHeight();
            image.scalePercent(30);
            Rectangle rect = placeImageUpsideDown(startX, startY, image, pageHeight);
            PdfContentByte contentByte = stamper.getOverContent(pageNum);
            image.setAlignment(1);
            image.setAbsolutePosition(80, 770);
            contentByte.addImage(image);

            if (stamper != null) {
                stamper.close();
            }
            result = new ByteArrayInputStream(os.toByteArray());

//            FileUtils.writeByteArrayToFile(new File("D:/tt.pdf"), os.toByteArray());
        } catch (BadElementException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (MalformedURLException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } catch (DocumentException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      } finally {
            org.apache.commons.io.IOUtils.closeQuietly(os);
            if (reader != null) {
                reader.close();
            }
            stamper = null;
            reader = null;
        }
        return result;
    }

    public Rectangle placeImageUpsideDown(float startX, float startY, Image image, float pageHeight) {
        float llx = startX;
        float lly = pageHeight - startY - image.getScaledHeight();
        float urx = llx + image.getScaledWidth();
        float ury = lly + image.getScaledHeight();
        return new Rectangle(llx, lly, urx, ury);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

icool_ali

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值