资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

图片添加水印java代码 图片添加水印java代码是什么

java怎么给word文档加水印

可以使用Free Spire.Doc for Java在word文档中添加文本水印或图片水印。Free Spire.Doc for Java下载链接:网页链接

汤阴网站制作公司哪家好,找成都创新互联!从网页设计、网站建设、微信开发、APP开发、响应式网站设计等网站项目制作,到程序开发,运营维护。成都创新互联成立于2013年到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联

1.添加文本水印——代码如下:

import com.spire.doc.*;

import com.spire.doc.documents.WatermarkLayout;

import java.awt.*;

public class WordTextWatermark {

public static void main(String[] args) {

Document document = new Document();

document.loadFromFile("Sample.docx");

insertTextWatermark(document.getSections().get(0));

}

private static void insertTextWatermark(Section section) {

TextWatermark txtWatermark = new TextWatermark();

txtWatermark.setText("内部使用");

txtWatermark.setFontSize(40);

txtWatermark.setColor(Color.red);

txtWatermark.setLayout(WatermarkLayout.Diagonal);

section.getDocument().setWatermark(txtWatermark);

}

}

2.添加图片水印——代码如下:

import com.spire.doc.*;

public class WordImageWatermark {

public static void main(String[] args)  throws Exception{

Document document = new Document();

document.loadFromFile("Sample.docx");

PictureWatermark picture = new PictureWatermark();

picture.setPicture("logo.png");

picture.setScaling(5);

picture.isWashout(false);

document.setWatermark(picture);

document.saveToFile("out/result2.docx",FileFormat.Docx )

}

}

java中打印的pdf怎么添加图片水印

添加水印可以参考使用控件来添加的方法,如下:

1. 添加单个图片水印效果:

import com.spire.pdf.*;

import java.awt.geom.Rectangle2D;

public class watermark {

public static void main(String[] args) {

//加载PDF文档

PdfDocument doc = new PdfDocument();

doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

//获取第一页

PdfPageBase page = doc.getPages().get(0);

//设置背景图片

page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png");

//设置背景区域

Rectangle2D.Float rect = new Rectangle2D.Float();

rect.setRect(280, 300, 150, 150);

page.setBackgroundRegion(rect);

//保存文档

doc.saveToFile("output/imageWaterMark.pdf");

doc.close();

}

}

2. 添加平铺图片水印效果

import com.spire.pdf.*;

import com.spire.pdf.graphics.PdfImage;

import com.spire.pdf.graphics.PdfTilingBrush;

import java.awt.*;

import java.awt.geom.Dimension2D;

import java.awt.geom.Rectangle2D;

public class AddBackground {

public static void main(String[] args) {

//创建PdfDocument对象,并加载PDF测试文档

PdfDocument pdf = new PdfDocument();

pdf.loadFromFile("test.pdf");

//遍历文档每一页,加载图片,并设置成平铺背景(水印)

for (int i = 0; i pdf.getPages().getCount();i++)

{

PdfPageBase page = pdf.getPages().get(i);

Dimension2D dimension2D = new Dimension();

dimension2D.setSize(page.getCanvas().getSize().getWidth()/4, page.getCanvas().getSize().getHeight()/3);

PdfTilingBrush brush = new PdfTilingBrush(dimension2D);

brush.getGraphics().setTransparency(0.2f);

 brush.getGraphics().translateTransform(brush.getSize().getWidth()/10,brush.getSize().getHeight()/10);

brush.getGraphics().rotateTransform(30);

PdfImage image = PdfImage.fromImage("logo.png");

brush.getGraphics().drawImage(image,brush.getSize().getWidth()-image.getWidth()/2,(brush.getSize().getHeight())/2);

Rectangle2D rectangle2D = new Rectangle2D.Float();

rectangle2D.setFrame(new Point(0,0),page.getCanvas().getClientSize());

page.getCanvas().drawRectangle(brush,rectangle2D);

}

//保存文档

pdf.saveToFile("SetTiledBackground.pdf");

pdf.dispose();

}

}

注:这里使用的是free Spire.Pdf.jar(以上代码参考自文章1、文章2)

Java图片处理 文字水印 图片水印 缩放 补白

package util;

import java awt AlphaComposite;

import java awt Color;

import java awt Font;

import java awt Graphics D;

import java awt Image;

import java awt geom AffineTransform;

import java awt image AffineTransformOp;

import java awt image BufferedImage;

import java io File;

import java io IOException;

import javax imageio ImageIO;

/**

* @author Eric Xu

*

*/

public final class ImageUtils {

/**

* 图片水印

* @param pressImg 水印图片

* @param targetImg 目标图片

* @param x 修正值 默认在中间

* @param y 修正值 默认在中间

* @param alpha 透明度

*/

public final static void pressImage(String pressImg String targetImg int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIO read(img);

int wideth = src getWidth(null);

int height = src getHeight(null);

BufferedImage image = new BufferedImage(wideth height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g drawImage(src wideth height null);

//水印文件

Image src_biao = ImageIO read(new File(pressImg));

int wideth_biao = src_biao getWidth(null);

int height_biao = src_biao getHeight(null);

g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));

g drawImage(src_biao (wideth wideth_biao) / (height height_biao) / wideth_biao height_biao null);

//水印文件结束

g dispose();

ImageIO write((BufferedImage) image jpg img);

} catch (Exception e) {

e printStackTrace();

}

}

/**

* 文字水印

* @param pressText 水印文字

* @param targetImg 目标图片

* @param fontName 字体名称

* @param fontStyle 字体样式

* @param color 字体颜色

* @param fontSize 字体大小

* @param x 修正值

* @param y 修正值

* @param alpha 透明度

*/

public static void pressText(String pressText String targetImg String fontName int fontStyle Color color int fontSize int x int y float alpha) {

try {

File img = new File(targetImg);

Image src = ImageIO read(img);

int width = src getWidth(null);

int height = src getHeight(null);

BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g drawImage(src width height null);

g setColor(color);

g setFont(new Font(fontName fontStyle fontSize));

g setComposite(AlphaComposite getInstance(AlphaComposite SRC_ATOP alpha));

g drawString(pressText (width (getLength(pressText) * fontSize)) / + x (height fontSize) / + y);

g dispose();

ImageIO write((BufferedImage) image jpg img);

} catch (Exception e) {

e printStackTrace();

}

}

/**

* 缩放

* @param filePath 图片路径

* @param height 高度

* @param width 宽度

* @param bb 比例不对时是否需要补白

*/

public static void resize(String filePath int height int width boolean bb) {

try {

double ratio = ; //缩放比例

File f = new File(filePath);

BufferedImage bi = ImageIO read(f);

Image itemp = bi getScaledInstance(width height bi SCALE_SMOOTH);

//计算比例

if ((bi getHeight() height) || (bi getWidth() width)) {

if (bi getHeight() bi getWidth()) {

ratio = (new Integer(height)) doubleValue() / bi getHeight();

} else {

ratio = (new Integer(width)) doubleValue() / bi getWidth();

}

AffineTransformOp op = new AffineTransformOp(AffineTransform getScaleInstance(ratio ratio) null);

itemp = op filter(bi null);

}

if (bb) {

BufferedImage image = new BufferedImage(width height BufferedImage TYPE_INT_RGB);

Graphics D g = image createGraphics();

g setColor(Color white);

g fillRect( width height);

if (width == itemp getWidth(null))

g drawImage(itemp (height itemp getHeight(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);

else

g drawImage(itemp (width itemp getWidth(null)) / itemp getWidth(null) itemp getHeight(null) Color white null);

g dispose();

itemp = image;

}

ImageIO write((BufferedImage) itemp jpg f);

} catch (IOException e) {

e printStackTrace();

}

}

public static void main(String[] args) throws IOException {

pressImage( D:\\shuiyin png D:\\test jpg f);

pressText( 我是文字水印 D:\\test jpg 微软雅黑 Color white f);

resize( D:\\test jpg true);

}

public static int getLength(String text) {

int length = ;

for (int i = ; i text length(); i++) {

if (new String(text charAt(i) + ) getBytes() length ) {

length += ;

} else {

length += ;

}

}

return length / ;

}

lishixinzhi/Article/program/Java/hx/201311/26874

java 如何给pdf文件加水印

可以使用Spire.PDF for Java通过Java来添加水印。

首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。您可以从这个链接下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。

repositories

repository

idcom.e-iceblue/id

url;/url

/repository/repositoriesdependencies

dependency

groupIde-iceblue/groupId

artifactIdspire.pdf/artifactId

version5.3.1/version

/dependency/dependencies

1.添加图片水印

代码如下:

import com.spire.pdf.*;

import java.awt.geom.Rectangle2D;

public class watermark {

public static void main(String[] args) {

//加载PDF文档

PdfDocument doc = new PdfDocument();

doc.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

//获取第一页

PdfPageBase page = doc.getPages().get(0);  

//设置背景图片

page.setBackgroundImage("C:\\Users\\Administrator\\Desktop\\logo.png");

//设置背景区域

Rectangle2D.Float rect = new Rectangle2D.Float();

rect.setRect(280, 300, 150, 150);

page.setBackgroundRegion(rect);

//保存文档

doc.saveToFile("output/imageWaterMark.pdf");

doc.close();

}

}

2.添加文本水印

代码如下:

import com.spire.pdf.*;

import com.spire.pdf.graphics.*;

import java.awt.*;

import java.awt.geom.*;

public class Textwatermark {

public static void main(String[] args) {

//创建PdfDocument对象

PdfDocument pdf = new PdfDocument();

//加载示例文档

pdf.loadFromFile("C:\\Users\\Administrator\\Desktop\\Sample.pdf");

//获取第一页

PdfPageBase page = pdf.getPages().get(0);

//调用insertWatermark方法插入文本水印

insertWatermark(page, "E-ICEBLUE");

//保存文档

pdf.saveToFile("out/textWaterMark.pdf");

}

static void insertWatermark(PdfPageBase page, String watermark) {

Dimension2D dimension2D = new Dimension();

dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 2, page.getCanvas().getClientSize().getHeight() / 3);

PdfTilingBrush brush = new PdfTilingBrush(dimension2D);

brush.getGraphics().setTransparency(0.3F);

brush.getGraphics().save();

brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 2, (float) brush.getSize().getHeight() / 2);

brush.getGraphics().rotateTransform(-45);

brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 24), PdfBrushes.getViolet(), 0, 0, new PdfStringFormat(PdfTextAlignment.Center));

brush.getGraphics().restore();

brush.getGraphics().setTransparency(1);

Rectangle2D loRect = new Rectangle2D.Float();

loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());

page.getCanvas().drawRectangle(brush, loRect);

}

}

希望对您有帮助。


网站栏目:图片添加水印java代码 图片添加水印java代码是什么
文章网址:http://cdkjz.cn/article/hhjieo.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220