100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > Java 水印(Word Excel PPT PDF JPG)等多种文档

Java 水印(Word Excel PPT PDF JPG)等多种文档

时间:2022-10-19 02:32:09

相关推荐

Java 水印(Word Excel PPT PDF JPG)等多种文档

本文主要使用Free spire.Office for java 对Word,PDF,PPT文档加水印

/download/qq_39220268/12470571免费版的spire

可以以去free spire 官网下载https://www.e-/Download/office-for-java-free.html,付费版功能更强

PPT文档,代码如下

import com.spire.presentation.*;import com.spire.presentation.drawing.FillFormatType;import java.awt.*;import java.awt.geom.Rectangle2D;public class PptWaterMark {public void addWaterMark(String srcWordPath,String tarWordPath) throws Exception {//创建一个PPT文档实例并加载目标文档Presentation presentation = new Presentation();presentation.loadFromFile(srcWordPath);//添加文本水印InsertTextWatermark(presentation);presentation.saveToFile(tarWordPath, FileFormat.PPTX_);}private static void InsertTextWatermark(Presentation presentation1) throws Exception{//设置文本水印的宽和高int width= 400;int height= 300;//定义一个长方形区域Rectangle2D.Double rect = new Rectangle2D.Double((presentation1.getSlideSize().getSize().getWidth() - width) / 2,(presentation1.getSlideSize().getSize().getHeight() - height) / 2, width, height);//遍历每一个幻灯片for(int i = 0; i< presentation1.getSlides().getCount(); i++){//添加一个shape到定义区域IAutoShape shape = presentation1.getSlides().get(i).getShapes().appendShape(ShapeType.RECTANGLE, rect);//设置shape样式shape.getFill().setFillType(FillFormatType.NONE);shape.getShapeStyle().getLineColor().setColor(Color.white);shape.setRotation(-45);shape.getLocking().setSelectionProtection(true);shape.getLine().setFillType(FillFormatType.NONE);//添加文本到shapeshape.getTextFrame().setText("PPT测试水印");PortionEx textRange = shape.getTextFrame().getTextRange();//设置文本水印样式textRange.getFill().setFillType(FillFormatType.SOLID);textRange.getFill().getSolidColor().setColor(Color.red);textRange.setFontHeight(50);}System.out.println("PPT水印添加完成!");}}

Word文档,代码如下

import java.awt.Color;import com.spire.doc.Document;import com.spire.doc.FileFormat;import com.spire.doc.Section;import com.spire.doc.TextWatermark;import com.spire.doc.documents.WatermarkLayout;public class WordWaterMark {public void addWaterMark(String srcWordPath,String tarWordPath){//创建一个Word文档实例并加载目标文档Document document = new Document();document.loadFromFile(srcWordPath);//插入文本水印InsertTextWatermark(document.getSections().get(0));//保存文档document.saveToFile(tarWordPath,FileFormat.Docx );}private static void InsertTextWatermark(Section section){TextWatermark txtWatermark = new TextWatermark();txtWatermark.setText("Word测试水印");txtWatermark.setFontSize(48);txtWatermark.setColor(Color.red);txtWatermark.setLayout(WatermarkLayout.Diagonal);section.getDocument().setWatermark(txtWatermark);System.out.println("WORD水印添加完成!");}}

PDF文档,代码如下

EXCEL文档,则采用poi工具进行操作。

具体代码放在链接里了

/download/qq_39220268/12474999

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。