ppt水印怎么加

更新时间: 阅读: 评论:0

自制飞机模型-庶夫

ppt水印怎么加
2023年3月16日发(作者:西关幼儿园)

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

最近接到⼀个需求,要把客户上传的各种格式的⽂件下载时都要打上⽔印。

这些格式包括所有图⽚格式、pdf、各种版本的office⽂件。

搜罗整理了⼀下,⽬前除了2003版的office还不⽀持⽔印外,基本满⾜需求。

下⾯是新建的WaterMarkUtils,集成了图⽚、pdf、docx、xlsx、pptx等⽂件的添加⽔印的⽅法。

有⽤到poi和itextpdf

maven引⼊

poi

3.16

poi-ooxml

3.16

df

itext-asian

5.2.0

pdfbox

2.0.18

df

itextpdf

5.5.13.1

;

omposite;

troke;

;

;

cs2D;

;

ingHints;

arency;

nderContext;

gle2D;

edImage;

edInputStream;

edOutputStream;

rayOutputStream;

;

putStream;

tFoundException;

tputStream;

ption;

tream;

Stream;

;

O;

con;

s;

eData;

eType;

Anchor;

g;

e;

;

ok;

deShow;

ctureShape;

ide;

rkbook;

aderFooterPolicy;

cument;

ader;

ragraph;

ect;

e;

ntException;

t;

nt;

tentByte;

ate;

der;

mper;

publicclassWaterMarkUtils{

/**

*@paramargs

*/

publicstaticvoidmain1(String[]args){

//原图位置,输出图⽚位置,⽔印⽂字颜⾊,⽔印⽂字

try{

newWaterMarkUtils().mark("D:/pdf/","D:/pdf/",,"圖⽚來源:XXX");

}catch(Exceptione){

tackTrace();

}

}

/**

*图⽚添加⽔印

*

*@paramsrcImgPath

*需要添加⽔印的图⽚的路径

*@paramoutImgPath

*添加⽔印后图⽚输出路径

*@parammarkContentColor

*⽔印⽂字的颜⾊

*@paramwaterMarkContent

*⽔印的⽂字

*/

publicstaticvoidmark(StringsrcImgPath,StringoutImgPath,ColormarkContentColor,StringwaterMarkContent)throwsException{

try{

//读取原图⽚信息

FilesrcImgFile=newFile(srcImgPath);

ImagesrcImg=(srcImgFile);

intsrcImgWidth=th(null);

intsrcImgHeight=ght(null);

//加⽔印

BufferedImagebufImg=newBufferedImage(srcImgWidth,srcImgHeight,_INT_RGB);

Graphics2Dg=Graphics();

age(srcImg,0,0,srcImgWidth,srcImgHeight,null);

//Fontfont=newFont("CourierNew",,12);

Fontfont=newFont("宋体",,20);

or(markContentColor);//根据图⽚的背景设置⽔印颜⾊

t(font);

intx=(srcImgWidth-getWatermarkLength(waterMarkContent,g))/2;

inty=srcImgHeight/2;

//intx=(srcImgWidth-getWatermarkLength(watermarkStr,g))/2;

//inty=srcImgHeight/2;

ring(waterMarkContent,x,y);

ring(waterMarkContent,x,y);

e();

//输出图⽚

FileOutputStreamoutImgStream=newFileOutputStream(outImgPath);

(bufImg,"jpg",outImgStream);

();

();

}catch(Exceptione){

tackTrace();

}

}

/**

*获取⽔印⽂字总长度

*

*@paramwaterMarkContent

*⽔印的⽂字

*@paramg

*@return⽔印⽂字总长度

*/

publicstaticintgetWatermarkLength(StringwaterMarkContent,Graphics2Dg){

tMetrics(t()).charsWidth(Array(),0,());

}

/**

*给图⽚添加⽔印

*

*@paramiconPath

*⽔印图⽚路径

*@paramsrcImgPath

*源图⽚路径

*@paramtargerPath

*⽬标图⽚路径

*/

publicstaticvoidmarkImageByIcon(StringiconPath,StringsrcImgPath,StringtargerPath){

markImageByIcon(iconPath,srcImgPath,targerPath,null);

}

/**

*给图⽚添加⽔印、可设置⽔印图⽚旋转⾓度

*

*@paramiconPath

*⽔印图⽚路径

*@paramsrcImgPath

*源图⽚路径

*@paramtargerPath

*⽬标图⽚路径

*@paramdegree

*⽔印图⽚旋转⾓度

*/

publicstaticvoidmarkImageByIcon(StringiconPath,StringsrcImgPath,StringtargerPath,Integerdegree){

OutputStreamos=null;

try{

ImagesrcImg=(newFile(srcImgPath));

BufferedImagebuffImg=newBufferedImage(th(null),ght(null),

_INT_RGB);

//得到画笔对象

//Graphicsg=phics();

Graphics2Dg=Graphics();

//设置对线段的锯齿状边缘处理

deringHint(_INTERPOLATION,_INTERPOLATION_BILINEAR);

age(ledInstance(th(null),ght(null),_SMOOTH),0,

0,null);

if(null!=degree){

//设置⽔印旋转

(ans(degree),(double)th()/2,(double)ght()/2);

}

//⽔印图象的路径⽔印⼀般为gif或者png的,这样可设置透明度

//⽔印图象的路径⽔印⼀般为gif或者png的,这样可设置透明度

ImageIconimgIcon=newImageIcon(iconPath);

//得到Image对象。

Imageimg=ge();

floatalpha=0.5f;//透明度

posite(tance(_ATOP,alpha));

//表⽰⽔印图⽚的位置

age(img,150,300,null);

posite(tance(_OVER));

e();

os=newFileOutputStream(targerPath);

//⽣成图⽚

(buffImg,"JPG",os);

n("图⽚完成添加Icon印章。。。。。。");

}catch(Exceptione){

tackTrace();

}finally{

try{

if(null!=os)

();

}catch(Exceptione){

tackTrace();

}

}

}

/**

*@paramargs

*/

publicstaticvoidmain2(String[]args){

StringsrcImgPath="D:/pdf/";

StringiconPath="D:/pdf/";

StringtargerPath="D:/pdf/";

StringtargerPath2="D:/pdf/";

//给图⽚添加⽔印

ageByIcon(iconPath,srcImgPath,targerPath);

//给图⽚添加⽔印,⽔印旋转-45

ageByIcon(iconPath,srcImgPath,targerPath2,-45);

}

/**

*pdf设置⽂字⽔印

*

*@paraminputPath

*@paramoutPath

*@parammarkStr

*@throwsDocumentException

*@throwsIOException

*/

publicstaticvoidtPdfWatermark(StringinputPath,StringoutPath,StringmarkStr)

throwsDocumentException,IOException{

Filefile=newFile(outPath);

if(!()){

try{

NewFile();

}catch(IOExceptione){

tackTrace();

}

}

BufferedOutputStreambufferOut=null;

try{

bufferOut=newBufferedOutputStream(newFileOutputStream(file));

}catch(FileNotFoundExceptione1){

tackTrace();

}

PdfReaderreader=newPdfReader(inputPath);

PdfStamperstamper=newPdfStamper(reader,bufferOut);

inttotal=berOfPages()+1;

PdfContentBytecontent;

BaFontba=Font("STSong-Light","UniGB-UCS2-H",ED);

//BaFontba=Font("/data/tmis/uploads/file/font/,1",TY_H,

//ED);

PdfGStategs=newPdfGState();

for(inti=1;i

content=rContent(i);//在内容上⽅加⽔印

//content=erContent(i);//在内容下⽅加⽔印

lOpacity(0.2f);

//ate(gs);

ext();

//ColorFill(192,192,192);

ColorFill(220,20,60);

tAndSize(ba,50);

tMatrix(100,250);

xtAligned(_CENTER,markStr,250,400,55);

//xtAligned(_CENTER,

//"检测管理信息系统!",400,250,55);

ColorFill(0,0,0);

tAndSize(ba,8);

t();

}

();

try{

();

();

}catch(IOExceptione){

tackTrace();

}

}

publicstaticvoidmain3(String[]args){

try{

tPdfWatermark("D:/pdf/","D:/pdf/","我是⽔印");

}catch(DocumentExceptione){

tackTrace();

}catch(IOExceptione){

tackTrace();

}

}

/**

*word⽂字⽔印

*@paraminputPath

*@paramoutPath

*@parammarkStr

*/

publicstaticvoidtWordWaterMark(StringinputPath,StringoutPath,StringmarkStr,StringfileType)throwsException{

if("docx".equals(fileType)){

FileinputFile=newFile(inputPath);

XWPFDocumentdoc=null;

try{

doc=newXWPFDocument(newFileInputStream(inputFile));

}catch(FileNotFoundExceptione){

tackTrace();

}catch(IOExceptione){

tackTrace();

}

XWPFParagraphparagraph=Paragraph();

//XWPFRunrun=Run();

//t("TheBody:");

//createheader-footer

XWPFHeaderFooterPolicyheaderFooterPolicy=derFooterPolicy();

if(headerFooterPolicy==null)headerFooterPolicy=HeaderFooterPolicy();

//createdefaultWatermark-fillcolorblackandnotrotated

Watermark(markStr);

//getthedefaultheader

//Note:createWatermarkalsotsFIRSTandEVENheaders

//Note:createWatermarkalsotsFIRSTandEVENheaders

//butthiscodedoesnotupdatingthootherheaders

XWPFHeaderheader=der(T);

paragraph=agraphArray(0);

n(().getRArray(0));

n(().getRArray(0).getPictArray(0));

//ewherefillcolorandrotationist

ect[]xmlobjects=().getRArray(0).getPictArray(0).lectChildren(

("urn:schemas-microsoft-com:vml","shape"));

if(>0){

ectshape=(e)xmlobjects[0];

//tfillcolor

//lcolor("#d8d8d8");

lcolor("#CC00FF");

//trotation

le(le()+";rotation:315");

//n(ctshape);

}

Filefile=newFile(outPath);

if(!()){

try{

NewFile();

}catch(IOExceptione){

tackTrace();

}

}

try{

(newFileOutputStream(file));

();

}catch(FileNotFoundExceptione){

tackTrace();

}catch(IOExceptione){

tackTrace();

}

}elif("doc".equals(fileType)){

}

}

publicstaticvoidmain4(String[]args)throwsFileNotFoundException,IOException{

try{

tWordWaterMark("D:/pdf/","D:/pdf/","我是⽔印","docx");

}catch(Exceptione){

tackTrace();

}

}

/*

*为Excel打上⽔印⼯具函数请⾃⾏确保参数值,以保证⽔印图⽚之间不会覆盖。在计算⽔印的位置的时候,并没有考虑到单元格合并的情况,请注意

*

*@paramwb

*ExcelWorkbook

*@paramsheet

*需要打⽔印的Excel

*@paramwaterRemarkPath

*⽔印地址,classPath,⽬前只⽀持png格式的图⽚,

*因为⾮png格式的图⽚打到Excel上后可能会有图⽚变红的问题,且不容易做出透明效果。

*同时请注意传⼊的地址格式,应该为类似:""

*@paramstartXCol

*⽔印起始列

*@paramstartYRow

*⽔印起始⾏

*@parambetweenXCol

*⽔印横向之间间隔多少列

*@parambetweenYRow

*⽔印纵向之间间隔多少⾏

*@paramXCount

*横向共有⽔印多少个

*@paramYCount

*纵向共有⽔印多少个

*@paramwaterRemarkWidth

*⽔印图⽚宽度为多少列

*@paramwaterRemarkHeight

*⽔印图⽚⾼度为多少⾏

*⽔印图⽚⾼度为多少⾏

*@throwsIOException

*/

publicstaticvoidputWaterRemarkToExcel(Workbookwb,Sheetsheet,StringwaterRemarkPath,intstartXCol,

intstartYRow,intbetweenXCol,intbetweenYRow,intXCount,intYCount,intwaterRemarkWidth,

intwaterRemarkHeight)throwsIOException{

//校验传⼊的⽔印图⽚格式

if(!th("png")&&!th("PNG")){

thrownewRuntimeException("向Excel上⾯打印⽔印,⽬前⽀持png格式的图⽚。");

}

//加载图⽚

ByteArrayOutputStreambyteArrayOut=newByteArrayOutputStream();

InputStreamimageIn=newFileInputStream(waterRemarkPath);

//InputStreamimageIn=tThread().getContextClassLoader().getResourceAsStream(waterRemarkPath);

if(null==imageIn||ble()<1){

thrownewRuntimeException("向Excel上⾯打印⽔印,读取⽔印图⽚失败(1)。");

}

BufferedImagebufferImg=(imageIn);

if(null==bufferImg){

thrownewRuntimeException("向Excel上⾯打印⽔印,读取⽔印图⽚失败(2)。");

}

(bufferImg,"png",byteArrayOut);

//开始打⽔印

Drawingdrawing=DrawingPatriarch();

//按照共需打印多少⾏⽔印进⾏循环

for(intyCount=0;yCount

//按照每⾏需要打印多少个⽔印进⾏循环

for(intxCount=0;xCount

//创建⽔印图⽚位置

intxIndexInteger=startXCol+(xCount*waterRemarkWidth)+(xCount*betweenXCol);

intyIndexInteger=startYRow+(yCount*waterRemarkHeight)+(yCount*betweenYRow);

/*

*参数定义:第⼀个参数是(x轴的开始节点);第⼆个参数是(是y轴的开始节点);第三个参数是(是x轴的结束节点);

*第四个参数是(是y轴的结束节点);第五个参数是(是从Excel的第⼏列开始插⼊图⽚,从0开始计数);

*第六个参数是(是从excel的第⼏⾏开始插⼊图⽚,从0开始计数);第七个参数是(图⽚宽度,共多少列);

*第8个参数是(图⽚⾼度,共多少⾏);

*/

ClientAnchoranchor=Anchor(0,0,0,0,xIndexInteger,

yIndexInteger,xIndexInteger+waterRemarkWidth,yIndexInteger+waterRemarkHeight);

Picturepic=Picture(anchor,

ture(Array(),E_TYPE_PNG));

();

}

}

}

/**

*根据⽂字⽣成⽔印图⽚

*@paramcontent

*@parampath

*@throwsIOException

*/

publicstaticvoidcreateWaterMarkImage(Stringcontent,Stringpath)throwsIOException{

Integerwidth=300;

Integerheight=200;

BufferedImageimage=newBufferedImage(width,height,_INT_RGB);//获取bufferedImage对象

StringfontType="宋体";

IntegerfontStyle=;

IntegerfontSize=50;

Fontfont=newFont(fontType,fontStyle,fontSize);

Graphics2Dg2d=Graphics();//获取Graphics2d对象

image=iceConfiguration().createCompatibleImage(width,height,UCENT);

e();

g2d=Graphics();

//or(newColor(0,0,0,80));//设置字体颜⾊和透明度

or(newColor(255,180,0,80));//设置字体颜⾊和透明度

oke(newBasicStroke(1));//设置字体

t(font);//设置字体类型加粗⼤⼩

(ans(-10),(double)th()/2,(double)ght()/2);//设置倾斜度

FontRenderContextcontext=tRenderContext();

Rectangle2Dbounds=ingBounds(content,context);

doublex=(th())/2;

doubley=(ght())/2;

doubleascent=-();

doublebaY=y+ascent;

//写⼊⽔印⽂字原定⾼度过⼩,所以累计写⽔印,增加⾼度

ring(content,(int)x,(int)baY);

//设置透明度

posite(tance(_OVER));

//释放对象

e();

(image,"png",newFile(path));

}

/**

*excel设置⽔印

*@paraminputPath

*@paramoutPath

*@parammarkStr

*/

publicstaticvoidtExcelWaterMark(StringinputPath,StringoutPath,StringmarkStr)throwsException{

//读取excel⽂件

Workbookwb=null;

try{

wb=newXSSFWorkbook(newFileInputStream(inputPath));

}catch(FileNotFoundExceptione1){

tackTrace();

}catch(IOExceptione1){

tackTrace();

}

//设置⽔印图⽚路径

StringimgPath=_STATICFILE_PATH()+"/temp/"+"waterImage"+UUID().toString().replace("-","")+".png";

try{

createWaterMarkImage(markStr,imgPath);

}catch(IOExceptione1){

tackTrace();

}

//获取excelsheet个数

intsheets=berOfSheets();

//循环sheet给每个sheet添加⽔印

for(inti=0;i

Sheetsheet=etAt(i);

//excel加密只读

//tSheet(UUID().toString());

//获取excel实际所占⾏

//introw=stRowNum()+tRowNum();

introw=0;

//获取excel实际所占列

intcell=0;

/*

*if(null!=(stRowNum())){

*cell=(stRowNum()).getLastCellNum()+1;

*}

*/

//根据⾏与列计算实际所需多少⽔印

try{

putWaterRemarkToExcel(wb,sheet,imgPath,0,0,5,5,cell/5+1,row/5+1,0,0);

}catch(IOExceptione){

tackTrace();

}

}

ByteArrayOutputStreamos=newByteArrayOutputStream();

try{

(os);

();

}catch(IOExceptione){

tackTrace();

}

byte[]content=Array();

byte[]content=Array();

//Excel⽂件⽣成后存储的位置。

Filefile=newFile(outPath);

OutputStreamfos=null;

try{

fos=newFileOutputStream(file);

(content);

();

();

}catch(Exceptione){

tackTrace();

}

FileimageTempFile=newFile(imgPath);

if(()){

();

}

}

publicstaticvoidmain(String[]args)throwsIOException{

try{

tExcelWaterMark("D:/pdf/","D:/pdf/","我是⽔印");

}catch(Exceptione){

tackTrace();

}

}

//改变所有⽂本,不改变样式

publicstaticvoidtPPTWaterMark(Stringpath,Stringtargetpath,StringmarkStr,StringfileType)throwsIOException{

//设置⽔印图⽚路径

StringimgPath=_STATICFILE_PATH()+"/temp/"+"waterImage"+UUID().toString().replace("-","")+".png";

try{

createWaterMarkImage(markStr,imgPath);

}catch(IOExceptione1){

tackTrace();

}

if("pptx".equals(fileType)){

XMLSlideShowslideShow=newXMLSlideShow(newFileInputStream(path));

byte[]pictureData=Array(newFileInputStream(imgPath));

//byte[]pictureData=Array(newFileInputStream("E:"));

PictureDatapictureData1=ture(pictureData,);

for(XSLFSlideslide:des()){

XSLFPictureShapepictureShape=Picture(pictureData1);

hor(gle(50,300,100,100));

CTSlidectSlide=Object();

XmlObject[]allText=Path(

"declarenamespacea='/drawingml/2006/main'"+

".//a:t"

);

/*

*for(inti=0;i<;i++){

*if(allText[i]instanceofXmlString){

*XmlStringxmlString=(XmlString)allText[i];

*Stringtext=ingValue();

*if(text==null||(""))continue;

*if(status==1)ingValue(t(text));

*ingValue(t(text));

*}

*}

*/

}

FileOutputStreamout=newFileOutputStream(targetpath);

(out);

();

();

}elif("ppt".equals(fileType)){

/*

*HSLFSlideShowppt=newHSLFSlideShow();

*SlideShowppt=newSlideShow(newHSLFSlideShow("PPT测试.ppt"));

*SlideShowslideShow=new(newFileInputStream(path));

*byte[]pictureData=Array(newFileInputStream(imgPath));

*//byte[]pictureData=Array(newFileInputStream("E:"));

*PictureDatapictureData1=ture(pictureData,);

*for(XSLFSlideslide:des()){

*XSLFPictureShapepictureShape=Picture(pictureData1);

*XSLFPictureShapepictureShape=Picture(pictureData1);

*hor(gle(50,300,100,100));

*CTSlidectSlide=Object();

*XmlObject[]allText=Path(

*"declarenamespacea='/drawingml/2006/main'"+

*".//a:t"

*);

*}

*

*FileOutputStreamout=newFileOutputStream(targetpath);

*(out);

*();

*();

*/

}

}

publicstaticvoidmain5(String[]args){

try{

tPPTWaterMark("D:/pdf/","D:/pdf/","我是⽔印","ppt");

}catch(IOExceptione){

tackTrace();

}

}

}

本文发布于:2023-03-16 10:00:28,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/1678932029274645.html

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

本文word下载地址:ppt水印怎么加.doc

本文 PDF 下载地址:ppt水印怎么加.pdf

上一篇:酸辣汤面
下一篇:返回列表
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
  • ppt水印怎么加
    自制飞机模型-庶夫2023年3月16日发(作者:西关幼儿园)java实现给图⽚、pdf、office(docx、xlsx、pptx)添加⽔印最近接到⼀个需求,要把客户上传的各种格式的⽂件下载时都要打上⽔印。这些格式包括所有图⽚格式、pdf、各种版本的office⽂件。搜罗整理了⼀下,⽬前除了2003版的office还不⽀持⽔印外,基本满⾜需求。下⾯是新建的WaterMarkUtils,集成了图⽚、
  • 0℃酸辣汤面
  • 0℃外委
  • 0℃基本尺寸
  • 0℃郊县
  • 0℃我是白痴
  • 0℃自制印章
  • 0℃大肌群
  • 0℃南京中山路
  • 0℃守宫吃什么
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图