package cn.hmsoft.helper.pdf;

import java.io.File;

import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.layout.Document;

import cn.hmsoft.helper.StringHelper;

/************************
 * 
 * @note pdf页面实体
 * @author revisit@126.com
 * @Company: www.hmsoft.cn
 * @create 2017年11月25日 上午8:49:35
 * @version 4.0.0
 */
public class ItextDocumentModel {

	/********************
	 * 页面宽度,单位mm
	 */
	private int page_width = 210;

	/********************
	 * 页面高度,单位mm
	 */
	private int page_height = 297;
	/********************
	 * 页面边距left ，right，bottom left
	 */
	private String page_margin = "20 30 20 30";

	protected int margin_top = 10;
	protected int margin_right = 10;
	protected int margin_bottom = 10;
	protected int margin_left = 10;

	public int getPage_width() {
		return page_width;
	}

	public void setPage_width(int page_width) {
		this.page_width = page_width;
	}

	public int getPage_height() {
		return page_height;
	}

	public void setPage_height(int page_height) {
		this.page_height = page_height;
	}

	public String getPage_margin() {
		return page_margin;
	}

	public void setPage_margin(String page_margin) {
		this.page_margin = page_margin;
	}

	protected PdfFont font = null;

	protected PdfFont getFont() {
		return font;
	}

	public Document prepareDocument(File tempFile) {
		return prepareDocument(tempFile, ItextPdfHelper.getPdfFontSTSong());
	}

	public Document prepareDocument(File tempFile, PdfFont font) {
		try {
			String[] array = StringHelper.splitWithSpace(this.page_margin
					.replaceAll("px", ""));
			margin_top = Integer.parseInt(array[0]);
			margin_right = Integer.parseInt(array[1]);
			margin_bottom = Integer.parseInt(array[2]);
			margin_left = Integer.parseInt(array[3]);
		} catch (Exception e) {
			e.printStackTrace();
		}

		Document doc = ItextPdfHelper.createPdf(tempFile, ItextPdfHelper
				.getPageSizeByMillimeter(this.page_width, this.page_height));

		doc.setMargins(margin_top, margin_right, margin_bottom, margin_left);
		if (font == null)
			font = ItextPdfHelper.getPdfFontSTSong();
		this.font = font;
		doc.setFont(font);

		return doc;
	}

	public void rotate() {
		int w = this.page_width;
		this.page_width = this.page_height;
		this.page_height = w;
	}
}
