package cn.hmsoft.helper.pdf;

import java.io.File;
import org.springframework.stereotype.Service;

import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Image;

import cn.hmsoft.frame.exception.BusinessException;
import cn.hmsoft.helper.pdf.ItextPdfHelper;
import cn.hmsoft.log.LogHelper;


/*****************
 * 南艺答卷pdf
 * 
 * @author YeFeng
 *
 */
@Service
public class PdfService {

	public void makePdf(String path, PageSize size) {
		File pPath = new File(path);
		if (!pPath.exists())
			throw new BusinessException("路径【" + path + "】不存在！");
		if (!pPath.isDirectory())
			throw new BusinessException("路径【" + path + "】不是有效文件夹！");

		File[] stdPath = pPath.listFiles();
		LogHelper.info("[MakePdf]：一共【" + stdPath.length + "】个考生文件夹");

		// 创建文件夹
		File pdfFolder = new File(pPath.getPath() + File.separator + "pdf");
		pdfFolder.mkdir();

		for (File std : stdPath) {
			if (!std.getName().equals("pdf") || std.listFiles().length == 0)
				continue;
			try {
				this.makePdf(std, pdfFolder, size);
			} catch (Exception e) {
				LogHelper.error("[MakePdf]：考生【" + std.getName() + "】无法生成pdf文件");
			}
		}

	}

/*	private void makePdf(File std, File pdfFolder, PageSize size) throws Exception {
		File stdFile = new File(pdfFolder.getPath() + File.separator + std.getName() + ".pdf");
		Document doc = ItextPdfHelper.createPdf(stdFile, size, null);
		for (File img : std.listFiles()) {
			Image imgUpload = new Image(ImageDataFactory.create(img.getPath()));
			imgUpload.scaleToFit(size.getWidth(), size.getHeight());
			imgUpload.setFixedPosition(0, 0);
			doc.add(imgUpload);
			doc.add(new AreaBreak());
		}
		doc.close();
	}*/
	
	public void makePdf(File std, File pdfFolder, PageSize size) throws Exception {
		  File stdFile = new File(pdfFolder.getPath() + File.separator + std.getName() + ".pdf");
		  Document doc = ItextPdfHelper.createPdf(stdFile, size, null);
		  for (File img : std.listFiles()) {
		   try {
		    Image imgUpload = new Image(ImageDataFactory.create(img.getPath()));
		    imgUpload.scaleToFit(size.getWidth(), size.getHeight());
		    imgUpload.setFixedPosition(0, 0);
		    doc.add(imgUpload);
		    doc.add(new AreaBreak());
		   } catch (Exception e) {
		    LogHelper.warn("文件【" + img.getPath() + "】不是有效的图片文件");
		   }
		  }
		  doc.close();
		 }
	
	public static void main(String[] args) throws Exception {
		String aspect_name ="舞蹈学-命题作文";
		//StdEnrolSubjectDao stdDataDao = new StdEnrolSubjectDao();
		// List<Map<String, Object>> lstStdData = stdDataDao.listStd(aspect_name);
		//for(int i=0;i<=lstStdData.size();i++) {
			String cert_id ="32011120040302082X";
			String path = "E:\\0403---南艺答卷\\答案\\舞蹈学-命题作文\\32011120040302082X";
			File std = new File(path);
			int page_width =595 ;
			int page_height = 842;
			PdfService pdf = new PdfService();
			pdf.makePdf(path, ItextPdfHelper
					.getPageSizeByMillimeter(page_width, page_height));
			File pdfFolder = new File("E:\\0403---南艺答卷\\答案\\舞蹈学-命题作文\\1pdf");
			pdf.makePdf(std, pdfFolder, ItextPdfHelper
					.getPageSizeByMillimeter(page_width, page_height));
			
		//}
		
	}
		

}
