java 图片转pdf

时间:2025-04-01 16:51:23
package ;

import ;
import ;
import ;
import .slf4j.Slf4j;
import ;
import ;
import ;
import ;
import ;
import ;

import ;
import ;
import ;
import ;
import .*;
/**图片转pdf*/
@Slf4j
public class Img2Pdf {
   public static void main(String[] args) throws IOException {
      //copyFiles("");
      //(tmpDir);
      /*File targetFile = new File("D:\\");
      File imgDir = new File("D:\\img");
      List<File> files = new ArrayList<>();
      if (() && ()) {
         File [] fileArr = ();
         for (File file : fileArr) {
            if (()) {
               (file);
            }
         }
      }
      if (() == 0) {
         ("PDF create error, no images!");
         return;
      }
      createBlankPdf(targetFile, ());
      fillImg2Pdf(targetFile, files);*/
   }
   public static void delDir(FileCopyToDir dto)  {

      try{
         (new File(()));
      }catch(Exception e){
         ();
      }

   }
   public static void copyFiles(FileCopyToDir dto)  {

      try{
         File tmpDir = new File(());
         ();
         for (String str:()) {
            (new File(str),tmpDir);
         }
      }catch(Exception e){
         ();
      }

   }

   public static void imgToPdf(ImgToPdf dto) {
      if((())){
         ("图片转pdf缺少源文件夹参数{}", (dto));
      }
      if((())){
         ("图片转pdf缺少目标文件参数{}", (dto));
      }
      File targetFile = new File(());
      File imgDir = new File(());
      List<File> files = new ArrayList<>();
      if (() && ()) {
         File [] fileArr = ();
         for (File file : fileArr) {
            if (()) {
               (file);
            }
         }
      }
      if (() == 0) {
         ("PDF create error, no images!");
         return;
      }
      createBlankPdf(targetFile, ());
      fillImg2Pdf(targetFile, files);
   }
   /**
    * 创建空白PDF文档
    * @param pageNum
    */
   public static void createBlankPdf(File targetFile,int pageNum) {
      // 创建文档
      PDDocument pdDocument = new PDDocument();
      // 设置文档属性
      createMainInformation(pdDocument);
      for (int i=0; i<pageNum; i++) {
         // 创建页码
           PDPage blankPage = new PDPage();
           // 添加到文档
           (blankPage);
         }
      try {
         // 保存文档
         (targetFile);
      } catch (IOException e) {
         ();
      } finally {
         try {
            // 关闭文档
            ();
         } catch (IOException e) {
            ();
         }
      }
      ("PDF created");
   }
   
   /**
    * 设置文档属性
    * @param pdDocument
    */
   public static void createMainInformation(PDDocument pdDocument) {
      PDDocumentInformation pdd = ();
      ("章三");
      ("图片转换PDF");
      ("PDF Examples");
      ("主题哦");
      Calendar date = new GregorianCalendar();
      (2019, 11, 6); 
      (date);
      (2019, 11, 7); 
      (date);
      ("hi, my pdf!"); 
   }
   
   /**
    * 填充图片到PDF文件
    */
   public static void fillImg2Pdf (File targetFile, List<File> imgFileList) {
      // 加载现有文档
      PDDocument doc = null;
      try {
         doc = (targetFile);
         for (int i = 0; i < (); i++) {
            File imgFile = (i);
            // 获取文档页面
            PDPage page = (i);
            PDImageXObject pdImage = ((),doc);
            PDPageContentStream contents = new PDPageContentStream(doc, page);
            Map<String, Float> sizeMap = getSize(imgFile, page, 30, 30);
            (pdImage, ("x"), ("y"), ("width") , ("height"));
            ("Image inserted");
            ();
         }
         (targetFile);
         
      } catch (IOException e) {
         ();
      } finally {
         if (doc != null) {
            try {
               ();
            } catch (IOException e) {
               ();
            }
         }
      }
   }
   
   /**
    * 
    * @param imgFile 图片文件
    * @param page PDF页
    * @param paddW 横向内边距
    * @param paddH 纵向内边距
    * @return
    */
   private static Map<String, Float> getSize(File imgFile, PDPage page, float paddW, float paddH) {
      Map<String, Float> map = new HashMap<String, Float>();
      BufferedImage bufferedImage;
      try {
         bufferedImage = (imgFile);
         float pageW = ().getWidth() - paddW*2;// 文档真实宽
         float pageH = ().getHeight() - paddH*2;// 文档真实高
         float imageW = ();
         float imageH = ();
         float scale = resize(bufferedImage, pageW, pageH);// 缩放比例
         // 获取位置
         // 文档横坐标
         ("x", paddW + (pageW - imageW*scale)/2);
         // 文档纵坐标
         ("y", pageH - imageH*scale + paddH);
         // 图片缩放后宽度
         ("width", imageW*scale);
         // 图片缩放后高度
         ("height", imageH*scale);
      } catch (IOException e) {
         ();
      }
      return map;
   }
   
   /**
    * 获取图片缩放比例
    * @param imgFile
    * @param pageW
    * @param pageH
    * @return
    */
   private static float resize(File imgFile, float pageW, float pageH) {
      BufferedImage bufferedImage;
      try {
         bufferedImage = (imgFile);
         float width = ();
         float height = ();
         float scale = 1f;
           // 宽高都小于PDF,原样输出
           if (width <= pageW && height <= pageH) {
            return scale;
         }
           // 宽和高都大于PDF
           if (width > pageW && height > pageH) {
            return pageW/width < pageH/height ? pageW/width : pageH/height;
         }
           // 宽和高其一大于PDF
           return width > pageW ? pageW/width : pageH/height;
      } catch (IOException e) {
         ();
      }
        return 1;
   }
   
   /**
    * 获取图片缩放比例
    * @param bufferedImage
    * @param pageW
    * @param pageH
    * @return
    */
   private static float resize(BufferedImage bufferedImage, float pageW, float pageH) {
      float width = ();
      float height = ();
      float scale = 1f;
      // 宽高都小于PDF,原样输出
      if (width <= pageW && height <= pageH) {
         return scale;
      }
      // 宽和高都大于PDF
      if (width > pageW && height > pageH) {
         return pageW/width < pageH/height ? pageW/width : pageH/height;
      }
      // 宽和高其一大于PDF
      return width > pageW ? pageW/width : pageH/height;
   }
}