下载文档加水印 Java 实现指南

1. 引言

在实际开发中,我们常常需要实现下载文档并加上水印的功能,以确保文档的版权和安全性。本文将指导你如何使用 Java 实现这个功能。在开始之前,确保你已经掌握 Java 编程基础知识。

2. 实现流程

首先,我们来看一下整个实现的流程。下面的表格展示了具体的步骤:

步骤 动作
1 下载文档
2 添加水印
3 保存加水印后的文档

接下来,我们将逐步讲解每一步需要做什么,以及需要使用的代码。

3. 下载文档

在 Java 中,我们可以使用 java.net 包中的 URLURLConnection 类来实现文件的下载。下面的代码演示了如何下载一个文档:

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public class DocumentDownloader {
    public static void downloadDocument(String url, String savePath) throws Exception {
        URL documentUrl = new URL(url);
        URLConnection connection = documentUrl.openConnection();
        InputStream inputStream = connection.getInputStream();
        BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
        FileOutputStream fileOutputStream = new FileOutputStream(savePath);

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = bufferedInputStream.read(buffer, 0, 1024)) != -1) {
            fileOutputStream.write(buffer, 0, bytesRead);
        }

        fileOutputStream.close();
        bufferedInputStream.close();
        inputStream.close();
    }
}

上述代码中,我们首先创建了一个 URL 对象,用于表示下载的文档的链接。然后,我们打开了一个 URLConnection 连接,并通过 getInputStream() 方法获取到文档的输入流。接着,我们使用缓冲输入流和文件输出流将文档写入到本地的指定路径。

4. 添加水印

在 Java 中,我们可以使用 Apache PDFBox 库来实现对 PDF 文档的操作。下面的代码演示了如何在 PDF 文档中添加水印:

import java.io.File;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class WatermarkAdder {
    public static void addWatermark(String documentPath, String watermarkText, String outputPath) throws Exception {
        PDDocument document = PDDocument.load(new File(documentPath));
        int pageCount = document.getNumberOfPages();

        for (int i = 0; i < pageCount; i++) {
            PDPage page = document.getPage(i);
            PDRectangle pageSize = page.getMediaBox();
            float pageWidth = pageSize.getWidth();
            float pageHeight = pageSize.getHeight();

            PDPageContentStream contentStream = new PDPageContentStream(document, page, PDPageContentStream.AppendMode.APPEND, true, true);
            contentStream.setFont(PDType1Font.HELVETICA_BOLD, 32);
            contentStream.setLeading(40);

            contentStream.beginText();
            contentStream.newLineAtOffset(100, pageHeight - 100);
            contentStream.showText(watermarkText);
            contentStream.endText();
            contentStream.close();
        }

        document.save(outputPath);
        document.close();
    }
}

上述代码中,我们首先加载了要添加水印的 PDF 文档。然后,我们遍历文档的每一页,获取页面的宽度和高度。接下来,我们创建一个 PDPageContentStream 对象,用于操作页面的内容流。在内容流中,我们设置了水印的字体和大小,并使用 showText() 方法添加水印文字。最后,我们保存添加水印后的文档并关闭相关资源。

5. 保存加水印后的文档

最后一步是将加上水印的文档保存到文件系统中。下面的代码演示了如何保存文档:

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class DocumentSaver {
    public static void saveDocument(byte[] documentBytes, String savePath) throws Exception {
        File file = new File