java pdf添加图片水印图片_如何使用Java为PDF中的图像添加水印。(How to add water marks to the images in a PDF using Java.)...

如何使用Java为PDF中的图像添加水印。(How to add water marks to the images in a PDF using Java.)

问题描述 (Problem Description)

如何使用Java为PDF中的图像添加水印。

解决方案 (Solution)

以下是使用Java在PDF中为图像添加水印的程序。import com.itextpdf.io.image.ImageDataFactory;

import com.itextpdf.kernel.color.DeviceGray;

import com.itextpdf.kernel.geom.Rectangle;

import com.itextpdf.kernel.pdf.PdfDocument;

import com.itextpdf.kernel.pdf.PdfWriter;

import com.itextpdf.kernel.pdf.xobject.PdfFormXObject;

import com.itextpdf.layout.Canvas;

import com.itextpdf.layout.Document;

import com.itextpdf.layout.element.Image;

import com.itextpdf.layout.property.TextAlignment;

public class AddingToImagesInPDF {

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

String file = "C:/EXAMPLES/itextExamples/addingImageToPDF.pdf";

//Creating a PdfDocument object

PdfDocument pdfDoc = new PdfDocument(new PdfWriter(file));

//Creating a Document object

Document doc = new Document(pdfDoc);

//Creating an Image

Image image = new Image(ImageDataFactory.create(

"C:/EXAMPLES/itextExamples/logo.jpg"));

image.scaleToFit(400, 700);

//Creating template

PdfFormXObject template = new PdfFormXObject(new Rectangle(

image.getImageScaledWidth(), image.getImageScaledHeight()));

Canvas canvas = new Canvas(template, pdfDoc).add(image);

String watermark = "Welcome to IoWiki";

canvas.setFontColor(DeviceGray.LIGHT_GRAY)

.showTextAligned(watermark, 100, 360, TextAlignment.CENTER);

//Adding template to document

Image image1 = new Image(template);

doc.add(image1);

//Closing the document

doc.close();

System.out.println("Watermark added successfully..");

}

}

输入 (Input)

a68b432eab26ebcec83275061c398d87.png

输出 (Output)

6c5bf7d35cd4d200843659998b3cc13a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值