package com.emr.util;


import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.canvas.PdfCanvas;
import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Image;
import com.itextpdf.layout.element.Paragraph;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 合并pdf并加上水印
 * @date 2023-03-07
 */
@Slf4j
public class NewPdfUtil {


    /***
     * 图片转pdf
     * @param source
     * @param target
     */
    public static void img2Pdf(String source, String target) {
        File file = new File(source);
        if (!file.exists()) {
            log.error("文件不存在");
            return;
        }
        Document document;
        try {
            PdfWriter writer = new PdfWriter(target);
            PdfDocument pdf = new PdfDocument(writer);
            document = new Document(pdf);
            Image image = new Image(ImageDataFactory.create(source));
            image.setAutoScale(true);
            image.setAutoScaleHeight(true);
            image.setAutoScaleWidth(true);
            Paragraph p = new Paragraph().add(image);
            document.add(p);
            //关闭文档
            document.close();
            writer.close();
        } catch (Exception e) {
            log.error("图片:{}，转pdf失败：{}", source, e.getMessage());
        }
    }





}