package com.emr.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class AddImgToPdf {
    final private static float A4_weight = 595;//标准A4的宽
    final private static float A4_height = 842;//标准A4的高
    public static String addImgToPdf(String templatePath,String targetPath,String imagePath,int pageNum)  {
      String result="";
        try{
            // 读取模板文件
            InputStream input = new FileInputStream(new File(templatePath));
            PdfReader reader = new PdfReader(input);
            PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(targetPath));
            //读图片
            Image image = Image.getInstance(imagePath);
            // 获取操作的页面
            PdfContentByte under =null;
            if(pageNum==0) {
                stamper.insertPage(1 , reader.getPageSizeWithRotation(1));//新增空白页
                // 获取操作的页面
                 under = stamper.getOverContent(1);
            }else{
                stamper.insertPage(pageNum + 1, reader.getPageSizeWithRotation(pageNum));//新增空白页
                // 获取操作的页面
                 under = stamper.getOverContent(pageNum+1);
            }

            /*处理图片缩放比例*/
            float w = image.getWidth();
            float h = image.getHeight();
            float percent = 0;
            if ((w > A4_weight) && (h < A4_height)) {
                percent = (A4_weight * 100) / w;
            } else if ((w < A4_weight) && (h > A4_height)) {
                percent = (A4_height * 100) / h;
            } else if ((w > A4_weight) && (h > A4_height)) {
                percent = (A4_weight * 100) / w;
                h = (h * percent) / 100;
                if (h > A4_height) {
                    percent = (A4_height * 100) / h;
                }
            }
            // 读取图片流
            image.setAlignment(com.lowagie.text.Image.ALIGN_CENTER);
            if (percent != 100) {
                image.scaleAbsolute(A4_weight, A4_height);
            } else {
                image.scalePercent(percent);
            } //这里是写死要插入的坐标
            float x = 0;
            float y = 0;
            // 添加图片
            image.setAbsolutePosition(x, y);
            under.addImage(image);
            stamper.close();
            reader.close();
            //System.out.println("操作完成");
        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }



    public static void main(String[] args) throws Exception {
        //F:\old\EMR_Medical_Record\target\emr_medical_record-1.0-SNAPSHOT\static\img\delPicImg\202104011839513
        // 模板文件路径
        String templatePath = "C:\\Users\\Administrator\\Desktop\\test\\delPicImg\\331.pdf";

        // 生成的文件路径
        String targetPath = "C:\\Users\\Administrator\\Desktop\\test\\delPicImg\\332.pdf";
        // 图片路径
        String imagePath = "C:\\Users\\Administrator\\Desktop\\test\\delPicImg\\2.jpg";
        // 页数
        int pageNum=1;
        System.out.println(templatePath);

        //String result=AddImgToPdf( templatePath, targetPath, imagePath,pageNum);
        PrintToPdfUtil.addPageNum(templatePath, targetPath);

    }
}
