(一)创建一个简单的PDF文件 pdf_file
package com.pdf.file;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Test {
public static void main(String[] args) throws FileNotFoundException, DocumentException {
// 1.新建document对象
Document document = new Document();
// 2.建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中。
// 创建 PdfWriter 对象 第一个参数是对文档对象的引用,第二个参数是文件的实际名称,在该名称中还会给出其输出路径。
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/test.pdf"));
// 3.打开文档
document.open();
// 4.添加一个内容段落
document.add(new Paragraph("Hello Everyone!we are family"));
// 5.关闭文档
document.close();
}
}
(二)给PDF文件设置文件属性 pdf_content
package com.pdf.content;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Test {
public static void main(String[] args) throws FileNotFoundException, DocumentException {
// 创建文件
Document document = new Document();
// 建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/content.pdf"));
// 打开文件
document.open();
// 添加内容
document.add(new Paragraph("Some content here"));
// 设置属性
// 标题
document.addTitle("this is a title");
// 作者
document.addAuthor("H__D");
// 主题
document.addSubject("this is subject");
// 关键字
document.addKeywords("Keywords");
// 创建时间
document.addCreationDate();
// 应用程序
document.addCreator("hd.com");
// 关闭文档
document.close();
// 关闭书写器
writer.close();
}
}
(三)PDF中添加图片 pdf_image
package com.pdf.image;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class Test {
public static void main(String[] args) throws DocumentException, IOException {
// 创建文件
Document document = new Document();
// 建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("E:/image/chanpin/22.pdf"));
// 打开文件
document.open();
// 添加内容
document.add(new Paragraph("my image"));
// 图片
Image image= Image.getInstance("E:/image/chanpin/22.jpg");
// 设置图片位置的x轴和y周
image.setAbsolutePosition(100f, 550f);
// 设置图片的宽度和高度
image.scaleAbsolute(200, 200);
// 将图片添加到pdf文件中
document.add(image);
// 关闭文档
document.close();
// 关闭书写器
writer.close();
}
}
(四)PDF中创建表格 pdf_table
package com.pdf.table;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfPCell;
import com.lowagie.text.pdf.PdfPRow;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
public class Test {
public static void main(String[] args) throws DocumentException, FileNotFoundException {
// 创建文件
Document document = new Document();
// 建立一个书写器
PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("E:/image/table.pdf"));
// 打开文件
document.open();
// 添加内容
document.add(new Paragraph("-----------------table----------------"));
// 3列的表
PdfPTable table = new PdfPTable(3);
table.setWidthPercentage(100); // 宽度100%填充
table.setSpacingBefore(10f); // 前间距
table.setSpacingAfter(10f); // 后间距
List<PdfPRow> listRow = table.getRows();
// 设置列宽
float[] columnWidths = { 2f, 2f, 2f };
table.setWidths(columnWidths);
// 行1
PdfPCell cells1[] = new PdfPCell[3];
PdfPRow row1 = new PdfPRow(cells1);
// 单元格
// 单元格内容
cells1[0] = new PdfPCell(new Paragraph("name"));
// 边框验证
// cells1[0].setBorderColor(BaseColor.BLUE);
// 左填充20
cells1[0].setPaddingLeft(20);
// 水平居中
cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells1[1] = new PdfPCell(new Paragraph("pwd"));
// 左填充20
cells1[1].setPaddingLeft(20);
// 水平居中
cells1[1].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells1[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells1[2] = new PdfPCell(new Paragraph("birthday"));
// 左填充20
cells1[2].setPaddingLeft(20);
// 水平居中
cells1[2].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells1[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 行2
PdfPCell cells2[] = new PdfPCell[3];
PdfPRow row2 = new PdfPRow(cells2);
// 单元格内容
cells2[0] = new PdfPCell(new Paragraph("zhangsan"));
// 左填充20
cells2[0].setPaddingLeft(20);
// 水平居中
cells2[0].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells2[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells2[1] = new PdfPCell(new Paragraph("666"));
// 左填充20
cells2[1].setPaddingLeft(20);
// 水平居中
cells2[1].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells2[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells2[2] = new PdfPCell(new Paragraph("1993-02-05"));
// 左填充20
cells2[2].setPaddingLeft(20);
// 水平居中
cells2[2].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells2[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 行3
PdfPCell cells3[] = new PdfPCell[3];
PdfPRow row3 = new PdfPRow(cells3);
// 单元格内容
cells3[0] = new PdfPCell(new Paragraph("lisi"));
// 左填充20
cells3[0].setPaddingLeft(20);
// 水平居中
cells3[0].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells3[0].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells3[1] = new PdfPCell(new Paragraph("222"));
// 左填充20
cells3[1].setPaddingLeft(20);
// 水平居中
cells3[1].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells3[1].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 单元格内容
cells3[2] = new PdfPCell(new Paragraph("1993-02-05"));
// 左填充20
cells3[2].setPaddingLeft(20);
// 水平居中
cells3[2].setHorizontalAlignment(Element.ALIGN_CENTER);
// 垂直居中
cells3[2].setVerticalAlignment(Element.ALIGN_MIDDLE);
// 把第一行添加到集合
listRow.add(row1);
listRow.add(row2);
listRow.add(row3);
// 把表格添加到文件中
document.add(table);
// 关闭文档
document.close();
// 关闭书写器
writer.close();
}
}
(五)读取/修改已有PDF文件 pdf_reader
package com.pdf.reader;
import java.io.FileOutputStream;
import java.io.IOException;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Image;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
public class Test {
public static void main(String[] args) throws DocumentException, IOException {
// 读取pdf文件
PdfReader pdfReader = new PdfReader("E:/image/test.pdf");
// 修改器
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileOutputStream("E:/image/reader.pdf"));
Image image = Image.getInstance("E:/image/watermark.png");
image.scaleAbsolute(50, 50);
image.setAbsolutePosition(0, 700);
for (int i = 1; i <= pdfReader.getNumberOfPages(); i++) {
PdfContentByte content = pdfStamper.getUnderContent(i);
content.addImage(image);
}
pdfStamper.close();
}
}
(六)PDF水印 pdf_watermark
package com.pdf.mark;
import java.io.*;
import com.lowagie.text.Document;
import com.lowagie.text.Font;
import com.lowagie.text.Image;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfContentByte;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfStamper;
import com.lowagie.text.pdf.PdfWriter;
/**
* PDF生成
*/
public class PDFBuild {
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文件
*
*/
public static boolean createPDFFile(File file) {
Rectangle rect = new Rectangle(PageSize.A4);
Document doc = new Document(rect, 50.0F, 50.0F, 50.0F, 50.0F);
try {
PdfWriter.getInstance(doc, new FileOutputStream(file));
doc.open();
// 使用系统字体
BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", "Identity-H", true);
Font font = new Font(bf, 20.0F, 0);
// 设置样式
font.setStyle(37);
// 设置字体
font.setFamily("宋体");
Paragraph p = new Paragraph("我的水印图\r\n", font);
p.setAlignment(1);
doc.add(p);
doc.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
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(100, 280);
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);
}
stamper.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
// 水印图片路径
String imageFilePath = "E:/image/watermark.png";
// 文件生成路径
String pdfFilePath = "E:/image/watermark.pdf";
buidPDF(pdfFilePath, imageFilePath, "正版授权", 20);
}
}
原文:http://www.cnblogs.com/baobao1234/p/6438343.html