package com.zhgd.xmgl.pdf;

import java.awt.FontMetrics;
import java.io.IOException;

import javax.swing.JLabel;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Chunk;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
//import com.itextpdf.text.pdf.PdfWriter;

public class Watermark1 extends PdfPageEventHelper {
	Font FONT = new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD, new GrayColor(0.95f));
	private String waterCont;// 水印内容

	public Watermark1() {

	}

	public Watermark1(String waterCont) {
		this.waterCont = waterCont;
	}

	// @Override
	// public void onEndPage(PdfWriter writer, Document document) {
	// for (int i = 0; i < 5; i++) {
	// for (int j = 0; j < 5; j++) {
	// ColumnText.showTextAligned(writer.getDirectContentUnder(),
	// Element.ALIGN_CENTER,
	// new Phrase(this.waterCont == null ? "" : this.waterCont, FONT), (50.5f +
	// i * 350),
	// (40.0f + j * 150), writer.getPageNumber() % 2 == 1 ? 45 : -45);
	// }
	// }
	// }
	public void onEndPage(PdfWriter writer, Document document) {
        try{
            float pageWidth = document.right()+document.left();//获取pdf内容正文页面宽度
            float pageHeight = document.top()+document.bottom();//获取pdf内容正文页面高度
            //设置水印字体格式
            BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
            Font waterMarkFont = new Font(base, 20, Font.BOLD, BaseColor.LIGHT_GRAY);
            PdfContentByte waterMarkPdfContent = writer.getDirectContentUnder();
            Phrase phrase = new Phrase(waterCont, waterMarkFont);
            //两行三列
            ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER,phrase,
                    pageWidth*0.25f,pageHeight*0.2f,45);
            ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER,phrase,
                    pageWidth*0.25f,pageHeight*0.5f,45);
            ColumnText.showTextAligned(waterMarkPdfContent, Element.ALIGN_CENTER,phrase,
                    pageWidth*0.25f,pageHeight*0.8f,45);
            ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER,phrase,
                    pageWidth*0.65f,pageHeight*0.2f,45);
            ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER,phrase,
                    pageWidth*0.65f,pageHeight*0.5f,45);
            ColumnText.showTextAligned(waterMarkPdfContent,Element.ALIGN_CENTER,phrase,
                    pageWidth*0.65f,pageHeight*0.8f,45);
        }catch(DocumentException de) {
            de.printStackTrace();
        }catch(IOException de) {
            de.printStackTrace();
        }
    }

}