package com.artifex.mupdfdemo;

import android.graphics.Bitmap;
import android.util.Log;

import com.lowagie.text.BadElementException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;


/**
 * Created by Jammy on 2016/6/23.
 */
public class SavePdf {

    private float defaultScale = 0.90756303f;

    public void setWidthScale(float widthScale) {
        this.widthScale = widthScale;
    }

    public void setHeightScale(float heightScale) {
        this.heightScale = heightScale;
    }

    float widthScale;
    float heightScale;
    String inPath;/////当前的PDF地址
    String outPath;////要输出的PDF地址
    private int pageNum;/////签名所在的页码
    private Bitmap bitmap;//////签名图像
    private float scale;
    private float density;  ///手机屏幕的分辨率密度

    private float width;
    private float height;

    /**
     * 设置放大比例
     *
     * @param scale
     */
    public void setScale(float scale) {
        this.scale = scale;
    }

    /**
     * 设置宽高
     *
     * @param
     */
    public void setWH(float width, float height) {
        this.width = width;
        this.height = height;
    }

    /**
     * 设置分辨率密度
     *
     * @param density
     */
    public void setDensity(float density) {
        this.density = density;
    }

    /**
     * 设置嵌入的图片
     *
     * @param bitmap
     */
    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    /**
     * 设置需要嵌入的页面
     *
     * @param pageNum
     */
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public SavePdf(String inPath, String outPath) {
        this.inPath = inPath;
        this.outPath = outPath;
    }

    /**
     * 将图片加入PDF并保存
     */
    public void addText() {
        try {
            PdfReader reader = new PdfReader(inPath, "PDF".getBytes());///打开要写入的PDF
            Log.e("addText", "1");
            FileOutputStream outputStream = new FileOutputStream(outPath);//设置涂鸦后的PDF
            Log.e("addText", "2");
            PdfStamper stamp;
            stamp = new PdfStamper(reader, outputStream);
            Log.e("addText", "3  " + pageNum);
            PdfContentByte over = stamp.getOverContent(pageNum);//////用于设置在第几页打印签名
            Log.e("addText", "4");
            if (bitmap != null) {
                byte[] bytes = Bitmap2Bytes(bitmap);
                Image img = Image.getInstance(bytes);//将要放到PDF的图片传过来，要设置为byte[]类型
                com.lowagie.text.Rectangle rectangle = reader.getPageSize(pageNum);
                img.setAlignment(Image.MIDDLE);// 图像在文档中的对齐方式
                img.scalePercent(rectangle.getWidth() * widthScale * 100);
                img.setAbsolutePosition(width * (rectangle.getWidth() * widthScale) * (scale), rectangle.getHeight() - ((height) * (rectangle.getWidth() * widthScale) * (scale / defaultScale)) + img.getHeight() / 2 * widthScale * 100);
                over.addImage(img);
            }
            Log.e("addText", "5");
            stamp.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BadElementException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    /**
     * 将BitMap转换为Bytes
     *
     * @param bm
     * @return
     */
    public byte[] Bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }
}
