/**
* pdf 加水印
*
* @return
*/
public byte[] pdfAddWaterMark(byte[] byes) {

String fileName = UUID.randomUUID().toString() + ".pdf";
String courseFile = "";
try {
// 第二种:获取项目路径 D:\git\daotie\daotie
//生成临时文件 , 读取完删除
File directory = new File("");// 参数为空
courseFile = directory.getCanonicalPath() + "/";
} catch (IOException e) {
e.printStackTrace();
}
byte[] returnBytes = null;
// 待加水印的文件
PdfReader reader = null;
PdfStamper stamper = null;
// ByteArrayOutputStream baos = null;
FileOutputStream os = null;
try {
reader = new PdfReader(byes);
// 加完水印的文件
// baos = new ByteArrayOutputStream();
// stamper = new PdfStamper(reader, baos);
// 加完水印的文件
os = new FileOutputStream(courseFile + fileName);
stamper = new PdfStamper(reader, os);

int total = reader.getNumberOfPages() + 1;
PdfContentByte content;
// BaseFont font = BaseFont.createFont();
BaseFont basefont = BaseFont.createFont("/static/fonts/SIMYOU.TTF", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
//这里的字体设置比较关键,这个设置是支持中文的写法
/*BaseFont base = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体*/

/*//设置透明度
PdfGState gs = new PdfGState();
gs.setFillOpacity(1f);
gs.setStrokeOpacity(1f);*/

PdfContentByte under;
com.itextpdf.text.Rectangle pageRect = null;

// 循环对每页插入水印
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().getPageSizeWithRotation(i);
// 计算水印X,Y坐标
float x = (float) (pageRect.getWidth() / 1.98);
float y = (float) (pageRect.getHeight() / 2.8);
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(1.f);
under.setGState(gs);
under.restoreState();
under.beginText();
under.setFontAndSize(basefont, pageRect.getHeight() / 17);
under.setColorFill(BaseColor.RED);

// 水印文字成45度角倾斜
System.out.println("width" + pageRect.getWidth());
System.out.println("height" + pageRect.getHeight());
System.out.println("x" + x);
System.out.println("y" + y);
under.showTextAligned(Element.ALIGN_CENTER, "图片仅供预览,不可用于商业用途", x, y, 45);
// 添加水印文字
under.endText();
under.setLineWidth(1f);
under.stroke();
}
// returnBytes = baos.toByteArray();
} catch (IOException e) {
e.printStackTrace();
} catch (DocumentException e) {
e.printStackTrace();
} finally {
try {
stamper.close();
if (os != null) {
os.close();
}
if (reader != null) {
reader.close();
}
} catch (DocumentException e) {
e.printStackTrace(http://www.amjmh.com/v/BIBRGZ_558768/);
} catch (IOException e) {
e.printStackTrace();
}
}
————————————————

/static/fonts/SIMYOU.TTF’ 字体的更多相关文章

  1. [转]TrueType(TTF)字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪)

    原文入口: TTF字体文件裁剪(支持简体中文,繁体中文TTF字体裁剪) 对于TrueType(TTF)字体格式的介绍可以看: https://www.cnblogs.com/slysky/p/1131 ...

  2. Android自定义TTF字体

    前言: 在Android Design中一个设计手册.在设计手册中有常用的UI图标,图标大小规范等. 其中,有一个TTF字体,以前感觉没什么用.但是我在学习时,常看到有许多开发者使用Google 提供 ...

  3. 【转】cocos2d-x使用第三方的TTF字体库

    步骤一:找一个ttf字体库 步骤二:找到这个ttf字体库的真实名称 打开你的应用 "字体册"(MAC OS系统下),如下图操作): 找到了字体库真实名称,那么修改将其真名作为为此新 ...

  4. Cocos2d-x教程(28)-ttf 字体库的使用

    欢迎增加 Cocos2d-x 交流群: 193411763 转载请注明原文出处:http://blog.csdn.net/u012945598/article/details/37650843 通常为 ...

  5. iOS上使用自己定义ttf字体

    项目中想使用第三方的字体,在stackoverflow上查询解决的方法,也折腾一会,加入成功,示比例如以下: 1.将xx.ttf字体库增加project里面 2.在project的xx-Info.pl ...

  6. TTF字体基本知识及其在QT中的应用

    字体类型 以Windows为例,有4种字体技术: Raster:光栅型,就是用位图来绘制字形(glyph),每个字都以位图形式保存 Vector:矢量型,就是用一系列直线的结束点来表示字形 TrueT ...

  7. iOS上使用自定义ttf字体

    本文转载至 http://blog.csdn.net/allison162004/article/details/38777777 项目中想使用第三方的字体,在stackoverflow上查询解决办法 ...

  8. php GD 和图像处理函数, 用 STHUPO.TTF 字体向图像写入文本

    php GD 和图像处理函数,   用  STHUPO.TTF 字体向图像写入文本 注意: 01)   imagettftext() 这个函数不能使用相对路径, 要想使用相对路径要先使用  puten ...

  9. WPF解析TTF 字体

    偶遇需要自己解析 TTF 字体并显示,此做... using System; using System.Collections.Generic; using System.Drawing.Text; ...

随机推荐

  1. 引入DDT

    一.大致介绍: DDT-Data Driven Test 是Python的第三方库,提供了创建数据驱动的测试,在线安装为:pip install ddt @data 表示元祖的列表数据 @unpack ...

  2. C语言经典100例(51-100)

    [程序51] 题目:学习使用按位与 & . 分析:0&0=0; 0&1=0; 1&0=0; 1&1=1 #include "stdio.h" ...

  3. leecode刷题(28)-- 二叉树的前序遍历

    leecode刷题(28)-- 二叉树的前序遍历 二叉树的前序遍历 给定一个二叉树,返回它的 前序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,2,3] 思路 ...

  4. Nginx(高并发)

    Nginx(engine x)高性能和反向代理的web服务器反向代理:保护客户资源,只要是http协议都可以Web服务器:IIS 阿帕奇 NginxNginx可以作为负载均衡(NLB只支持Http)我 ...

  5. java 周期时期计算

    package org.apple.date; import java.text.SimpleDateFormat; import java.util.Calendar; import java.ut ...

  6. Redis总结1

    一.Redis安装(Linux) 1.在官网上下载Linux版本的Redis(链接https://redis.io/download) 2.在Linux的/usr/local中创建Redis文件夹mk ...

  7. Java注解的继承

    注解继承的说明 1.首先要想Annotation能被继承,需要在注解定义的时候加上@Inherited,并且如果要被反射应用的话,就需要还有个事@Retention(RetentionPolicy.R ...

  8. 12、Nginx代理缓存服务

    通常情况下缓存是用来减少后端压力, 将压力尽可能的往前推, 减少后端压力,提高网站并发延时 1.缓存常见类型 服务端缓存 代理缓存, 获取服务端内容进行缓存 客户端浏览器缓存 Nginx代理缓存原理 ...

  9. 运维学习篇之jenkins的安装(CentOS7)

    一. 介绍   Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能二. 作用  1.持续的软件版本 ...

  10. 一、Nginx多站点配置

    一.下载 目录文件: 二.运行方式 (1)直接双击nginx.exe,双击后一个黑色的弹窗一闪而过 (2)打开cmd命令窗口,切换到nginx解压目录下,输入命令 nginx.exe 或者 start ...