- 浏览: 38426 次
- 性别:
- 来自: 北京
文章分类
最新评论
/**
* 页眉页脚
* mark一下,原文地址:https://blog.csdn.net/ae6623/article/details/11734285/
*/
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfHeaderFooter extends PdfPageEventHelper {
/**
* 页眉
*/
public String header = "";
/**
* 文档字体大小,页脚页眉最好和文本大小一致
*/
public int presentFontSize = 12;
/**
* 文档页面大小,最好前面传入,否则默认为A4纸张
*/
public Rectangle pageSize = PageSize.A4;
// 模板
public PdfTemplate total;
// 基础字体对象
public BaseFont bf = null;
// 利用基础字体生成的字体对象,一般用于生成中文文字
public Font fontDetail = null;
/**
*
* Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.
*
*/
public PdfHeaderFooter() {
}
/**
*
* Creates a new instance of PdfReportM1HeaderFooter 构造方法.
*
* @param yeMei
* 页眉字符串
* @param presentFontSize
* 数据体字体大小
* @param pageSize
* 页面文档大小,A4,A5,A6横转翻转等Rectangle对象
*/
public PdfHeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize) {
this.header = yeMei;
this.presentFontSize = presentFontSize;
this.pageSize = pageSize;
}
public void setHeader(String header) {
this.header = header;
}
public void setPresentFontSize(int presentFontSize) {
this.presentFontSize = presentFontSize;
}
/**
*
* TODO 文档打开时创建模板
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(50, 50);// 共 页 的矩形的长宽高
}
/**
*
* TODO 关闭每页的时候,写入页眉,写入'第几页共'这几个字。
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
if (bf == null) {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
}
if (fontDetail == null) {
fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 数据体字体
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 1.写入页眉
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.left(), document.top() + 20, 0);
// 2.写入前半部分的 第 X页/共
int pageS = writer.getPageNumber();
String foot1 = "第 " + pageS + " 页 /共";
Phrase footer = new Phrase(foot1, fontDetail);
// 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
float len = bf.getWidthPoint(foot1, presentFontSize);
// 4.拿到当前的PdfContentByte
PdfContentByte cb = writer.getDirectContent();
// 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了 ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20F, document.bottom() - 20, 0);
// 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len , y 轴和之前的保持一致,底边界-20
cb.addTemplate(total, (document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F, document.bottom() - 20); // 调节模版显示的位置
}
/**
*
* TODO 关闭文档时,替换模板,完成整个页眉页脚组件
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
// 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
total.beginText();
total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色
String foot2 = " " + (writer.getPageNumber() - 1) + " 页";
total.showText(foot2);// 模版显示的内容
total.endText();
total.closePath();
}
}
/**
* 生成pdf
*/
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.test.dorm.domain.vo.DmAgreemeetVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by xyh
*/
public class PdfTestServiceImpl {
private static Logger logger = LoggerFactory.getLogger(PdfTestServiceImpl.class);
/**
* 追加协议内容
*
* @param document 原协议文档
* @param agre 协议内容
* @throws DocumentException
* @throws IOException
*/
private void addAgreement(Document document, DmAgreemeetVo agre) throws DocumentException, IOException {
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese18 = new Font(bfChinese, 18, Font.BOLD);
Font fontChinese16 = new Font(bfChinese, 16, Font.BOLD);
Font fontChinese11Bold = new Font(bfChinese, 11, Font.BOLD);
Font fontChinese11Normal = new Font(bfChinese, 11, Font.NORMAL);
// 标题【物品放行单】
PdfPTable subject = new PdfPTable(1);
//设置每列宽度比例
int width21[] = {98};
subject.setWidths(width21);
subject.getDefaultCell().setBorder(0);
PdfPCell cell21 = new PdfPCell(new Paragraph("物品放行单", fontChinese16));
cell21.setBorder(0);
cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
subject.addCell(cell21);
document.add(subject);
//加入空行
Paragraph blankRow2 = new Paragraph(18f, " ", fontChinese18);
document.add(blankRow2);
// 申请单号
PdfPTable applyCodeTbl = new PdfPTable(3);
BaseColor applyCodeBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int applyCodeCellWidth[] = {160, 30, 50};
applyCodeTbl.setWidths(applyCodeCellWidth);
PdfPCell applyCodeCell1 = this.setPdfPCell("", 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
PdfPCell applyCodeCell2 = this.setPdfPCell("申请单号:", 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
PdfPCell applyCodeCell3 = this.setPdfPCell(agre.getTaskId(), 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
applyCodeCell1.setBorder(0);
applyCodeCell2.setBorder(0);
applyCodeCell3.setBorder(0);
applyCodeCell3.setHorizontalAlignment(Element.ALIGN_LEFT);
applyCodeTbl.addCell(applyCodeCell1);
applyCodeTbl.addCell(applyCodeCell2);
applyCodeTbl.addCell(applyCodeCell3);
document.add(applyCodeTbl);
// 放行日期
PdfPTable releaseDateTbl = new PdfPTable(2);
BaseColor releaseDateBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int releaseDateCellWidth[] = {30, 210};
releaseDateTbl.setWidths(releaseDateCellWidth);
PdfPCell releaseDateCell1 = this.setPdfPCell("放行日期", 25, fontChinese11Normal, releaseDateBorder, true, PdfPCell.BOX);
PdfPCell releaseDateCell2 = this.setPdfPCell("", 25, fontChinese11Normal, releaseDateBorder, true, PdfPCell.BOX);
releaseDateTbl.addCell(releaseDateCell1);
releaseDateTbl.addCell(releaseDateCell2);
document.add(releaseDateTbl);
//申请人信息
PdfPTable applyUserTbl = new PdfPTable(6);
BaseColor applyUserBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int applyUserCellWidth[] = {30, 35, 35, 35, 20, 85};
applyUserTbl.setWidths(applyUserCellWidth);
PdfPCell applyUserCell1 = this.setPdfPCell("申请人", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell3 = this.setPdfPCell("电话", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell4 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell5 = this.setPdfPCell("部门", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell6 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
applyUserTbl.addCell(applyUserCell1);
applyUserTbl.addCell(applyUserCell2);
applyUserTbl.addCell(applyUserCell3);
applyUserTbl.addCell(applyUserCell4);
applyUserTbl.addCell(applyUserCell5);
applyUserTbl.addCell(applyUserCell6);
// 携物人信息
PdfPCell carryGoodsUserCell1 = this.setPdfPCell("携物人", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell3 = this.setPdfPCell("电话", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell4 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell5 = this.setPdfPCell("单位", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell6 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
applyUserTbl.addCell(carryGoodsUserCell1);
applyUserTbl.addCell(carryGoodsUserCell2);
applyUserTbl.addCell(carryGoodsUserCell3);
applyUserTbl.addCell(carryGoodsUserCell4);
applyUserTbl.addCell(carryGoodsUserCell5);
applyUserTbl.addCell(carryGoodsUserCell6);
document.add(applyUserTbl);
// 物品列表
PdfPTable goodsListTbl = new PdfPTable(4);
BaseColor goodsListBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int goodsListCellWidth[] = {75, 15, 75, 75};
goodsListTbl.setWidths(goodsListCellWidth);
PdfPCell goodsListCell1 = this.setPdfPCell("物品名称", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell2 = this.setPdfPCell("数量", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell3 = this.setPdfPCell("规格", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell4 = this.setPdfPCell("型号", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
goodsListTbl.addCell(goodsListCell1);
goodsListTbl.addCell(goodsListCell2);
goodsListTbl.addCell(goodsListCell3);
goodsListTbl.addCell(goodsListCell4);
int goodsRows = 10;
for (int i = 0; i < goodsRows; i++) {
PdfPCell goodsNameCell = this.setPdfPCell("物品名称", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsCountCell = this.setPdfPCell(i + "", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsSpecificationCell = this.setPdfPCell("啊大大逻辑发阿达啊爱就爱累加方式sfsfsfdsh哈哈faaaaaaaadfsfsfdsfds福建省发送到发送到", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsModelCell = this.setPdfPCell("adsadjaojdddddddddddsadsa2342424234s2aa" + i, null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
goodsListTbl.addCell(goodsNameCell);
goodsListTbl.addCell(goodsCountCell);
goodsListTbl.addCell(goodsSpecificationCell);
goodsListTbl.addCell(goodsModelCell);
}
document.add(goodsListTbl);
// 物品所属部门
PdfPTable goodsBelongDeptTbl = new PdfPTable(2);
BaseColor goodsBelongDeptBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int goodsBelongDeptCellWidth[] = {30, 210};
goodsBelongDeptTbl.setWidths(goodsBelongDeptCellWidth);
PdfPCell goodsBelongDeptCell1 = this.setPdfPCell("物品所属部门", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell goodsBelongDeptCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(goodsBelongDeptCell1);
goodsBelongDeptTbl.addCell(goodsBelongDeptCell2);
// 批准人
PdfPCell approverUserCell1 = this.setPdfPCell("批准人", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell approverUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(approverUserCell1);
goodsBelongDeptTbl.addCell(approverUserCell2);
// 出门原因
PdfPCell outReasonCell1 = this.setPdfPCell("出门原因", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell outReasonCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(outReasonCell1);
goodsBelongDeptTbl.addCell(outReasonCell2);
// 备注
PdfPCell remarkCell1 = this.setPdfPCell("备注", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell remarkCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(remarkCell1);
goodsBelongDeptTbl.addCell(remarkCell2);
// 注
PdfPCell noteCell1 = this.setPdfPCell("注:", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell1.setRowspan(3);
goodsBelongDeptTbl.addCell(noteCell1);
PdfPCell noteCell2 = this.setPdfPCell("1、放行单当日有效,逾期作废", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell2.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell2);
PdfPCell noteCell3 = this.setPdfPCell("2、各项内容需填写完整、准确,不得涂改、损坏", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell3.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell3);
PdfPCell noteCell4 = this.setPdfPCell("3、须将此物品放行单交于当值安保员", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell4.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell4);
document.add(goodsBelongDeptTbl);
}
/**
* 填充单元格
*
* @param cellVal 值
* @param height 表格高度
* @param font 字体
* @param lightGrey 边框颜色
* @param borderFlag 是否显示边框 true:显示;false:不显示
* @param borderSide 显示或不显示那个边框
* @return
*/
private PdfPCell setPdfPCell(String cellVal, Integer height, Font font, BaseColor lightGrey, boolean borderFlag, int borderSide) {
PdfPCell cell = new PdfPCell(new Paragraph(cellVal, font));
//表格高度,null表示需要自动换行展示,所以设置padding
if (height != null) {
cell.setFixedHeight(25);
} else {
cell.setPadding(4);
}
//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//边框颜色
cell.setBorderColor(lightGrey);
if (!borderFlag) {
//去掉左右边框
cell.disableBorderSide(borderSide);
} else {
//添加左右边框
cell.enableBorderSide(borderSide);
}
return cell;
}
/**
* 加文本水印
* @param inputFile 你的PDF文件地址
* @param outputFile 添加水印后生成PDF存放的地址
* @param waterMarkName 你的水印
* @return
*/
public static boolean waterMarkWidthText(String inputFile,
String outputFile, String waterMarkName) {
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
// 计算水印X,Y坐标
// float x = pageRect.getWidth()/10;
// float y = pageRect.getHeight()/10-10;
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(0.8f);
under.setGState(gs);
under.restoreState();
under.beginText();
under.setFontAndSize(base, 25);
under.setTextMatrix(30, 30);
under.setColorFill(BaseColor.RED);
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 8; x++) {
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_LEFT
, waterMarkName, 100 + 300 * x, 300 * y, 45);
}
}
// 添加水印文字
under.endText();
under.setLineWidth(1f);
under.stroke();
}
stamper.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 设置页码
* @param writer
* @throws DocumentException
* @throws IOException
*/
private void setFooter(PdfWriter writer) throws Exception {
PdfHeaderFooter headerFooter = new PdfHeaderFooter();//就是上面那个类
writer.setBoxSize("art", PageSize.A4);
writer.setPageEvent(headerFooter);
}
/**
* 生成pdf文件
*
* @param pdfFile 要生成的pdf文件
* @param agre pdf文件中的数据对象
* @throws Exception
*/
private void createPdfFile(File pdfFile, DmAgreemeetVo agre) throws Exception {
FileOutputStream fo = new FileOutputStream(pdfFile);
Document document1 = new Document(PageSize.A4.rotate());
PdfWriter writer = null;
// 初始化pdf文件写出流
writer = PdfWriter.getInstance(document1, fo);
this.setFooter(writer);
document1.open();
agre.setDepositStatus(1);
// 追加协议内容
this.addAgreement(document1, agre);
document1.close();
writer.close();
fo.close();
}
/**
* 加图片水印
*
* @param pdfFile 源pdf文件
* @param pdfWatermarkFile 加水印后的pdf文件
* @throws Exception
*/
private void waterMarkWidthImage(File pdfFile, File pdfWatermarkFile) throws Exception {
// 源pdf文件
FileInputStream inputFile = new FileInputStream(pdfFile);
PdfReader reader = new PdfReader(inputFile);
int total = reader.getNumberOfPages() + 1;
//实现A4纸页面 并且横向显示(不设置则为纵向)
Document document = new Document(PageSize.A4.rotate());
// 打开文档
document.open();
// 加入水印
FileOutputStream resultOutFile = new FileOutputStream(pdfWatermarkFile);
PdfStamper stamper = new PdfStamper(reader, resultOutFile);
PdfContentByte waterMar = null;
// 设置水印透明度
PdfGState gs = new PdfGState();
// 设置笔触字体不透明度为0.2f
gs.setStrokeOpacity(0.8f);
gs.setFillOpacity(0.8f);
try {
Image image = Image.getInstance("F:/京东logo.png");
// 设置坐标 绝对位置 X Y
image.setAbsolutePosition(200, 300);
// 设置旋转弧度
image.setRotation(30);// 旋转 弧度
// 设置旋转角度
image.setRotationDegrees(45);// 旋转 角度
// 设置等比缩放
image.scalePercent(20);// 依照比例缩放
for (int i = 1; i < total; i++) {
waterMar = stamper.getUnderContent(i);
// 开始设置水印
waterMar.beginText();
// 设置透明度
waterMar.setGState(gs);
// 添加水印图片
waterMar.addImage(image);
//结束设置
waterMar.endText();
waterMar.stroke();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
waterMar = null;
gs = null;
// 关闭文档
document.close();
stamper.close();
inputFile.close();
resultOutFile.close();
}
}
public static void main(String[] args) throws Exception {
//创建文件夹目录
String dirPath = "/export/home/releasePermitDownload";
File baseDir = new File(dirPath);
if (!baseDir.exists()) {
boolean mkResult = baseDir.mkdirs();
if (!mkResult) {
logger.error("创建目录失败:{}", baseDir.getAbsolutePath());
return;
}
}
String baseDirPath = baseDir.getPath();
String applyErp = "xiayanghui";
File pdfFile = new File(baseDirPath + File.separator + applyErp + System.currentTimeMillis() + ".pdf");
DmAgreemeetVo agre = new DmAgreemeetVo();
agre.setTaskId("FXD00000000001");
PdfTestServiceImpl pdfTestService = new PdfTestServiceImpl();
// 生成pdf文件
pdfTestService.createPdfFile(pdfFile, agre);
// 加水印
File pdfWatermarkFile = new File(baseDirPath + File.separator + applyErp + "Watermark" + System.currentTimeMillis() + ".pdf");
pdfTestService.waterMarkWidthImage(pdfFile, pdfWatermarkFile);
// 记得最后删除文件
pdfFile.delete();
//pdfWatermarkFile.delete();
}
}
* 页眉页脚
* mark一下,原文地址:https://blog.csdn.net/ae6623/article/details/11734285/
*/
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
public class PdfHeaderFooter extends PdfPageEventHelper {
/**
* 页眉
*/
public String header = "";
/**
* 文档字体大小,页脚页眉最好和文本大小一致
*/
public int presentFontSize = 12;
/**
* 文档页面大小,最好前面传入,否则默认为A4纸张
*/
public Rectangle pageSize = PageSize.A4;
// 模板
public PdfTemplate total;
// 基础字体对象
public BaseFont bf = null;
// 利用基础字体生成的字体对象,一般用于生成中文文字
public Font fontDetail = null;
/**
*
* Creates a new instance of PdfReportM1HeaderFooter 无参构造方法.
*
*/
public PdfHeaderFooter() {
}
/**
*
* Creates a new instance of PdfReportM1HeaderFooter 构造方法.
*
* @param yeMei
* 页眉字符串
* @param presentFontSize
* 数据体字体大小
* @param pageSize
* 页面文档大小,A4,A5,A6横转翻转等Rectangle对象
*/
public PdfHeaderFooter(String yeMei, int presentFontSize, Rectangle pageSize) {
this.header = yeMei;
this.presentFontSize = presentFontSize;
this.pageSize = pageSize;
}
public void setHeader(String header) {
this.header = header;
}
public void setPresentFontSize(int presentFontSize) {
this.presentFontSize = presentFontSize;
}
/**
*
* TODO 文档打开时创建模板
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onOpenDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onOpenDocument(PdfWriter writer, Document document) {
total = writer.getDirectContent().createTemplate(50, 50);// 共 页 的矩形的长宽高
}
/**
*
* TODO 关闭每页的时候,写入页眉,写入'第几页共'这几个字。
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onEndPage(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onEndPage(PdfWriter writer, Document document) {
try {
if (bf == null) {
bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", false);
}
if (fontDetail == null) {
fontDetail = new Font(bf, presentFontSize, Font.NORMAL);// 数据体字体
}
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 1.写入页眉
ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(header, fontDetail), document.left(), document.top() + 20, 0);
// 2.写入前半部分的 第 X页/共
int pageS = writer.getPageNumber();
String foot1 = "第 " + pageS + " 页 /共";
Phrase footer = new Phrase(foot1, fontDetail);
// 3.计算前半部分的foot1的长度,后面好定位最后一部分的'Y页'这俩字的x轴坐标,字体长度也要计算进去 = len
float len = bf.getWidthPoint(foot1, presentFontSize);
// 4.拿到当前的PdfContentByte
PdfContentByte cb = writer.getDirectContent();
// 5.写入页脚1,x轴就是(右margin+左margin + right() -left()- len)/2.0F 再给偏移20F适合人类视觉感受,否则肉眼看上去就太偏左了 ,y轴就是底边界-20,否则就贴边重叠到数据体里了就不是页脚了;注意Y轴是从下往上累加的,最上方的Top值是大于Bottom好几百开外的。
ColumnText.showTextAligned(cb, Element.ALIGN_CENTER, footer, (document.rightMargin() + document.right() + document.leftMargin() - document.left() - len) / 2.0F + 20F, document.bottom() - 20, 0);
// 6.写入页脚2的模板(就是页脚的Y页这俩字)添加到文档中,计算模板的和Y轴,X=(右边界-左边界 - 前半部分的len值)/2.0F + len , y 轴和之前的保持一致,底边界-20
cb.addTemplate(total, (document.rightMargin() + document.right() + document.leftMargin() - document.left()) / 2.0F + 20F, document.bottom() - 20); // 调节模版显示的位置
}
/**
*
* TODO 关闭文档时,替换模板,完成整个页眉页脚组件
*
* @see com.itextpdf.text.pdf.PdfPageEventHelper#onCloseDocument(com.itextpdf.text.pdf.PdfWriter, com.itextpdf.text.Document)
*/
@Override
public void onCloseDocument(PdfWriter writer, Document document) {
// 7.最后一步了,就是关闭文档的时候,将模板替换成实际的 Y 值,至此,page x of y 制作完毕,完美兼容各种文档size。
total.beginText();
total.setFontAndSize(bf, presentFontSize);// 生成的模版的字体、颜色
String foot2 = " " + (writer.getPageNumber() - 1) + " 页";
total.showText(foot2);// 模版显示的内容
total.endText();
total.closePath();
}
}
/**
* 生成pdf
*/
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfGState;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import com.itextpdf.text.pdf.PdfWriter;
import com.test.dorm.domain.vo.DmAgreemeetVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by xyh
*/
public class PdfTestServiceImpl {
private static Logger logger = LoggerFactory.getLogger(PdfTestServiceImpl.class);
/**
* 追加协议内容
*
* @param document 原协议文档
* @param agre 协议内容
* @throws DocumentException
* @throws IOException
*/
private void addAgreement(Document document, DmAgreemeetVo agre) throws DocumentException, IOException {
//设置字体
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
Font fontChinese18 = new Font(bfChinese, 18, Font.BOLD);
Font fontChinese16 = new Font(bfChinese, 16, Font.BOLD);
Font fontChinese11Bold = new Font(bfChinese, 11, Font.BOLD);
Font fontChinese11Normal = new Font(bfChinese, 11, Font.NORMAL);
// 标题【物品放行单】
PdfPTable subject = new PdfPTable(1);
//设置每列宽度比例
int width21[] = {98};
subject.setWidths(width21);
subject.getDefaultCell().setBorder(0);
PdfPCell cell21 = new PdfPCell(new Paragraph("物品放行单", fontChinese16));
cell21.setBorder(0);
cell21.setHorizontalAlignment(Element.ALIGN_CENTER);
subject.addCell(cell21);
document.add(subject);
//加入空行
Paragraph blankRow2 = new Paragraph(18f, " ", fontChinese18);
document.add(blankRow2);
// 申请单号
PdfPTable applyCodeTbl = new PdfPTable(3);
BaseColor applyCodeBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int applyCodeCellWidth[] = {160, 30, 50};
applyCodeTbl.setWidths(applyCodeCellWidth);
PdfPCell applyCodeCell1 = this.setPdfPCell("", 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
PdfPCell applyCodeCell2 = this.setPdfPCell("申请单号:", 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
PdfPCell applyCodeCell3 = this.setPdfPCell(agre.getTaskId(), 25, fontChinese11Normal, applyCodeBorder, true, PdfPCell.BOX);
applyCodeCell1.setBorder(0);
applyCodeCell2.setBorder(0);
applyCodeCell3.setBorder(0);
applyCodeCell3.setHorizontalAlignment(Element.ALIGN_LEFT);
applyCodeTbl.addCell(applyCodeCell1);
applyCodeTbl.addCell(applyCodeCell2);
applyCodeTbl.addCell(applyCodeCell3);
document.add(applyCodeTbl);
// 放行日期
PdfPTable releaseDateTbl = new PdfPTable(2);
BaseColor releaseDateBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int releaseDateCellWidth[] = {30, 210};
releaseDateTbl.setWidths(releaseDateCellWidth);
PdfPCell releaseDateCell1 = this.setPdfPCell("放行日期", 25, fontChinese11Normal, releaseDateBorder, true, PdfPCell.BOX);
PdfPCell releaseDateCell2 = this.setPdfPCell("", 25, fontChinese11Normal, releaseDateBorder, true, PdfPCell.BOX);
releaseDateTbl.addCell(releaseDateCell1);
releaseDateTbl.addCell(releaseDateCell2);
document.add(releaseDateTbl);
//申请人信息
PdfPTable applyUserTbl = new PdfPTable(6);
BaseColor applyUserBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int applyUserCellWidth[] = {30, 35, 35, 35, 20, 85};
applyUserTbl.setWidths(applyUserCellWidth);
PdfPCell applyUserCell1 = this.setPdfPCell("申请人", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell3 = this.setPdfPCell("电话", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell4 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell5 = this.setPdfPCell("部门", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell applyUserCell6 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
applyUserTbl.addCell(applyUserCell1);
applyUserTbl.addCell(applyUserCell2);
applyUserTbl.addCell(applyUserCell3);
applyUserTbl.addCell(applyUserCell4);
applyUserTbl.addCell(applyUserCell5);
applyUserTbl.addCell(applyUserCell6);
// 携物人信息
PdfPCell carryGoodsUserCell1 = this.setPdfPCell("携物人", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell3 = this.setPdfPCell("电话", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell4 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell5 = this.setPdfPCell("单位", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
PdfPCell carryGoodsUserCell6 = this.setPdfPCell("", 25, fontChinese11Normal, applyUserBorder, true, PdfPCell.BOX);
applyUserTbl.addCell(carryGoodsUserCell1);
applyUserTbl.addCell(carryGoodsUserCell2);
applyUserTbl.addCell(carryGoodsUserCell3);
applyUserTbl.addCell(carryGoodsUserCell4);
applyUserTbl.addCell(carryGoodsUserCell5);
applyUserTbl.addCell(carryGoodsUserCell6);
document.add(applyUserTbl);
// 物品列表
PdfPTable goodsListTbl = new PdfPTable(4);
BaseColor goodsListBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int goodsListCellWidth[] = {75, 15, 75, 75};
goodsListTbl.setWidths(goodsListCellWidth);
PdfPCell goodsListCell1 = this.setPdfPCell("物品名称", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell2 = this.setPdfPCell("数量", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell3 = this.setPdfPCell("规格", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsListCell4 = this.setPdfPCell("型号", 25, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
goodsListTbl.addCell(goodsListCell1);
goodsListTbl.addCell(goodsListCell2);
goodsListTbl.addCell(goodsListCell3);
goodsListTbl.addCell(goodsListCell4);
int goodsRows = 10;
for (int i = 0; i < goodsRows; i++) {
PdfPCell goodsNameCell = this.setPdfPCell("物品名称", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsCountCell = this.setPdfPCell(i + "", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsSpecificationCell = this.setPdfPCell("啊大大逻辑发阿达啊爱就爱累加方式sfsfsfdsh哈哈faaaaaaaadfsfsfdsfds福建省发送到发送到", null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
PdfPCell goodsModelCell = this.setPdfPCell("adsadjaojdddddddddddsadsa2342424234s2aa" + i, null, fontChinese11Normal, goodsListBorder, true, PdfPCell.BOX);
goodsListTbl.addCell(goodsNameCell);
goodsListTbl.addCell(goodsCountCell);
goodsListTbl.addCell(goodsSpecificationCell);
goodsListTbl.addCell(goodsModelCell);
}
document.add(goodsListTbl);
// 物品所属部门
PdfPTable goodsBelongDeptTbl = new PdfPTable(2);
BaseColor goodsBelongDeptBorder = new BaseColor(0xCC, 0xCC, 0xCC);
int goodsBelongDeptCellWidth[] = {30, 210};
goodsBelongDeptTbl.setWidths(goodsBelongDeptCellWidth);
PdfPCell goodsBelongDeptCell1 = this.setPdfPCell("物品所属部门", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell goodsBelongDeptCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(goodsBelongDeptCell1);
goodsBelongDeptTbl.addCell(goodsBelongDeptCell2);
// 批准人
PdfPCell approverUserCell1 = this.setPdfPCell("批准人", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell approverUserCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(approverUserCell1);
goodsBelongDeptTbl.addCell(approverUserCell2);
// 出门原因
PdfPCell outReasonCell1 = this.setPdfPCell("出门原因", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell outReasonCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(outReasonCell1);
goodsBelongDeptTbl.addCell(outReasonCell2);
// 备注
PdfPCell remarkCell1 = this.setPdfPCell("备注", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
PdfPCell remarkCell2 = this.setPdfPCell("", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
goodsBelongDeptTbl.addCell(remarkCell1);
goodsBelongDeptTbl.addCell(remarkCell2);
// 注
PdfPCell noteCell1 = this.setPdfPCell("注:", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell1.setRowspan(3);
goodsBelongDeptTbl.addCell(noteCell1);
PdfPCell noteCell2 = this.setPdfPCell("1、放行单当日有效,逾期作废", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell2.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell2);
PdfPCell noteCell3 = this.setPdfPCell("2、各项内容需填写完整、准确,不得涂改、损坏", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell3.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell3);
PdfPCell noteCell4 = this.setPdfPCell("3、须将此物品放行单交于当值安保员", 25, fontChinese11Normal, goodsBelongDeptBorder, true, PdfPCell.BOX);
noteCell4.setHorizontalAlignment(Element.ALIGN_LEFT);
goodsBelongDeptTbl.addCell(noteCell4);
document.add(goodsBelongDeptTbl);
}
/**
* 填充单元格
*
* @param cellVal 值
* @param height 表格高度
* @param font 字体
* @param lightGrey 边框颜色
* @param borderFlag 是否显示边框 true:显示;false:不显示
* @param borderSide 显示或不显示那个边框
* @return
*/
private PdfPCell setPdfPCell(String cellVal, Integer height, Font font, BaseColor lightGrey, boolean borderFlag, int borderSide) {
PdfPCell cell = new PdfPCell(new Paragraph(cellVal, font));
//表格高度,null表示需要自动换行展示,所以设置padding
if (height != null) {
cell.setFixedHeight(25);
} else {
cell.setPadding(4);
}
//水平居中
cell.setHorizontalAlignment(Element.ALIGN_CENTER);
//垂直居中
cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
//边框颜色
cell.setBorderColor(lightGrey);
if (!borderFlag) {
//去掉左右边框
cell.disableBorderSide(borderSide);
} else {
//添加左右边框
cell.enableBorderSide(borderSide);
}
return cell;
}
/**
* 加文本水印
* @param inputFile 你的PDF文件地址
* @param outputFile 添加水印后生成PDF存放的地址
* @param waterMarkName 你的水印
* @return
*/
public static boolean waterMarkWidthText(String inputFile,
String outputFile, String waterMarkName) {
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
outputFile));
//这里的字体设置比较关键,这个设置是支持中文的写法
BaseFont base = BaseFont.createFont("STSong-Light",
"UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);// 使用系统字体
int total = reader.getNumberOfPages() + 1;
PdfContentByte under;
Rectangle pageRect = null;
for (int i = 1; i < total; i++) {
pageRect = stamper.getReader().
getPageSizeWithRotation(i);
// 计算水印X,Y坐标
// float x = pageRect.getWidth()/10;
// float y = pageRect.getHeight()/10-10;
// 获得PDF最顶层
under = stamper.getOverContent(i);
under.saveState();
// set Transparency
PdfGState gs = new PdfGState();
// 设置透明度为0.2
gs.setFillOpacity(0.8f);
under.setGState(gs);
under.restoreState();
under.beginText();
under.setFontAndSize(base, 25);
under.setTextMatrix(30, 30);
under.setColorFill(BaseColor.RED);
for (int y = 0; y < 10; y++) {
for (int x = 0; x < 8; x++) {
// 水印文字成45度角倾斜
under.showTextAligned(Element.ALIGN_LEFT
, waterMarkName, 100 + 300 * x, 300 * y, 45);
}
}
// 添加水印文字
under.endText();
under.setLineWidth(1f);
under.stroke();
}
stamper.close();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 设置页码
* @param writer
* @throws DocumentException
* @throws IOException
*/
private void setFooter(PdfWriter writer) throws Exception {
PdfHeaderFooter headerFooter = new PdfHeaderFooter();//就是上面那个类
writer.setBoxSize("art", PageSize.A4);
writer.setPageEvent(headerFooter);
}
/**
* 生成pdf文件
*
* @param pdfFile 要生成的pdf文件
* @param agre pdf文件中的数据对象
* @throws Exception
*/
private void createPdfFile(File pdfFile, DmAgreemeetVo agre) throws Exception {
FileOutputStream fo = new FileOutputStream(pdfFile);
Document document1 = new Document(PageSize.A4.rotate());
PdfWriter writer = null;
// 初始化pdf文件写出流
writer = PdfWriter.getInstance(document1, fo);
this.setFooter(writer);
document1.open();
agre.setDepositStatus(1);
// 追加协议内容
this.addAgreement(document1, agre);
document1.close();
writer.close();
fo.close();
}
/**
* 加图片水印
*
* @param pdfFile 源pdf文件
* @param pdfWatermarkFile 加水印后的pdf文件
* @throws Exception
*/
private void waterMarkWidthImage(File pdfFile, File pdfWatermarkFile) throws Exception {
// 源pdf文件
FileInputStream inputFile = new FileInputStream(pdfFile);
PdfReader reader = new PdfReader(inputFile);
int total = reader.getNumberOfPages() + 1;
//实现A4纸页面 并且横向显示(不设置则为纵向)
Document document = new Document(PageSize.A4.rotate());
// 打开文档
document.open();
// 加入水印
FileOutputStream resultOutFile = new FileOutputStream(pdfWatermarkFile);
PdfStamper stamper = new PdfStamper(reader, resultOutFile);
PdfContentByte waterMar = null;
// 设置水印透明度
PdfGState gs = new PdfGState();
// 设置笔触字体不透明度为0.2f
gs.setStrokeOpacity(0.8f);
gs.setFillOpacity(0.8f);
try {
Image image = Image.getInstance("F:/京东logo.png");
// 设置坐标 绝对位置 X Y
image.setAbsolutePosition(200, 300);
// 设置旋转弧度
image.setRotation(30);// 旋转 弧度
// 设置旋转角度
image.setRotationDegrees(45);// 旋转 角度
// 设置等比缩放
image.scalePercent(20);// 依照比例缩放
for (int i = 1; i < total; i++) {
waterMar = stamper.getUnderContent(i);
// 开始设置水印
waterMar.beginText();
// 设置透明度
waterMar.setGState(gs);
// 添加水印图片
waterMar.addImage(image);
//结束设置
waterMar.endText();
waterMar.stroke();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
waterMar = null;
gs = null;
// 关闭文档
document.close();
stamper.close();
inputFile.close();
resultOutFile.close();
}
}
public static void main(String[] args) throws Exception {
//创建文件夹目录
String dirPath = "/export/home/releasePermitDownload";
File baseDir = new File(dirPath);
if (!baseDir.exists()) {
boolean mkResult = baseDir.mkdirs();
if (!mkResult) {
logger.error("创建目录失败:{}", baseDir.getAbsolutePath());
return;
}
}
String baseDirPath = baseDir.getPath();
String applyErp = "xiayanghui";
File pdfFile = new File(baseDirPath + File.separator + applyErp + System.currentTimeMillis() + ".pdf");
DmAgreemeetVo agre = new DmAgreemeetVo();
agre.setTaskId("FXD00000000001");
PdfTestServiceImpl pdfTestService = new PdfTestServiceImpl();
// 生成pdf文件
pdfTestService.createPdfFile(pdfFile, agre);
// 加水印
File pdfWatermarkFile = new File(baseDirPath + File.separator + applyErp + "Watermark" + System.currentTimeMillis() + ".pdf");
pdfTestService.waterMarkWidthImage(pdfFile, pdfWatermarkFile);
// 记得最后删除文件
pdfFile.delete();
//pdfWatermarkFile.delete();
}
}
相关推荐
本篇文章将深入探讨如何使用iText来生成PDF的目录。 首先,理解PDF目录的基本结构至关重要。PDF目录通常由一系列层次化的书签构成,每个书签对应文档中的一个部分、章节或子章节。在iText中,这些书签是通过`...
JavaPDF文件生成是Java开发中一个重要的领域,主要用于创建、编辑和处理PDF文档。iTextPDF是一个流行的开源库,它提供了丰富的API,使得在Java环境中生成高质量的PDF文档变得简单易行。本篇文章将深入探讨如何使用...
itext7 html转换为pdf;iText7页码、页眉、页脚,itext 的复杂表格实现;完整springboot项目代码
首先,iTextPDF的核心在于其 PdfWriter 类,它是生成PDF文件的关键。PdfWriter允许开发者创建一个新的PDF文档,或者向已存在的PDF文档添加内容。通过创建一个PdfWriter实例,开发者可以指定输出流(如文件或内存流)...
5. 加载图片:使用`com.itextpdf.text.Image`类加载图片,例如,`Image.getInstance("image.png")`。这里可以设置图片的DPI,如`image.setDpi(300, 300)`,以确保高清效果。 6. 缩放图片:`iText`提供了三种缩放...
在给定的压缩包文件中,"itextpdf"可能是包含不同版本iTextPDF库的文件夹。每个版本通常以jar包的形式存在,例如`itextpdf-5.x.x.jar`代表iTextPDF的5.x.x版本。为了使用,将jar包添加到项目的类路径中,然后可以...
iText 实现根据pdf模板生成pdf并导入数据.....iText-5.0.6.jar....附字体jar包 很久很久之前写的了 代码见: https://github.com/thisisthis/noh-doc
iTextPDF作为一个强大的PDF操作工具包,为开发者提供了丰富的API,使得生成、编辑PDF文件变得简单易行。本文将详细探讨iTextPDF 5.5.5版本的核心功能和使用方法。 iTextPDF是iText库的一个子项目,专注于PDF文档的...
4. **生成PDF**:使用Itext读取FreeMarker处理后的结果,并将其转换为PDF文档。 在给定的文件列表中,`pom.xml`可能是Maven项目的依赖管理文件,其中会包含FreeMarker和Itext的依赖项。`src`则包含了源代码,可能...
iText生成pdf解决中文不显示字库,pdf凉字不显示,由于生成iText插件生成pdf的时候中文会显示不出来,遇到过的是"凉"字,查到是字体库的原因,网上下载字体库msyh.ttc,生成的时候指定字体库,就可以解决了,小bug一...
11. **事件处理**:讨论iTextPDF中的事件模型,允许开发者在特定PDF生成阶段进行自定义处理。 12. **性能优化**:提供有关如何提高处理大量PDF文档时的性能的建议,例如使用内存管理策略和流式写入。 13. **PDF/A...
iText5-itextpdf-5.5.11.jar是主要的库文件,包含了大部分的功能实现;iText5-xmlworker-5.5.11.jar则用于处理HTML到PDF的转换,使得你可以将网页内容轻松转换为PDF格式。 2. itext-asian5.20.jar:这个库是iText的...
这意味着开发人员可能已经实现了相关的JavaScript或者HTML5 API来与后端交互,获取由Itext生成的PDF文件,并在用户界面上提供预览和打印功能。前端预览通常使用`<iframe>`或者PDF.js这样的库实现,而打印则可以通过...
1. iText主库:`itextpdf-版本号.jar` - 这是iText的核心库,包含了生成PDF的所有基本功能。 2. Bouncy Castle库:`bcprov-jdk15on-版本号.jar` - iText在处理加密和数字签名时依赖Bouncy Castle库,因此需要将其...
iTextpdf是一款强大的Java库,专门用于生成和编辑PDF文档,同时也支持在Android平台上使用。这个库使得开发者能够轻松地在应用程序中创建、修改和处理PDF文档,包括添加文本、图像、表格、链接等元素。在标题中提到...
本篇将深入探讨如何使用iText来实现Word文档中的目录生成、页码设置、表格创建以及图片插入等功能。 首先,我们需要理解的是,由于iText主要用于PDF,所以在操作Word时,我们通常会借助于像Apache POI这样的库来...
com.itextpdf.itextpdf.5.5.13.3 相关jar包和包含源码jar包 <groupId>com.itextpdf</groupId> <artifactId>itextpdf <version>5.5.13.3 下载地址为: ...
通过以上步骤,我们可以使用iText 2.1.7动态生成包含文本、图像、表格、列表等内容的PDF文件。当然,iText的功能远不止这些,还有如书签、超链接、电子签名等高级特性等待探索。实践过程中,不断查阅官方文档和相关...
本主题聚焦于使用Java的iTextPDF库来完成这个任务。iTextPDF是一个强大的Java库,允许开发人员创建、修改和优化PDF文档。 首先,我们需要了解HTML和PDF之间的差异。HTML是一种标记语言,用于构建和设计网页,而PDF...