首页 新闻 会员 周边

关于下载pdf文件加水印问题

0
[已解决问题] 解决于 2018-11-23 10:44

/**
* 下载文件
*/
@ApiOperation(value = "下载文件")
@GetMapping("getFile")
public GridFSDBFile getFile(String filePath, String dataName, HttpServletResponse response) {
try {
GridFSDBFile gridFSDBFile = mongdbService.getFile(filePath);
if (gridFSDBFile != null) {
OutputStream out = response.getOutputStream();
response.setContentType("application/force-download");
response.setHeader("Content-Disposition", "attachment; filename=" + dataName);
gridFSDBFile.writeTo(out);
out.flush();
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
怎么在这个方法基础上加水印

刘凌枫羽的主页 刘凌枫羽 | 菜鸟二级 | 园豆:355
提问于:2018-11-06 14:02
< >
分享
最佳答案
0

尝试使用iText看看吧。

/**
     * 
     * 【功能描述:添加图片和文字水印】 【功能详细描述:功能详细描述】
     * @param srcFile 待加水印文件
     * @param destFile 加水印后存放地址
     * @param text 加水印的文本内容
     * @param textWidth 文字横坐标
     * @param textHeight 文字纵坐标
     * @throws Exception
     */
    public void addWaterMark(String srcFile, String destFile, String text,
            int textWidth, int textHeight) throws Exception
    {
        // 待加水印的文件
        PdfReader reader = new PdfReader(srcFile);
        // 加完水印的文件
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
                destFile));
        int total = reader.getNumberOfPages() + 1;
        PdfContentByte content;
        // 设置字体
        BaseFont font = BaseFont.createFont();
        // 循环对每页插入水印
        for (int i = 1; i < total; i++)
        {
            // 水印的起始
            content = stamper.getUnderContent(i);
            // 开始
            content.beginText();
            // 设置颜色 默认为蓝色
            content.setColorFill(BaseColor.BLUE);
            // content.setColorFill(Color.GRAY);
            // 设置字体及字号
            content.setFontAndSize(font, 38);
            // 设置起始位置
            // content.setTextMatrix(400, 880);
            content.setTextMatrix(textWidth, textHeight);
            // 开始写入水印
            content.showTextAligned(Element.ALIGN_LEFT, text, textWidth,
                    textHeight, 45);
            content.endText();
        }
        stamper.close();
    }

参考链接:https://www.zhihu.com/question/264003636/answer/279707367

奖励园豆:5
芽衣 | 菜鸟二级 |园豆:384 | 2018-11-08 10:16
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册