给PDF文件添加图片水印

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * <dependency>
 *     <groupId>com.itextpdf</groupId>
 *     <artifactId>itextpdf</artifactId>
 *     <version>5.5.13.1</version>
 * </dependency>
 * 需求:用户输入一个源pdf文件,一个用做水印的图片,程序负责生成一个新的pdf文件,内有图片水印,且图片旋转一个度数。
 */
public class TransparentWatermark34 {

    public static final String SRC = "D:\\Dump\\TestDB\\watermark\\src\\main\\resources\\hero.pdf";
    public static final String DEST = "D:\\Dump\\TestDB\\watermark\\src\\main\\resources\\hero_watermarked.pdf";
    public static final String IMG = "D:\\img3.png";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new TransparentWatermark34().manipulatePdf(SRC, DEST);
    }

    public void manipulatePdf(String src, String dest) {
        PdfReader reader = null;
        PdfStamper stamper = null;

        try {
            reader = new PdfReader(src);
            int pdfNumberOfPages = reader.getNumberOfPages();
            stamper = new PdfStamper(reader, new FileOutputStream(dest));
            stamper.setRotateContents(false);

            // 作为水印的图片
            Image img = Image.getInstance(IMG);
            // 设置绝对坐标
            img.setAbsolutePosition(170, 380);
            // 设置旋转弧度
            img.setRotation(30);
            // 设置旋转角度
            img.setRotationDegrees(45);
            // 设置缩放比例
            img.scalePercent(60);

            PdfGState gs1 = new PdfGState();
            // 填充不透明度
            gs1.setFillOpacity(0.5f);

            PdfContentByte over;
            for (int i = 1; i <= pdfNumberOfPages; i++) {
                over = stamper.getOverContent(i);
                over.saveState();
                over.setGState(gs1);
                over.addImage(img);
                over.restoreState();
            }
        } catch (IOException ioe) {
            ioe.printStackTrace();
        } catch (DocumentException de) {
            de.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (stamper != null) {
                try {
                    stamper.close();
                } catch (Exception e) {
                    // 空
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (Exception e) {
                    // 空
                }
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现Python中PDF图片水印的功能,可以使用PyMuPDF库和Pillow库。具体步骤如下: 1. 安装PyMuPDF和Pillow库,可以使用pip命令进行安装: ``` pip install PyMuPDF pip install Pillow ``` 2. 导入所需的库: ```python import fitz from PIL import Image, ImageDraw, ImageFont ``` 3. 使用fitz库打开PDF文件,将每一页转换为图片,并保存到本地: ```python pdf_path = 'path/to/pdf/file.pdf' doc = fitz.open(pdf_path) for i in range(doc.page_count): page = doc[i] pix = page.getPixmap() img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) img_path = f'page_{i+1}.jpg' img.save(img_path) ``` 4. 加载水印图片,并使用Pillow库将水印图片绘制到每一页图片上: ```python watermark_path = 'path/to/watermark/image.png' watermark = Image.open(watermark_path) for i in range(doc.page_count): img_path = f'page_{i+1}.jpg' img = Image.open(img_path) draw = ImageDraw.Draw(img) draw.bitmap((0, 0), watermark, fill=None) img.save(img_path) ``` 5. 关闭PDF文件: ```python doc.close() ``` 完整代码示例: ```python import fitz from PIL import Image, ImageDraw, ImageFont pdf_path = 'path/to/pdf/file.pdf' watermark_path = 'path/to/watermark/image.png' doc = fitz.open(pdf_path) for i in range(doc.page_count): page = doc[i] pix = page.getPixmap() img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples) img_path = f'page_{i+1}.jpg' img.save(img_path) watermark = Image.open(watermark_path) for i in range(doc.page_count): img_path = f'page_{i+1}.jpg' img = Image.open(img_path) draw = ImageDraw.Draw(img) draw.bitmap((0, 0), watermark, fill=None) img.save(img_path) doc.close() ``` 注意事项: 1. 水印图片PDF文件的路径需要根据实际情况进行修改。 2. 水印图片需要是透明背景的PNG格式图片,否则覆盖原图内容。 3. 在应用水印时,可以自定义水印的位置和透明度,以达到更好的效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值