java 如何给pdf文件加水印

java 如何给pdf文件加水印,第1张

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

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

<repositories>

<repository>

<id>com.e-iceblue</id>

<url>https://repo.e-iceblue.cn/repository/maven-public/</url>

</repository></repositories><dependencies>

<dependency>

<groupId>e-iceblue</groupId>

<artifactId>spire.pdf</artifactId>

<version>5.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)

}

}

希望对您有帮助。

需要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 = 1i <totali++) {

// 水印的起始

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 = 0k <jk++) {

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 = 1i <= pageSizei++) {

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()

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/bake/11833946.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-19
下一篇 2023-05-19

发表评论

登录后才能评论

评论列表(0条)

保存