}
package aacmm.common.pdf;
import java.io.IOException;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPCell;
/**
*
* @author 霍春华
*
*/
public class PdfUtil {
private static Font pdf20Font = null;
//获取指定内容与字体的单元格
public static PdfPCell getPDFCell(String string, Font font)
{
//创建单元格对象,将内容与字体放入段落中作为单元格内容
PdfPCell cell=new PdfPCell(new Paragraph(string,font));
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//设置最小单元格高度
cell.setMinimumHeight(25);
return cell;
}
//合并行的静态函数
public static PdfPCell mergeRow(String str,Font font,int i) {
//创建单元格对象,将内容及字体传入
PdfPCell cell=new PdfPCell(new Paragraph(str,font));
//设置单元格内容居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//将该单元格所在列包括该单元格在内的i行单元格合并为一个单元格
cell.setRowspan(i);
return cell;
}
//合并列的静态函数
public static PdfPCell mergeCol(String str,Font font,int i) {
PdfPCell cell=new PdfPCell(new Paragraph(str,font));
cell.setMinimumHeight(25);
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//将该单元格所在行包括该单元格在内的i列单元格合并为一个单元格
cell.setColspan(i);
return cell;
}
public static Font getChinese20Font() throws DocumentException, IOException {
if (pdf20Font == null) {
// 设置中文字体和字体样式
BaseFont bfChinese = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
pdf20Font = new Font(bfChinese);
pdf20Font.setSize(20);
pdf20Font.setColor(new GrayColor(0.9f));
}
return pdf20Font;
}
}
package aacmm.common.pdf;
import org.apache.commons.logging.Log;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.GrayColor;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
* @author 霍春华
* pdf打印添加水印
*
*/
public class Watermark extends PdfPageEventHelper {
protected Log logger = org.apache.commons.logging.LogFactory.getLog(this
.getClass());
Font FONT = new Font(FontFamily.HELVETICA, 30, Font.BOLD, new GrayColor(0.9f));
private String waterCont;//水印内容
private String time;//水印内容
public Watermark() {
}
public Watermark(String waterCont,String time) {
this.waterCont = waterCont;
this.time = time;
}
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
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, PdfUtil.getChinese20Font()),
(50.5f+i*350),
(40.0f+j*150),
writer.getPageNumber() % 2 == 1 ? 45 : -45);
ColumnText.showTextAligned(writer.getDirectContentUnder(),
Element.ALIGN_CENTER,
new Phrase(this.time == null ? "" : this.time, PdfUtil.getChinese20Font()),
(50.5f+i*350),
(40.0f+j*150+50),
writer.getPageNumber() % 2 == 1 ? 45 : -45);
}
}
}
catch(Exception e){
logger.error(e);
}
}
}