Java中实现PDF加水印的最佳实践是?
- 内容介绍
- 文章标签
- 相关推荐
本文共计866个文字,预计阅读时间需要4分钟。
本文分享了Java PDF加水印的具体代码示例,供大家参考学习。以下是一个简单的代码示例:
javaimport com.itextpdf.tool.xmlworker5.5.10.xmlworker;
public class PDFWatermarkExample { public static void main(String[] args) { // 加载PDF文档 Document document=new Document(); PdfReader reader=new PdfReader(source.pdf); PdfContentByte canvas=PdfContentByte.getOver(document);
// 设置水印文字 String watermarkText=Confidential;
// 设置水印字体、大小、颜色 Font font=new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD, BaseColor.BLUE); canvas.saveState(); canvas.setFontAndSize(font); canvas.setColorStroke(BaseColor.LIGHT_GRAY); canvas.beginText(); canvas.showText(280, 700, watermarkText); canvas.endText(); canvas.restoreState();
// 写入水印后的PDF文档 PdfWriter.getInstance(document, new FileOutputStream(watermarked.pdf)); document.open(); PdfImportedPage page=reader.getImportedPage(1); document.add(page); document.close(); reader.close(); }}
以上代码展示了如何使用iText PDF库在PDF文档中添加水印。首先,加载源PDF文档,然后创建一个PDFContentByte对象来添加水印。设置水印文字、字体、大小和颜色,最后将水印后的PDF文档写入到新文件中。
本文实例为大家分享了javapdf加水印的具体代码供大家参考具体内容如下引入依赖com.itextpdf.toolxmlworker5.5.10co本文实例为大家分享了java pdf加水印的具体代码供大家参考具体内容如下
引入依赖
com.itextpdf.tool
xmlworker
5.5.10
com.itextpdf
itextpdf
5.5.10
‘/static/fonts/SIMYOU.TTF 字体 本机没有的话, 可以百度下载
/**
* pdf 加水印
*
* return
*/
public byte[] pdfAddWaterMark(byte[] byes) {
String fileName UUID.randomUUID().toString() ".pdf";
String courseFile "";
try {
// 第二种获取项目路径 D:\git\daotie\daotie
//生成临时文件 , 读取完删除
File directory new File("");// 参数为空
courseFile directory.getCanonicalPath() "/";
} catch (IOException e) {
e.printStackTrace();
}
byte[] returnBytes null;
// 待加水印的文件
PdfReader reader null;
PdfStamper stamper null;
// ByteArrayOutputStream baos null;
FileOutputStream os null;
try {
reader new PdfReader(byes);
// 加完水印的文件
// baos new ByteArrayOutputStream();
// stamper new PdfStamper(reader, baos);
// 加完水印的文件
os new FileOutputStream(courseFile fileName);
stamper new PdfStamper(reader, os);
int total reader.getNumberOfPages() 1;
PdfContentByte content;
// BaseFont font BaseFont.createFont();
BaseFont basefont BaseFont.createFont("/static/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//这里的字体设置比较关键这个设置是支持中文的写法
/*BaseFont base BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体*/
/*//设置透明度
PdfGState gs new PdfGState();
gs.setFillOpacity(1f);
gs.setStrokeOpacity(1f);*/
PdfContentByte under;
com.itextpdf.text.Rectangle pageRect null;
// 循环对每页插入水印
for (int i 1; i pageRect stamper.getReader().getPageSizeWithRotation(i); // 计算水印X,Y坐标 float x (float) (pageRect.getWidth() / 1.98); float y (float) (pageRect.getHeight() / 2.8); // 获得PDF最顶层 under stamper.getOverContent(i); under.saveState(); // set Transparency PdfGState gs new PdfGState(); // 设置透明度为0.2 gs.setFillOpacity(1.f); under.setGState(gs); under.restoreState(); under.beginText(); under.setFontAndSize(basefont, pageRect.getHeight() / 17); under.setColorFill(BaseColor.RED); // 水印文字成45度角倾斜 System.out.println("width" pageRect.getWidth()); System.out.println("height" pageRect.getHeight()); System.out.println("x" x); System.out.println("y" y); under.showTextAligned(Element.ALIGN_CENTER, "图片仅供预览不可用于商业用途", x, y, 45); // 添加水印文字 under.endText(); under.setLineWidth(1f); under.stroke(); } // returnBytes baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { try { stamper.close(); if (os ! null) { os.close(); } if (reader ! null) { reader.close(); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
本文共计866个文字,预计阅读时间需要4分钟。
本文分享了Java PDF加水印的具体代码示例,供大家参考学习。以下是一个简单的代码示例:
javaimport com.itextpdf.tool.xmlworker5.5.10.xmlworker;
public class PDFWatermarkExample { public static void main(String[] args) { // 加载PDF文档 Document document=new Document(); PdfReader reader=new PdfReader(source.pdf); PdfContentByte canvas=PdfContentByte.getOver(document);
// 设置水印文字 String watermarkText=Confidential;
// 设置水印字体、大小、颜色 Font font=new Font(Font.FontFamily.HELVETICA, 30, Font.BOLD, BaseColor.BLUE); canvas.saveState(); canvas.setFontAndSize(font); canvas.setColorStroke(BaseColor.LIGHT_GRAY); canvas.beginText(); canvas.showText(280, 700, watermarkText); canvas.endText(); canvas.restoreState();
// 写入水印后的PDF文档 PdfWriter.getInstance(document, new FileOutputStream(watermarked.pdf)); document.open(); PdfImportedPage page=reader.getImportedPage(1); document.add(page); document.close(); reader.close(); }}
以上代码展示了如何使用iText PDF库在PDF文档中添加水印。首先,加载源PDF文档,然后创建一个PDFContentByte对象来添加水印。设置水印文字、字体、大小和颜色,最后将水印后的PDF文档写入到新文件中。
本文实例为大家分享了javapdf加水印的具体代码供大家参考具体内容如下引入依赖com.itextpdf.toolxmlworker5.5.10co本文实例为大家分享了java pdf加水印的具体代码供大家参考具体内容如下
引入依赖
com.itextpdf.tool
xmlworker
5.5.10
com.itextpdf
itextpdf
5.5.10
‘/static/fonts/SIMYOU.TTF 字体 本机没有的话, 可以百度下载
/**
* pdf 加水印
*
* return
*/
public byte[] pdfAddWaterMark(byte[] byes) {
String fileName UUID.randomUUID().toString() ".pdf";
String courseFile "";
try {
// 第二种获取项目路径 D:\git\daotie\daotie
//生成临时文件 , 读取完删除
File directory new File("");// 参数为空
courseFile directory.getCanonicalPath() "/";
} catch (IOException e) {
e.printStackTrace();
}
byte[] returnBytes null;
// 待加水印的文件
PdfReader reader null;
PdfStamper stamper null;
// ByteArrayOutputStream baos null;
FileOutputStream os null;
try {
reader new PdfReader(byes);
// 加完水印的文件
// baos new ByteArrayOutputStream();
// stamper new PdfStamper(reader, baos);
// 加完水印的文件
os new FileOutputStream(courseFile fileName);
stamper new PdfStamper(reader, os);
int total reader.getNumberOfPages() 1;
PdfContentByte content;
// BaseFont font BaseFont.createFont();
BaseFont basefont BaseFont.createFont("/static/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//这里的字体设置比较关键这个设置是支持中文的写法
/*BaseFont base BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体*/
/*//设置透明度
PdfGState gs new PdfGState();
gs.setFillOpacity(1f);
gs.setStrokeOpacity(1f);*/
PdfContentByte under;
com.itextpdf.text.Rectangle pageRect null;
// 循环对每页插入水印
for (int i 1; i pageRect stamper.getReader().getPageSizeWithRotation(i); // 计算水印X,Y坐标 float x (float) (pageRect.getWidth() / 1.98); float y (float) (pageRect.getHeight() / 2.8); // 获得PDF最顶层 under stamper.getOverContent(i); under.saveState(); // set Transparency PdfGState gs new PdfGState(); // 设置透明度为0.2 gs.setFillOpacity(1.f); under.setGState(gs); under.restoreState(); under.beginText(); under.setFontAndSize(basefont, pageRect.getHeight() / 17); under.setColorFill(BaseColor.RED); // 水印文字成45度角倾斜 System.out.println("width" pageRect.getWidth()); System.out.println("height" pageRect.getHeight()); System.out.println("x" x); System.out.println("y" y); under.showTextAligned(Element.ALIGN_CENTER, "图片仅供预览不可用于商业用途", x, y, 45); // 添加水印文字 under.endText(); under.setLineWidth(1f); under.stroke(); } // returnBytes baos.toByteArray(); } catch (IOException e) { e.printStackTrace(); } catch (DocumentException e) { e.printStackTrace(); } finally { try { stamper.close(); if (os ! null) { os.close(); } if (reader ! null) { reader.close(); } } catch (DocumentException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }

