java实现给图片、pdf、office(docx、xlsx、pptx)添加水印

更新时间:2023-07-31 06:12:17 阅读: 评论:0

import s.io.IOUtils;
import org.apache.poi.sl.urmodel.PictureData;
import org.apache.poi.sl.urmodel.PictureData.PictureType;
伟大英文import org.apache.poi.ss.urmodel.ClientAnchor;
import org.apache.poi.ss.urmodel.Drawing;
import org.apache.poi.ss.urmodel.Picture;
import org.apache.poi.ss.urmodel.Sheet;
import org.apache.poi.ss.urmodel.Workbook;
import org.apache.poi.xslf.urmodel.XMLSlideShow;
import org.apache.poi.xslf.urmodel.XSLFPictureShape;
import org.apache.poi.xslf.urmodel.XSLFSlide;
import org.apache.poi.xssf.urmodel.XSSFWorkbook;
import org.apache.del.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.urmodel.XWPFDocument;
import org.apache.poi.xwpf.urmodel.XWPFHeader;
import org.apache.poi.xwpf.urmodel.XWPFParagraph;
import lbeans.XmlObject;
import org.openxmlformats.schemas.prentationml.x2006.main.CTSlide;
import DocumentException;
import Element;
import pdf.BaFont;
import pdf.PdfContentByte;
import pdf.PdfGState;
import pdf.PdfReader;
import pdf.PdfStamper;
public class WaterMarkUtils {
/**
* @param args
*/
public static void main1(String[] args) {
// 原图位置, 输出图⽚位置, ⽔印⽂字颜⾊, ⽔印⽂字
try {
new WaterMarkUtils().mark("D:/pdf/app1.png", "D:/pdf/app2.png", d, "圖⽚來源:XXX");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 图⽚添加⽔印
*
* @param srcImgPath
* 需要添加⽔印的图⽚的路径
* @param outImgPath
* 添加⽔印后图⽚输出路径
* @param markContentColor
* ⽔印⽂字的颜⾊
* @param waterMarkContent
* ⽔印的⽂字
*/
public static void mark(String srcImgPath, String outImgPath, Color markContentColor, String waterMarkContent) throws Exception {        try {
// 读取原图⽚信息
File srcImgFile = new File(srcImgPath);
Image srcImg = ad(srcImgFile);
int srcImgWidth = Width(null);
int srcImgHeight = Height(null);
苹果手机介绍// 加⽔印
BufferedImage bufImg = new BufferedImage(srcImgWidth, srcImgHeight, BufferedImage.TYPE_INT_RGB);
Graphics2D g = ateGraphics();
g.drawImage(srcImg, 0, 0, srcImgWidth, srcImgHeight, null);
// Font font = new Font("Courier New", Font.PLAIN, 12);
Font font = new Font("宋体", Font.PLAIN, 20);
g.tColor(markContentColor); // 根据图⽚的背景设置⽔印颜⾊
g.tFont(font);
int x = (srcImgWidth - getWatermarkLength(waterMarkContent, g)) / 2;
int y = srcImgHeight / 2;
// int x = (srcImgWidth - getWatermarkLength(watermarkStr, g)) / 2;
// int y = srcImgHeight / 2;
g.drawString(waterMarkContent, x, y);
g.drawString(waterMarkContent, x, y);
g.dispo();
// 输出图⽚
FileOutputStream outImgStream = new FileOutputStream(outImgPath);
ImageIO.write(bufImg, "jpg", outImgStream);
outImgStream.flush();
outImgStream.clo();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取⽔印⽂字总长度
北京奥运会开幕式*
* @param waterMarkContent
* ⽔印的⽂字
* @param g
* @return ⽔印⽂字总长度
*/
public static int getWatermarkLength(String waterMarkContent, Graphics2D g) {
Font()).CharArray(), 0, waterMarkContent.length());
}
/**
* 给图⽚添加⽔印
*
* @param iconPath
* ⽔印图⽚路径
* @param srcImgPath
* 源图⽚路径
* @param targerPath
* ⽬标图⽚路径
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath) {
markImageByIcon(iconPath, srcImgPath, targerPath, null);
}
/**
* 给图⽚添加⽔印、可设置⽔印图⽚旋转⾓度
*
* @param iconPath
* ⽔印图⽚路径
* @param srcImgPath
* 源图⽚路径
* @param targerPath
* ⽬标图⽚路径
* @param degree
* ⽔印图⽚旋转⾓度
*/
public static void markImageByIcon(String iconPath, String srcImgPath, String targerPath, Integer degree) {
OutputStream os = null;
try {
Image srcImg = ad(new File(srcImgPath));
BufferedImage buffImg = new Width(null), Height(null),
BufferedImage.TYPE_INT_RGB);
// 得到画笔对象
// Graphics g= Graphics();
Graphics2D g = ateGraphics();
// 设置对线段的锯齿状边缘处理
g.tRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g.Width(null), Height(null), Image.SCALE_SMOOTH), 0,                    0, null);
if (null != degree) {
// 设置⽔印旋转
}
// ⽔印图象的路径⽔印⼀般为gif或者png的,这样可设置透明度
// ⽔印图象的路径⽔印⼀般为gif或者png的,这样可设置透明度
ImageIcon imgIcon = new ImageIcon(iconPath);
// 得到Image对象。
Image img = Image();
float alpha = 0.5f; // 透明度
g.Instance(AlphaComposite.SRC_ATOP, alpha));            // 表⽰⽔印图⽚的位置
g.drawImage(img, 150, 300, null);
g.Instance(AlphaComposite.SRC_OVER));
g.dispo();
os = new FileOutputStream(targerPath);
// ⽣成图⽚
ImageIO.write(buffImg, "JPG", os);
System.out.println("图⽚完成添加Icon印章。。。。。。");
} catch (Exception e) {中国象棋教程
e.printStackTrace();
} finally {
try {
if (null != os)
os.clo();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* @param args
*/
public static void main2(String[] args) {
String srcImgPath = "D:/pdf/kdmt.jpg";
String iconPath = "D:/pdf/qlq.jpeg";
String targerPath = "D:/pdf/qlq1.jpeg";
String targerPath2 = "D:/pdf/qlq2.jpeg";
/
/ 给图⽚添加⽔印
WaterMarkUtils.markImageByIcon(iconPath, srcImgPath, targerPath);
// 给图⽚添加⽔印,⽔印旋转-45
WaterMarkUtils.markImageByIcon(iconPath, srcImgPath, targerPath2, -45);
}
/**
* pdf设置⽂字⽔印
*
* @param inputPath
* @param outPath
* @param markStr
* @throws DocumentException
* @throws IOException
*/
public static void tPdfWatermark(String inputPath, String outPath, String markStr)
throws DocumentException, IOException {
File file = new File(outPath);
if (!ists()) {
try {
} catch (IOException e) {
e.printStackTrace();新手练车
}
一个不落}
BufferedOutputStream bufferOut = null;
try {
bufferOut = new BufferedOutputStream(new FileOutputStream(file));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
PdfReader reader = new PdfReader(inputPath);
PdfStamper stamper = new PdfStamper(reader, bufferOut);
int total = NumberOfPages() + 1;
PdfContentByte content;
BaFont ba = ateFont("STSong-Light", "UniGB-UCS2-H", BaFont.EMBEDDED);
// BaFont ba = ateFont("/data/tmis/uploads/file/,1", BaFont.IDENTITY_H,
// BaFont.EMBEDDED);
PdfGState gs = new PdfGState();
for (int i = 1; i < total; i++) {
content = OverContent(i);// 在内容上⽅加⽔印
//content = UnderContent(i);// 在内容下⽅加⽔印
gs.tFillOpacity(0.2f);
// content.tGState(gs);
content.beginText();
//content.tRGBColorFill(192, 192, 192);
content.tRGBColorFill(220, 20, 60);
content.tFontAndSize(ba, 50);
content.tTextMatrix(100, 250);林亦含
content.showTextAligned(Element.ALIGN_CENTER, markStr, 250, 400, 55);
// content.showTextAligned(Element.ALIGN_CENTER,
// "检测管理信息系统!",400,250, 55);
content.tRGBColorFill(0, 0, 0);
content.tFontAndSize(ba, 8);
}
stamper.clo();
try {
bufferOut.flush();
bufferOut.clo();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main3(String[] args) {
try {
tPdfWatermark("D:/pdf/app1.pdf", "D:/pdf/app2.pdf", "我是⽔印");
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* word⽂字⽔印
* @param inputPath
* @param outPath
* @param markStr
*/
public static void tWordWaterMark(String inputPath, String outPath, String markStr, String fileType) throws Exception {        if("docx".equals(fileType)) {
File inputFile = new File(inputPath);
XWPFDocument doc = null;
try {
doc = new XWPFDocument(new FileInputStream(inputFile));
} catch (FileNotFoundException e) {纹身龙图片
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
XWPFParagraph paragraph = ateParagraph();
//XWPFRun ateRun();
//run.tText("The Body:");
// create header-footer
XWPFHeaderFooterPolicy headerFooterPolicy = HeaderFooterPolicy();
if (headerFooterPolicy == null) headerFooterPolicy = ateHeaderFooterPolicy();
// create default Watermark - fill color black and not rotated
// get the default header
// Note: createWatermark also ts FIRST and EVEN headers

本文发布于:2023-07-31 06:12:17,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/1124449.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:设置   路径   印图
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图