Flying HTML生成PDF添加水印

HTML转PDF并添加水印

<!-- 用于生成PDF -->
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf</artifactId>
    <version>9.1.20</version>
</dependency>


import java.io.File;
import java.io.FileOutputStream;
import java.util.Random;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.Phrase;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.ColumnText;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfGState;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;

public class HtmlToPdfConverter {
     
    public static void main(String[] args) throws Exception {
        String htmlContent = "

我的标题

测试pdf

"
; // 使用Flying Saucer渲染HTML到临时PDF文件 File tempFile = File.createTempFile("temp", ".pdf"); ITextRenderer renderer = new ITextRenderer(); //设置字体 File fontFile = new File("F:\\weixin\\emlsr\\src\\main\\resources\\font\\simhei.ttf"); BaseFont baseFont = BaseFont.createFont(fontFile.getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); renderer.getFontResolver().addFont(fontFile.getAbsolutePath(), baseFont.getEncoding(), BaseFont.NOT_EMBEDDED); //生成数据 renderer.setDocumentFromString(htmlContent); renderer.layout(); renderer.createPDF(new FileOutputStream(tempFile)); // 用iText打开临时PDF文件,并添加水印 PdfGState gs = new PdfGState(); gs.setFillOpacity(0.5f); // 设置为 50% 的透明度 Font font = new Font(baseFont, 30, Font.NORMAL); PdfReader reader = new PdfReader(tempFile.getAbsolutePath()); PdfStamper stamper = new PdfStamper(reader, new FileOutputStream("c:\\output.pdf")); //水印图片(001) //Image watermarkImage = Image.getInstance("C:\\1.jpg"); //float scale = 0.2f; // 水印图片缩放比例 //watermarkImage.scaleAbsolute(watermarkImage.getWidth() * scale, watermarkImage.getHeight() * scale); //水印平铺(002) // 获取一个页面尺寸作为参考来计算水印图片的缩放比例 //Image watermarkImage = Image.getInstance("C:\\1.jpg"); // 设置水印图片平铺的宽度和高度(例如:将水印图片设为100x100像素) //float tileWidth = 100f; //float tileHeight = 100f; //watermarkImage.scaleAbsolute(tileWidth, tileHeight); //水印随机平铺3个(003) Image watermarkImage = Image.getInstance("C:\\1.jpg"); // 设置水印图片平铺的宽度和高度(例如:将水印图片设为100x100像素) float tileWidth = 100f; float tileHeight = 100f; Random random = new Random(); // 创建一个随机数生成器 int n = reader.getNumberOfPages(); for (int i = 1; i <= n; i++) { PdfContentByte underContent = stamper.getUnderContent(i); underContent.setGState(gs); // 字体添加透明度 ColumnText.showTextAligned(underContent, Element.ALIGN_CENTER, new Phrase("水印文字",font), reader.getPageSize(i).getWidth() / 2, reader.getPageSize(i).getHeight() / 2, 45); // 角度可调整水印倾斜程度 // 获取页面尺寸并计算水印位置(001) //Rectangle pageRect = reader.getPageSizeWithRotation(i); //float x = (pageRect.getWidth() - watermarkImage.getScaledWidth()) / 2; //float y = (pageRect.getHeight() - watermarkImage.getScaledHeight()) / 2; // 添加水印图片 //underContent.addImage(watermarkImage, watermarkImage.getWidth(), 0, 0, watermarkImage.getHeight(), x, y); //水印平铺(002)满屏 //Rectangle pageRect = reader.getPageSizeWithRotation(i); // 计算并添加水印图片的位置(按照平铺效果) //for (float x = 0; x < pageRect.getWidth(); x += tileWidth) { // for (float y = 0; y < pageRect.getHeight(); y += tileHeight) { // underContent.addImage(watermarkImage, tileWidth, 0, 0, tileHeight, x, y); // } //} //(003) Rectangle pageRect = reader.getPageSizeWithRotation(i); // 计算并添加三个随机位置的水印图片 for (int j = 0; j < 3; j++) { // 随机计算X和Y坐标 float x = random.nextFloat() * (pageRect.getWidth() - tileWidth); float y = random.nextFloat() * (pageRect.getHeight() - tileHeight); underContent.addImage(watermarkImage, tileWidth, 0, 0, tileHeight, x, y); } } // 关闭 stamper.close(); reader.close(); tempFile.delete(); System.out.println("带有水印的PDF文件已生成至: output.pdf"); } }

你可能感兴趣的:(java进阶综合提升,html,java)