1、iText簡介
iText是壹個開放源碼的Java類庫,可以用來方便地生成PDF文件。大家通過訪問/iText/tutorial/index.html
有比較詳細的教程。該教程從入門開始,比較系統地介紹了在PDF文件中放入文字、圖片、表格等的方法和技巧。
讀完這片教程,大致就可以做壹些從簡單到復雜的PDF文件了。不過,試圖通過教程解決在生成PDF文件過程中遇到的所有困難無疑是壹種奢望。所以,閱讀iText的api文檔顯得非常重要。讀者在下載類庫的同時,也可以下載類庫的文檔。
註:如果以上兩個下載鏈接無法下載而且通過網絡也找不到這個jar包的同誌可以留下郵箱地址,我會在兩個工作日之內發郵件過去。
以下部分我是我調試通過的源代碼,提供大家參考:
import java.awt.*;
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import
com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.*;
/**
* 最近的項目中使用Itext將txt文件轉換為PDF文件, 並且實現對文件的壹些權限控制。
現實對pdf文件加
*密,添加水印等。
*/
public class PDFConvertBL
{
//
txt原始文件的路徑
private static final String txtFilePath = "d:/11.txt";
// 生成的pdf文件路徑
private static final String pdfFilePath =
"d:/22.pdf";
// 添加水印圖片路徑
// private static final String
imageFilePath = "D:/33.jpg";
// 生成臨時文件前綴
private static final
String prefix = "tempFile";
// 所有者密碼
private static final String
OWNERPASSWORD = "12345678";
/**
* txt文件轉換為pdf文件
*
* @param txtFile
txt文件路徑
* @param pdfFile pdf文件路徑
* @param userPassWord
用戶密碼
* @param waterMarkName 水印內容
* @param permission
操作權限
*/
public static void generatePDFWithTxt(String txtFile,
String pdfFile, String userPassWord, String
waterMarkName,
int permission)
{
try
{
// 生成臨時文件
File file =
File.createTempFile(prefix, ".pdf");
//
創建pdf文件到臨時文件
if (createPDFFile(txtFile, file))
{
// 增加水印和加密
waterMark(file.getPath(),
pdfFile, userPassWord, OWNERPASSWORD, waterMarkName, permission);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 創建PDF文檔
*
* @param txtFilePath
txt文件路徑(源文件)
* @param pdfFilePath pdf文件路徑(新文件)
*/
private
static boolean createPDFFile(String txtFilePath, File file)
{
// 設置紙張
Rectangle rect = new Rectangle(PageSize.A4);
//
設置頁碼
HeaderFooter footer = new HeaderFooter(new Phrase("頁碼:",
setChineseFont()), true);
footer.setBorder(Rectangle.NO_BORDER);
// step1
Document
doc = new Document(rect, 50, 50, 50, 50);
doc.setFooter(footer);
try
{
FileReader
fileRead = new FileReader(txtFilePath);
BufferedReader read = new
BufferedReader(fileRead);
// 設置pdf文件生成路徑 step2
PdfWriter.getInstance(doc, new FileOutputStream(file));
//
打開pdf文件 step3
doc.open();
// 實例化Paragraph
獲取寫入pdf文件的內容,調用支持中文的方法. step4
while (read.ready())
{
// 添加內容到pdf(這裏將會按照txt文件的原始樣式輸出)
doc.add(new Paragraph(read.readLine(), setChineseFont()));
}
// 關閉pdf文件 step5
doc.close();
return true;
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
}
/**
* 在pdf文件中添加水印
*
* @param inputFile
原始文件
* @param outputFile 水印輸出文件
* @param waterMarkName
水印名字
*/
private static void waterMark(String inputFile, String
outputFile, String userPassWord, String ownerPassWord,
String waterMarkName, int permission)
{
try
{
PdfReader reader = new PdfReader(inputFile);
PdfStamper stamper = new PdfStamper(reader, new
FileOutputStream(outputFile));
// 設置密碼
stamper.setEncryption(userPassWord.getBytes(), ownerPassWord.getBytes(),
permission, false);
BaseFont base =
BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.NOT_EMBEDDED);
int total = reader.getNumberOfPages() +
1;
// Image image =
Image.getInstance(imageFilePath);
//
image.setAbsolutePosition(200, 400);
PdfContentByte
under;
int j = waterMarkName.length();
char c =
0;
int rise = 0;
for (int i = 1; i < total;
i++)
{
rise = 500;
under =
stamper.getUnderContent(i);
// 添加圖片
//
under.addImage(image);
under.beginText();
under.setColorFill(Color.CYAN);
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 +
"");
rise -= 20;
}
}
else
{
under.setTextMatrix(180, 100);
for (int k = 0; k < j; k++)
{
under.setTextRise(rise);
c = waterMarkName.charAt(k);
under.showText(c +
"");
rise -= 18;
}
}
// 字體設置結束
under.endText();
// 畫壹個圓
//
under.ellipse(250, 450, 350, 550);
//
under.setLineWidth(1f);
// under.stroke();
}
stamper.close();
}
catch (Exception
e)
{
e.printStackTrace();
}
}
/**
* 設置中文
*
* @return Font
*/
private static Font setChineseFont()
{
BaseFont base =
null;
Font fontChinese = null;
try
{
base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
BaseFont.EMBEDDED);
fontChinese = new Font(base, 12,
Font.NORMAL);
}
catch (DocumentException e)
{
e.printStackTrace();
}
catch (IOException
e)
{
e.printStackTrace();
}
return fontChinese;
}
public static void main(String[] args)
{
generatePDFWithTxt(txtFilePath, pdfFilePath, "123", "水印文字", 16);
}
}
文章參考壹些網絡博客稍加修改調試,特此申明
/sx_python/item/15081531ad7d1bc21b96965e