itext 生成 PDF(一)

itext 生成 PDF(二)

官網(wǎng):http://itextsupport.com/apidocs/itext5/latest/

博文:https://blog.csdn.net/u010154380/article/details/78087663

博文:https://blog.csdn.net/u013129932/article/details/43889705

iText是著名的開放源碼的站點(diǎn)sourceforge一個項目,是用于生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉(zhuǎn)化為PDF文件。

項目要使用iText,必須引入jar包。才能使用,maven依賴如下:

<dependency><groupId>com.itextpdf</groupId><artifactId>itextpdf</artifactId><version>5.5.10</version></dependency>

輸出中文,還要引入下面itext-asian.jar包:

<dependency><groupId>com.itextpdf</groupId><artifactId>itext-asian</artifactId><version>5.2.0</version></dependency>

設(shè)置pdf文件密碼,還要引入下面bcprov-jdk15on.jar包:

<dependency><groupId>org.bouncycastle</groupId><artifactId>bcprov-jdk15on</artifactId><version>1.54</version></dependency>

iText常用類

com.itextpdf.text.Document:這是iText庫中最常用的類,它代表了一個pdf實例。如果你需要從零開始生成一個PDF文件,你需要使用這個Document類。首先創(chuàng)建(new)該實例,然后打開(open)它,并添加(add)內(nèi)容,最后關(guān)閉(close)該實例,即可生成一個pdf文件。

com.itextpdf.text.Paragraph:表示一個縮進(jìn)的文本段落,在段落中,你可以設(shè)置對齊方式,縮進(jìn),段落前后間隔等

com.itextpdf.text.Chapter:表示PDF的一個章節(jié),他通過一個Paragraph類型的標(biāo)題和整形章數(shù)創(chuàng)建

com.itextpdf.text.Font:這個類包含了所有規(guī)范好的字體,包括family of font,大小,樣式和顏色,所有這些字體都被聲明為靜態(tài)常量

com.itextpdf.text.List:表示一個列表;

com.itextpdf.text.Anchor:表示一個錨,類似于HTML頁面的鏈接。

com.itextpdf.text.pdf.PdfWriter:當(dāng)這個PdfWriter被添加到PdfDocument后,所有添加到Document的內(nèi)容將會寫入到與文件或網(wǎng)絡(luò)關(guān)聯(lián)的輸出流中。

com.itextpdf.text.pdf.PdfReader:用于讀取PDF文件;

iText使用

創(chuàng)建一個簡單的pdf文件,如下:

packagecom.hd.pdf;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importcom.itextpdf.text.Document;importcom.itextpdf.text.DocumentException;importcom.itextpdf.text.Paragraph;importcom.itextpdf.text.pdf.PdfWriter;publicclassTestPDFDemo1{publicstaticvoidmain(String[]args)throws FileNotFoundException,DocumentException{// 1.新建document對象Document document=newDocument();// 2.建立一個書寫器(Writer)與document對象關(guān)聯(lián),通過書寫器(Writer)可以將文檔寫入到磁盤中。// 創(chuàng)建 PdfWriter 對象 第一個參數(shù)是對文檔對象的引用,第二個參數(shù)是文件的實際名稱,在該名稱中還會給出其輸出路徑。PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test.pdf"));// 3.打開文檔document.open();// 4.添加一個內(nèi)容段落document.add(newParagraph("Hello World!"));// 5.關(guān)閉文檔document.close();}}

打開文件

851491-20161209165247147-746087588.png

PDF中創(chuàng)建表格

publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//創(chuàng)建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test4.pdf"));//打開文件document.open();//添加內(nèi)容document.add(newParagraph("HD content here"));// 3列的表.PdfPTable table=newPdfPTable(3);table.setWidthPercentage(100);// 寬度100%填充table.setSpacingBefore(10f);// 前間距table.setSpacingAfter(10f);// 后間距List<PdfPRow>listRow=table.getRows();//設(shè)置列寬float[]columnWidths={1f,2f,3f};table.setWidths(columnWidths);//行1PdfPCell cells1[]=newPdfPCell[3];PdfPRow row1=newPdfPRow(cells1);//單元格cells1[0]=newPdfPCell(newParagraph("111"));//單元格內(nèi)容cells1[0].setBorderColor(BaseColor.BLUE);//邊框驗證cells1[0].setPaddingLeft(20);//左填充20cells1[0].setHorizontalAlignment(Element.ALIGN_CENTER);//水平居中cells1[0].setVerticalAlignment(Element.ALIGN_MIDDLE);//垂直居中cells1[1]=newPdfPCell(newParagraph("222"));cells1[2]=newPdfPCell(newParagraph("333"));//行2PdfPCell cells2[]=newPdfPCell[3];PdfPRow row2=newPdfPRow(cells2);cells2[0]=newPdfPCell(newParagraph("444"));//把第一行添加到集合listRow.add(row1);listRow.add(row2);//把表格添加到文件中document.add(table);//關(guān)閉文檔document.close();//關(guān)閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

給PDF文件設(shè)置文件屬性,例如:

publicstaticvoidmain(String[]args)throws FileNotFoundException,DocumentException{//創(chuàng)建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test2.pdf"));//打開文件document.open();//添加內(nèi)容document.add(newParagraph("Some content here"));//設(shè)置屬性//標(biāo)題document.addTitle("this is a title");//作者document.addAuthor("H__D");//主題document.addSubject("this is subject");//關(guān)鍵字document.addKeywords("Keywords");//創(chuàng)建時間document.addCreationDate();//應(yīng)用程序document.addCreator("hd.com");//關(guān)閉文檔document.close();//關(guān)閉書寫器writer.close();}

打開文件

851491-20161209165247147-746087588.png

PDF中添加圖片

publicstaticvoidmain(String[]args)throws DocumentException,IOException{//創(chuàng)建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test3.pdf"));//打開文件document.open();//添加內(nèi)容document.add(newParagraph("HD content here"));//圖片1Image image1=Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");//設(shè)置圖片位置的x軸和y周image1.setAbsolutePosition(100f,550f);//設(shè)置圖片的寬度和高度image1.scaleAbsolute(200,200);//將圖片1添加到pdf文件中document.add(image1);//圖片2Image image2=Image.getInstance(newURL("http://static.cnblogs.com/images/adminlogo.gif"));//將圖片2添加到pdf文件中document.add(image2);//關(guān)閉文檔document.close();//關(guān)閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

PDF中創(chuàng)建列表

publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//創(chuàng)建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test5.pdf"));//打開文件document.open();//添加內(nèi)容document.add(newParagraph("HD content here"));//添加有序列表List orderedList=newList(List.ORDERED);orderedList.add(newListItem("Item one"));orderedList.add(newListItem("Item two"));orderedList.add(newListItem("Item three"));document.add(orderedList);//關(guān)閉文檔document.close();//關(guān)閉書寫器writer.close();}

打開文件

851491-20161209180029726-1168732515.png

PDF中設(shè)置樣式/格式化輸出,輸出中文內(nèi)容,必須引入itext-asian.jar

publicstaticvoidmain(String[]args)throws DocumentException,IOException{//創(chuàng)建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test6.pdf"));//打開文件document.open();//中文字體,解決中文不能顯示問題BaseFont bfChinese=BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);//藍(lán)色字體Font blueFont=newFont(bfChinese);blueFont.setColor(BaseColor.BLUE);//段落文本Paragraph paragraphBlue=newParagraph("paragraphOne blue front",blueFont);document.add(paragraphBlue);//綠色字體Font greenFont=newFont(bfChinese);greenFont.setColor(BaseColor.GREEN);//創(chuàng)建章節(jié)Paragraph chapterTitle=newParagraph("段落標(biāo)題xxxx",greenFont);Chapter chapter1=newChapter(chapterTitle,1);chapter1.setNumberDepth(0);Paragraph sectionTitle=newParagraph("部分標(biāo)題",greenFont);Section section1=chapter1.addSection(sectionTitle);Paragraph sectionContent=newParagraph("部分內(nèi)容",blueFont);section1.add(sectionContent);//將章節(jié)添加到文章中document.add(chapter1);//關(guān)閉文檔document.close();//關(guān)閉書寫器writer.close();}

打開圖片

![

851491-20161209180029726-1168732515.png

]

851491-20161209165247147-746087588.png

給PDF文件設(shè)置密碼,需要引入bcprov-jdk15on.jar包:

publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 創(chuàng)建文件Document document=newDocument();// 建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test8.pdf"));//用戶密碼String userPassword="123456";//擁有者密碼String ownerPassword="hd";writer.setEncryption(userPassword.getBytes(),ownerPassword.getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打開文件document.open();//添加內(nèi)容document.add(newParagraph("password !!!!"));// 關(guān)閉文檔document.close();// 關(guān)閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

給PDF文件設(shè)置權(quán)限

publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 創(chuàng)建文件Document document=newDocument();// 建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test9.pdf"));// 只讀權(quán)限writer.setEncryption("".getBytes(),"".getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打開文件document.open();// 添加內(nèi)容document.add(newParagraph("password !!!!"));// 關(guān)閉文檔document.close();// 關(guān)閉書寫器writer.close();}

讀取/修改已有的PDF文件

publicstaticvoidmain(String[]args)throwsDocumentException,IOException{//讀取pdf文件PdfReaderpdfReader=newPdfReader("C:/Users/H__D/Desktop/test1.pdf");//修改器PdfStamperpdfStamper=newPdfStamper(pdfReader,newFileOutputStream("C:/Users/H__D/Desktop/test10.pdf"));Imageimage=Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");image.scaleAbsolute(50,50);image.setAbsolutePosition(0,700);for(inti=1;i<=pdfReader.getNumberOfPages();i++){PdfContentBytecontent=pdfStamper.getUnderContent(i);content.addImage(image);}pdfStamper.close();}

itext? 生成 PDF(二)

作者:lconcise

鏈接:http://www.itdecent.cn/p/20d4905383b4

來源:簡書

著作權(quán)歸作者所有。商業(yè)轉(zhuǎn)載請聯(lián)系作者獲得授權(quán),非商業(yè)轉(zhuǎn)載請注明出處。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • iText介紹 ? 轉(zhuǎn)自https://www.cnblogs.com/h--d/p/6150320.html...
    f6bd4b10e9c5閱讀 1,391評論 0 0
  • 下面是20個非常有用的Java程序片段,希望能對你有用。 1. 字符串有整型的相互轉(zhuǎn)換 Java代碼 String...
    高級java架構(gòu)師閱讀 588評論 0 1
  • 先收藏了,以后應(yīng)該用的到。 下面是20個非常有用的Java程序片段,希望能對你有用。 1. 字符串有整型的相互轉(zhuǎn)換...
    尹興飛閱讀 201評論 0 0
  • 大家好,我是傻明蠶豆,最近搞了一個html轉(zhuǎn)pdf,在這里把知識記錄下來,希望對大家有幫助。 廢話不多說,直奔主題...
    傻明蠶豆閱讀 1,481評論 0 2
  • 久違的晴天,家長會。 家長大會開好到教室時,離放學(xué)已經(jīng)沒多少時間了。班主任說已經(jīng)安排了三個家長分享經(jīng)驗。 放學(xué)鈴聲...
    飄雪兒5閱讀 7,818評論 16 22

友情鏈接更多精彩內(nèi)容