iTEXT 使用二 页眉页尾相关方法说明

table的渲染

table.writeSelectedRows(0,-1,235,50,writer.getDirectContent());
document.add(table);
//writeSelectedRows和add 都会渲染一次;

Rectangle 参数

/**
 * Constructs a <CODE>Rectangle</CODE> -object.
 * 这里是设置矩形的左下角和右上角的点位, 定义一个矩形
 * @param llx	lower left x
 * @param lly	lower left y
 * @param urx	upper right x
 * @param ury	upper right y
 */
public Rectangle(final float llx, final float lly, final float urx, final float ury) {
	this.llx = llx;
	this.lly = lly;
	this.urx = urx;
	this.ury = ury;
}

添加水印

@SneakyThrows
@Override
public void onStartPage(PdfWriter writer, Document document) {
	BaseFont baseFont = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	Font font = new Font(baseFont,12);
	if (writer.getPageNumber() < 3) {
		PdfContentByte cb = writer.getDirectContentUnder();
		cb.saveState();
		cb.setColorFill(BaseColor.PINK);
		cb.beginText();
		cb.setFontAndSize(baseFont, 48);
		cb.showTextAligned(Element.ALIGN_CENTER, "My Watermark Under " + writer.getPageNumber(), document.getPageSize().getWidth() / 2, document.getPageSize().getHeight() / 2, 45);
		cb.endText();
		cb.restoreState();
	}
}

PdfContentByte的saveState()和restoreState()方法

图形状态相当于一个栈(stack),SaveState为进栈、RestoreState为出栈。SaveState和RestoreState函数在代码中应该成对出现,
如果想使用RestoreState恢复图形状态,则必须首先使用了SaveState保存了图形状态;如果使用了SaveState保存了图形状态,则后面
必须有一个RestoreState函数与之对应。否则,将会抛出IllegalPdfSyntaxException异常。

点击查看示例

绘制线条

/**
 * 表格划线设置表头 绘制横线
 * @param cb
 * @param xFrom
 * @param yFrom
 * @param xTo
 * @param yTo
 */
public  void drawLine(PdfContentByte cb, float xFrom, float yFrom, float xTo, float yTo) {
	cb.saveState();// 必须和restoreState成对出现
	cb.moveTo(xFrom,yFrom);//起点
	cb.lineTo(xTo,yTo);//终点
	cb.setLineWidth(2);//设置线的宽度
	cb.stroke();
	cb.restoreState();
}

固定高度缩放图片

/**
 * 按照统一的高度缩放图片
 *
 * @param url               图片地址
 * @param fixedHeight       固定的高度
 * @return
 * @author LukeRen
 */
public static Image getImageScaleByFixedHeight(String url, float fixedHeight){
	if (StringUtils.isEmpty(url)) {
		return  null;
	}
	Image image = null;
	try {
		image = Image.getInstance(url);
	}catch (Exception e){
		log.error("PDF获取图片异常:",e);
		return null;
	}
	if(Objects.nonNull(image)) {
		float height = image.getHeight();
		float width = image.getWidth();
		float newWidth = fixedHeight * width / height;
		image.scaleAbsolute(newWidth, fixedHeight);
	}
	return  image;
}

不超过固定宽高缩放图片

/**
 * 通过固定高宽缩放图片
 *
 * @param url                   图片地址
 * @param maxHeight             最大长度
 * @param maxWidth              最大宽度
 * @return
 */
public static Image scaleImageByMaxHeightAndMaxWidth(String url, float maxHeight, float maxWidth){
	if (StringUtils.isEmpty(url)) {
		return  null;
	}
	Image image = null;
	try {
		image = Image.getInstance(url);
	}catch (Exception e){
		log.error("PDF获取图片异常:",e);
		return null;
	}
	if(Objects.nonNull(image)) {
		float height = image.getHeight();
		float width = image.getWidth();
		float scaleWidth = maxHeight * width / height;
		float scaleHeight = maxWidth * height / width;

		if (scaleWidth >= maxWidth){
			image.scaleAbsolute(maxWidth, scaleHeight);
		}else {
			image.scaleAbsolute(scaleWidth, maxHeight);
		}
	}
	return  image;
}
posted @ 2022-03-10 14:37  那年长安  阅读(207)  评论(0编辑  收藏  举报