記錄一次PDF文件生成水印

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>cn.surpass</groupId>
	<artifactId>pdf</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>pdf</name>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi</artifactId>
			<version>3.9</version>
		</dependency>

		<dependency>
			<groupId>org.apache.poi</groupId>
			<artifactId>poi-ooxml</artifactId>
			<version>3.9</version>
		</dependency>
		<dependency>
			<groupId>fr.opensagres.xdocreport</groupId>
			<artifactId>org.apache.poi.xwpf.converter.pdf</artifactId>
			<version>1.0.4</version>
		</dependency>

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itextpdf</artifactId>
			<version>5.4.2</version>
		</dependency>

		<dependency>
			<groupId>dom4j</groupId>
			<artifactId>dom4j</artifactId>
			<version>1.6.1</version>
		</dependency>

		<dependency>
			<groupId>com.itextpdf</groupId>
			<artifactId>itext-asian</artifactId>
			<version>5.2.0</version>
		</dependency>
	</dependencies>
</project>

2.PDF文件添加水印

public void waterMark(String inputFile, String outputFile, String waterMarkName) {
		PdfReader reader = null;
		PdfStamper stamper = null;
		try {
			reader = new PdfReader(inputFile);
			stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

			// 使用系統字體
			BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);

			// 設置水印參數
			PdfGState gs = new PdfGState();
			gs.setFillOpacity(0.3f);
			gs.setStrokeOpacity(1.0f);

			// 計算PDF總頁數
			int total = reader.getNumberOfPages() + 1;

			// 循環對每一頁加水印
			Rectangle pageRect;
			PdfContentByte under;

			// 初始化高度間隔
			int interValHeigh = 0;
			// 初始化縱座標
			int yPosition = -100;
			for (int i = 1; i < total; i++) {
				pageRect = reader.getPageSizeWithRotation(i);
				// 獲取page高度
				int pageHeight = Float.valueOf(pageRect.getHeight()).intValue();
				
				//重新計算高度間隔
				interValHeigh = pageHeight / 3;

				// 得到一個覆蓋在上層的水印文字
				under = stamper.getOverContent(i);
				under.saveState();
				under.setGState(gs);
				under.beginText();
				// 設置水印文字顏色
				under.setColorFill(BaseColor.BLACK);
				// 設置水印文字和大小
				under.setFontAndSize(base, 20);
				yPosition = yPosition + pageHeight + interValHeigh;
				do {
					yPosition = yPosition - interValHeigh;
					under.showTextAligned(Element.ALIGN_LEFT, waterMarkName, 200, yPosition, 30);
				} while (yPosition > 0 );
				under.endText();
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage(), e);
		} finally {
			// 關閉流
			try {
				if (stamper != null) {
					stamper.close();
				}
				if (reader != null) {
					reader.close();
				}
			} catch (Exception e) {
			} 
		}

	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章