实例介绍
【实例简介】
【实例截图】
【核心代码】
package com.test.main;
import java.io.File;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;
import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
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.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
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;
public class test4 {
public static void buidPDF(String pdfFile, String imageFile, String waterMarkName, int permission) {
try {
File file = File.createTempFile("tempFile", ".pdf"); // 创建临时文件
// 生成PDF
if (createPDFFile(file)) {
waterMark(file.getPath(), imageFile, pdfFile, waterMarkName,
permission); // 添加水印
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 创建PDF文件
*
* @param file
* 临时文件
* @return 成功/失败
*/
public static boolean createPDFFile(File file) {
Rectangle rect = new Rectangle(PageSize.A5);
Document document = new Document();
try {
PdfWriter.getInstance(document,
new FileOutputStream("TableCellBorder.pdf"));
document.open();
PdfPTable table = new PdfPTable(3);
PdfPCell cell1 = new PdfPCell(new Phrase("Cell 1"));
cell1.setUseBorderPadding(true);
//
// Setting cell's border width and color
//
cell1.setBorderWidth(5f);
cell1.setBorderColor(BaseColor.BLUE);
table.addCell(cell1);
PdfPCell cell2 = new PdfPCell(new Phrase("Cell 2"));
cell2.setUseBorderPadding(true);
//
// Setting cell's background color
//
cell2.setBackgroundColor(BaseColor.GRAY);
//
// Setting cell's individual border color
//
cell2.setBorderWidthTop(1f);
cell2.setBorderColorTop(BaseColor.RED);
cell2.setBorderColorRight(BaseColor.GREEN);
cell2.setBorderColorBottom(BaseColor.BLUE);
cell2.setBorderColorLeft(BaseColor.BLACK);
table.addCell(cell2);
PdfPCell cell3 = new PdfPCell(new Phrase("Cell 3"));
cell3.setUseBorderPadding(true);
//
// Setting cell's individual border width
//
cell3.setBorderWidthTop(2f);
cell3.setBorderWidthRight(1f);
cell3.setBorderWidthBottom(2f);
cell3.setBorderWidthLeft(1f);
table.addCell(cell3);
table.completeRow();
document.add(table);
} catch (Exception e) {
e.printStackTrace();
} finally {
document.close();
}
return true;
}
public static void waterMark(String inputFile, String imageFile,
String outputFile, String waterMarkName, int permission) {
try {
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
outputFile));
BaseFont base = BaseFont.createFont(
"C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);// 使用系统字体
int total = reader.getNumberOfPages() 1;
Image image = Image.getInstance(imageFile);
// 图片位置
image.setAbsolutePosition(0,88);
image.setRotationDegrees(45);
PdfContentByte under;
int j = waterMarkName.length();
char c = 0;
int rise = 0;
for (int i = 1; i < total; i ) {
rise = 400;
under = stamper.getUnderContent(i);
under.beginText();
under.setFontAndSize(base, 30);
if (j >= 15) {
under.setTextMatrix(200, 120);
for (int k = 0; k < j; k ) {
under.setTextRise(rise);
c = waterMarkName.charAt(k);
under.showText(c "");
}
} else {
under.setTextMatrix(240, 100);
for (int k = 0; k < j; k ) {
under.setTextRise(rise);
c = waterMarkName.charAt(k);
under.showText(c "");
rise -= 18;
}
}
// 添加水印文字
under.endText();
// 添加水印图片
under.addImage(image);
/* // 画个圈
under.ellipse(250, 450, 350, 550);
under.setLineWidth(1f);
under.stroke();*/
}
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String imageFilePath = "C:\\Users\\admin\\Desktop\\ruituo.png"; // 水印图片路径
String pdfFilePath = "F:\\Download\\itext.PDF"; // 文件生成路径
buidPDF(pdfFilePath, imageFilePath, "", 16);
}
public static String leftPad(String str, int i) {
int addSpaceNo = i-str.length();
String space = "";
for (int k=0; k<addSpaceNo; k ){
space= " " space;
};
String result =space str ;
return result;
}
public static void add(List<String> list,int num){
for(int i=0;i<num;i ){
list.add("A" i);
}
}
public static void add2(List<String> list,int num){
for(int i=0;i<num;i ){
list.add("D" i);
}
}
public static void add1(List<String> list,int num){
for(int i=0;i<num;i ){
list.add("C" i);
}
}
public static String printBlank(int tmp){
String space="";
for(int m=0;m<tmp;m ){
space=space " ";
}
return space;
}
/**
* 创建一个跨多列的单元格
* @param colspan 所占列数
* @param paragraph 单元格内容文字
* @param align 对齐方式
*/
public static PdfPCell newPdfPCellByColspan(int colspan,Paragraph paragraph,int align){
PdfPTable iTable=new PdfPTable(1);
PdfPCell iCell=new PdfPCell();
iCell.setColspan(colspan);
iCell.setBorder(0);
iCell.addElement(paragraph);
iCell.setHorizontalAlignment(align);
iTable.addCell(iCell);
PdfPCell cell=new PdfPCell(iTable);
return cell;
}
/**
* 创建一个跨多行的单元格
* @param rows 所占行数
* @param paragraph 单元格内容文字
* @param align 对齐方式
*/
public static PdfPCell newPdfPCellByRows(int rows,Paragraph paragraph,int align){
PdfPTable iTable=new PdfPTable(1);
PdfPCell iCell=new PdfPCell();
iCell.setFixedHeight(iCell.getFixedHeight()*rows);
iTable.addCell(iCell);
iCell.addElement(paragraph);
iCell.setHorizontalAlignment(align);
PdfPCell cell=new PdfPCell(iTable);
return cell;
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论