1和iText簡介
IText是壹個開源的Java類庫,可以用來方便地生成PDF文件。可以訪問/iText/iText/tutorial/index . html
還有更詳細的教程。本課程從壹開始就系統地介紹了將文字、圖片、表格放入PDF文件的方法和技巧。
看完這個教程,妳可以把壹些PDF文件從簡單到復雜。但是,試圖通過教程來解決PDF文件生成過程中遇到的所有困難,無疑是壹種奢望。因此,閱讀iText的api文檔非常重要。讀者可以同時下載類庫的文檔。
註:如果以上兩個下載鏈接下載不到,通過網絡找不到這個jar包的同誌可以留下郵箱,我會在兩個工作日內發郵件給他們。
以下部分是我調試通過的源代碼,供大家參考:
導入Java . awt . *;
導入Java . io . *;
導入com . lowa gie . text . *;
導入com . lowa gie . text . font;
進口
com . lowa gie . text . rectangle;
導入com . lowa gie . text . pdf . *;
/**
*在最近的項目中,使用Itext將txt文件轉換成PDF文件,實現了對文件的壹些權限控制。
現實添加到pdf文件
*保密,加水印等。
*/
公共類PDFConvertBL
{
//
txt原始文件的路徑。
私有靜態最終字符串txtFilePath = " d:/11 . txt ";
//生成的pdf文件的路徑
私有靜態最終字符串pdfFilePath =
”d:/22 . pdf”;
//添加水印圖像路徑
//私有靜態最終字符串
imagefile path = " D:/33 . jpg ";
//生成臨時文件前綴
私有靜態最終
String prefix = " tempFile
//所有者密碼
私有靜態最終字符串
owner password = " 12345678 ";
/**
* txt文件被轉換成pdf文件。
*
* @param txtFile
Txt文件路徑
* @param pdfFile pdf文件路徑
* @param用戶密碼
用戶口令
* @param waterMarkName水印內容
* @param權限
運輸許可證
*/
公共靜態void generatedpfwithtxt(String txt file,
字符串pdf文件,字符串用戶密碼,字符串
水印名稱,
int權限)
{
嘗試
{
//生成壹個臨時文件
文件文件=
File.createTempFile(前綴。pdf”);
//
將pdf文件創建為臨時文件
if (createPDFFile(txtFile,File))
{
//添加水印和加密
水印(file.getPath(),
pdfFile、userPassWord、OWNERPASSWORD、waterMarkName、permission);
}
}
捕捉(例外e)
{
e . printstacktrace();
}
}
/**
*創建PDF文檔
*
* @param txtFilePath
Txt文件路徑(源文件)
* @param pdfFilePath pdf文件路徑(新文件)
*/
私人的
靜態布爾createPDFFile(字符串txtFilePath,文件File)
{
//設置紙張
Rectangle rect =新矩形(PageSize。a4);
//
設置頁碼
header footer footer = new header footer(新短語("頁碼:",
setChineseFont()),true);
footer.setBorder(矩形。NO _ BORDER);
//步驟1
文件
doc =新文檔(rect,50,50,50,50);
doc.setFooter(頁腳);
嘗試
{
文件閱讀器
fileRead = new file reader(txtFilePath);
BufferedReader read = new
buffered reader(fileRead);
//設置pdf文件生成路徑step2
PdfWriter.getInstance(doc,new file output stream(file));
//
打開pdf文件步驟3。
doc . open();
//實例化段落
獲取pdf文件中寫的內容,調用支持中文的方法。第四步
while (read.ready())
{
//將內容添加到pdf中(這裏會按照txt文件的原始樣式輸出)
doc.add(新段落(read.readLine()、setChineseFont()));
}
//關閉pdf文件步驟5。
doc . close();
返回true
}
捕捉(例外e)
{
e . printstacktrace();
返回false
}
}
/**
*給pdf文件添加水印。
*
* @param輸入文件
正本單據
* @param outputFile水印輸出文件
* @param waterMarkName
水印名稱
*/
私有靜態void水印(字符串輸入文件,字符串
輸出文件,字符串用戶密碼,字符串所有者密碼,
字符串waterMarkName,int權限)
{
嘗試
{
pdf reader reader = new pdf reader(input file);
PdfStamper stamper =新PdfStamper(讀取器,新
FileOutputStream(輸出文件));
//設置密碼
stamper . set encryption(user password . getbytes()、ownerPassWord.getBytes(),
許可,假);
BaseFont base =
base font . create font(" ST song-Light "," UniGB-UCS2-H ",
基本字體。未_嵌入);
int total = reader . getnumberofpages()+
1;
// Image image =
image . getinstance(image file path);
//
image.setAbsolutePosition(200,400);
PdfContentByte
下;
int j = water mark name . length();
char c =
0;
int rise = 0;
for(int I = 1;我& lt總計;
i++)
{
上升= 500;
低於=
stamper . getundercontent(I);
//添加圖片
//
under.addImage(圖片);
under . begin text();
under.setColorFill(顏色。青色);
under.setFontAndSize(base,
30);
//設置水印字體傾斜開始。
if(j & gt;=
15)
{
under.setTextMatrix(200,
120);
for(int k = 0;k & ltj;
k++)
{
under.setTextRise(上升);
c =
water mark name . charat(k);
under.showText(c +
"");
上升-= 20;
}
}
其他
{
under.setTextMatrix(180,100);
for(int k = 0;k & ltj;k++)
{
under.setTextRise(上升);
c = water mark name . charat(k);
under.showText(c +
"");
rise-= 18;
}
}
//字體設置結束
under . end text();
//畫壹個圓
//
橢圓下(250,450,350,550);
//
under . set line width(1f);
//under . stroke();
}
stamper . close();
}
catch(異常
e)
{
e . printstacktrace();
}
}
/**
*設置中文
*
* @返回字體
*/
私有靜態字體setChineseFont()
{
BaseFont base =
null
Font fontChinese = null
嘗試
{
base = base font . create font(" ST song-Light "," UniGB-UCS2-H ",
基本字體。嵌入式);
fontChinese =新字體(base,12,
字體。正常);
}
catch(文檔異常e)
{
e . printstacktrace();
}
catch (IOException
e)
{
e . printstacktrace();
}
返回fontChinese
}
公共靜態void main(String[] args)
{
生成epdfwithtxt (txtfile路徑,pdf文件路徑,“123”,“水印文本”,16);
}
}
參考壹些網絡博客,文章稍加修改調試,特此聲明。
/sx _ python/item/15081531ad 7d 1bc 21b 96965 e