「java水印开源」java excel水印

博主:adminadmin 2023-03-19 08:03:07 485

本篇文章给大家谈谈java水印开源,以及java excel水印对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

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如何实现在web工程中用OpenOffice生成带有图片水印的pdf

需要itext2.1.5,

以下是对pdf加水印的代码,包括文字水印和图片水印

public int fileCopy(String srcPath, String destPath) {

FileOutputStream fos = null;

FileInputStream fis = null;

try {

fos = new FileOutputStream(destPath);

fis = new FileInputStream(srcPath);

byte[] buffer = new byte[1024];

int len = 0;

while ((len = fis.read(buffer)) 0) {

fos.write(buffer, 0, len);

}

return 1;

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

fis.close();

fos.flush();

fos.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

return 0;

}

/**

* 为pdf文件加文字水印

*

* @param srcPath

*            源文件路径

* @param destPath

*            目标文件路径

* @param waterText

*            水印文字

* @throws DocumentException

* @throws IOException

*/

public void wordWaterMark(String srcPath, String destPath, String waterText) throws DocumentException, IOException {

int result = fileCopy(srcPath, destPath);

if (result == 1) {

// 待加水印的文件

PdfReader reader = new PdfReader(destPath);

// 加完水印的文件

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(srcPath));

int total = reader.getNumberOfPages() + 1;

PdfContentByte content;

// 设置字体

BaseFont base = BaseFont.createFont(fontPath, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

// 水印文字

int j = waterText.length(); // 文字长度

char c = 0;

int high = 0;// 高度

// 循环对每页插入水印

for (int i = 1; i total; i++) {

// 水印的起始

high = 60;

content = stamper.getUnderContent(i);

PdfGState gs = new PdfGState();

gs.setFillOpacity(0.1f);// 设置透明度为0.2

content.setGState(gs);

// 开始

content.beginText();

// 设置颜色

// content.setColorFill(new Color());

// 设置字体及字号

content.setFontAndSize(base, 88);

// 设置起始位置

content.setTextMatrix(120, 333);

// 开始写入水印

for (int k = 0; k j; k++) {

content.setTextRise(high);

c = waterText.charAt(k);

content.showText(c + "");

high += 20;

}

content.endText();

}

stamper.close();

System.out.println("添加成功++++++++++++++++++++++++++++++++++++++++++");

} else {

System.out.println("复制pdf失败====================");

}

}

public void picWaterMark(String srcPath, String destPath, String imageFilePath)

throws DocumentException, IOException {

int result = fileCopy(srcPath, destPath);

if (result == 1) {

// 待加水印的文件

PdfReader reader = new PdfReader(destPath);

// 加完水印的文件

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(srcPath));

Image img = Image.getInstance(imageFilePath);

img.setAbsolutePosition(50, 400);// 坐标

img.setRotation(20);// 旋转 弧度

img.setRotationDegrees(45);// 旋转 角度

// image.scaleAbsolute(200,100);//自定义大小

img.scalePercent(50);// 依照比例缩放

int pageSize = reader.getNumberOfPages();

for (int i = 1; i = pageSize; i++) {

PdfContentByte under = stamper.getUnderContent(i);

under.addImage(img);

PdfGState gs = new PdfGState();

gs.setFillOpacity(0.2f);// 设置透明度为0.2

under.setGState(gs);

}

stamper.close();// 关闭

System.out.println("添加成功++++++++++++++++++++++++++++++++++++++++++");

} else {

System.out.println("复制pdf失败====================");

}

}

linux下转pdf可以用libreoffice,需要安装,这个是免费的,具体代码如下:

String command = "libreoffice5.0 --invisible --convert-to pdf:writer_pdf_Export --outdir " + destFilepath

+ " " + source;

try {

p = Runtime.getRuntime().exec(command);

p.waitFor();

} catch (InterruptedException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

java怎么给word文档加水印

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

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 )

}

}

有哪些可以给pdf加水印,java第三方开源类库

开源的有POI,要加水印的话可以自行搜索,有不少教程的。另一个就是国产的Free Spire.PDF for Java,这款是可以任意商用的无水印版的国产API, 加水印的代码如下:

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, "内部使用");

//保存文档

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 excel水印的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。