導航:首頁 > 文件類型 > itextwordpdf文件

itextwordpdf文件

發布時間:2023-04-28 14:16:47

A. itext pdf文檔生成

在日常開發過程中,若是想要把java中的某個對象中的內容,迅衡按某種排列方式輸出到PDF中,有幾種方式,此處只缺稿使用了itextpdf的方式。
itext pdf 文檔

在gradle中伏昌孝只需要加入

新建一個Document對象,再定義PdfWriter,把document中的內容寫入HelloWorld.pdf文件
在添加內容到document之前需要執行document.open()才可以。
document可以添加Paragraph、 PdfPTable 等元素。

這兩種方法中第一種不支持設置Leading為100,第二種方法支持設置Leading為100。除了在初始化Paragraph時可以設置Leading,還可以調用setLeading()來設置,第一個參數是固定的行間距,第二個參數是行間距為行高的倍數。

B. itext 生成 PDF(一)

itext 生成 PDF(二)

官網: http://itextsupport.com/apidocs/itext5/latest/

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

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

iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為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>

設置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類。首先創建(new)該實例,然後打開(open)它,並添加(add)內容,最後關閉(close)該實例,即可生成一個pdf文件。

com.itextpdf.text.Paragraph:表示一個縮進的文本段落,在段落中,你可以設置對齊方式,縮進,段落前後間隔等

com.itextpdf.text.Chapter:表示PDF的一個章節,他通過一個Paragraph類型的標題和整形章數創建

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

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

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

com.itextpdf.text.pdf.PdfWriter:當這個PdfWriter被添加到PdfDocument後,所有添嘩斗加世鉛到Document的內容將會寫入到與文件或網路關聯的輸出流中。

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

iText使用

創建一個簡單的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對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。// 創建 PdfWriter 對象 第一個參數是對文檔對象的引用,第二個參數是文件的實際名稱,在該名稱中還會給出其輸出路徑。PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test.pdf"));// 3.打開文檔document.open();// 4.添加一個內容段落document.add(newParagraph("Hello World!"));// 5.關閉文檔document.close();}}

打開文件

851491-20161209165247147-746087588.png

PDF中創建表格

publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//創建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test4.pdf"));//打開文件document.open();//添加內容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();//設置列寬float[]columnWidths={1f,2f,3f};table.setWidths(columnWidths);//行1PdfPCell cells1[]=newPdfPCell[3];PdfPRow row1=newPdfPRow(cells1);//單元格cells1[0]=newPdfPCell(newParagraph("111"));//單元格內容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);//關閉文檔document.close();//關閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

給PDF文件設置文件屬性,例如:

publicstaticvoidmain(String[]args)throws FileNotFoundException,DocumentException{//創建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test2.pdf"));//打開文件document.open();//添加內容document.add(newParagraph("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();}

打開文件

851491-20161209165247147-746087588.png

PDF中添加圖片

publicstaticvoidmain(String[]args)throws DocumentException,IOException{//創建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test3.pdf"));//打開文件document.open();//添加內容document.add(newParagraph("HD content here"));//圖片1Image image1=Image.getInstance("C:/Users/H__D/Desktop/IMG_0109.JPG");//設置圖片位置的x軸和y周image1.setAbsolutePosition(100f,550f);//設置圖片的寬度和高度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);//關閉文檔document.close();//關閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

PDF中創建列表

publicstaticvoidmain(String[]args)throws DocumentException,FileNotFoundException{//創建文件Document document=newDocument();//建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test5.pdf"));//打開文件document.open();//添加內容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);//關閉文檔document.close();//關閉書寫器writer.close();}

打開文件

851491-20161209180029726-1168732515.png

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

publicstaticvoidmain(String[]args)throws DocumentException,IOException{//創建文件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);//藍色字體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);//創建章節Paragraph chapterTitle=newParagraph("段落標題xxxx",greenFont);Chapter chapter1=newChapter(chapterTitle,1);chapter1.setNumberDepth(0);Paragraph sectionTitle=newParagraph("部分標題",greenFont);Section section1=chapter1.addSection(sectionTitle);Paragraph sectionContent=newParagraph("部分內容",blueFont);section1.add(sectionContent);//將章節添加到文章中document.add(chapter1);//關閉文檔document.close();//關閉書寫器writer.close();}

打開圖片

![

851491-20161209180029726-1168732515.png

]

851491-20161209165247147-746087588.png

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

publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 創建文件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();//添加內容document.add(newParagraph("password !!!!"));// 關閉文檔document.close();// 關閉書寫器writer.close();}

打開圖片

851491-20161209165247147-746087588.png

給PDF文件設置許可權

publicstaticvoidmain(String[]args)throws DocumentException,IOException{// 創建文件Document document=newDocument();// 建立一個書寫器PdfWriter writer=PdfWriter.getInstance(document,newFileOutputStream("C:/Users/H__D/Desktop/test9.pdf"));// 只讀許可權writer.setEncryption("".getBytes(),"".getBytes(),PdfWriter.ALLOW_PRINTING,PdfWriter.ENCRYPTION_AES_128);// 打開文件document.open();// 添加內容document.add(newParagraph("password !!!!"));// 關閉文檔document.close();// 關閉書寫器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(二)

鏈接:https://www.jianshu.com/p/20d4905383b4

C. 怎樣用iText讀取pdf文件

用iText讀取pdf文件舉例: (1)在Eclipse中新建一個Java工程。 (2)下載相應的iText-5.0.2.jar並放到對應的lib目錄下。在工程坦宏枝中創建包並創建測試類,該類包含一個inspect方法用於從一個PDF中獲取文本,它接受兩個參數,分別是PDF文件路徑和輸出流,指定要提取的PDF文件的路徑和讀取PDF所用的輸出流,比如:PDF路徑為E://text.pdf。然後調用iText提供的PdfReader類和PdfTextExtractor類,將PDF格式的文本提取出來並寫入txt文件中。部分代碼如下: import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import com.itextpdf.text.DocumentException; import com.itextpdf.text.pdf.PdfReader; import com.itextpdf.text.pdf.parser.PdfTextExtractor; public class PDF { /** The resulting text file with info about a PDF. */ public static final String RESULT = "d:/ceshi.txt";//存放由pdf轉換成txt文件的路徑。 /** * Main method. * @param args no arguments needed * @throws DocumentException * @throws IOException */ public static void main(String[] args) throws DocumentException, IOException { PrintWriter writer = new PrintWriter(new FileOutputStream(RESULT));//txt文件寫入流 String string = "E:/text.pdf";//pdf文件路徑 inspect(writer,string); //調用讀取方法 writer.close(); } /** * Inspect a PDF file and write the info to a txt file * @param writer Writer to a text file * @param filename Path to the PDF file * @throws IOException */ public static void inspect(PrintWriter writer, String filename) throws IOException { PdfReader reader = new PdfReader(filename); //讀取pdf所使用的輸讓敏出流 int num = reader.getNumberOfPages();//獲得頁數 String content = ""; //存放讀取出的絕攜文檔內容 for (int i = 1; i < num; i++) { content += PdfTextExtractor.getTextFromPage(reader, i); //讀取第i頁的文檔內容 } writer.write(content);//寫入文件內容 writer.flush(); } }

D. Itextpdf 給pdf加密跟限制許可權怎麼解決

方法一:如果文檔做保護時沒有設置密碼,找 acrobat pro設置為允許列印就可以了,如果設置了密碼,就只能用暴力破解軟體破解了,如果密碼比較長就很難破解出來

方法二,用帶OCR功能的轉換器,比如abbyy fine reader、solid pdf converter進行轉換,轉換為word文檔就可以列印了,但嚴重依賴於圖片的掃描質量,另外公式、圖表比較多的也比較難識別;
方法三:1)如果不是什麼重要文件,網路中可以找到的很多的話,建議下載別的類似PDF文件試試,雖然PDF可能會出現以上原作者限制的情況,但並不是很多數,因此如果方便,建議重新查找類似內容PDF文件再列印。
2)如果是該文件很難找,則也是有辦法的,不過我們需要藉助一款軟體:PDFSpliter。
PDFSpliter是一款十分熱門的軟體,主要功能是將多頁大PDF,拆分成多個單頁小PDF格式,並且可以可設置多個監測文件夾,自動批量轉換。
破解pdf文件不能列印的解決辦法如下:首先打開PDFSpliter軟體,找到工具欄的「pdf合並」,打開無法列印的pdf文件,點擊「打開」,之後點擊「開始」合並。
等待PDF文件合並之後,我們將合並後的新文件另存為一個桌面,建議放在桌面上,之後再到桌面上找到該文件即可,之後我們去列印這個合並後的PDF文件會發現已經不受版權等口令限制,可以正常列印了,有興趣的朋友不妨參考下。
其實這里用到是PDF文件合並工具,由於原始文件因為版權問題,原作者對文檔進行諸如修改與列印限制,導致無法正常列印,不過我們藉助專業的PDF文件合並軟體,進行合並一下即可解決此類問題,當然大家也可以使用pdf recover、Alt PDF Password Recovery等工具來實現。

E. 如何運用Java組件itext生成pdf

如何使用Java組件itext 生成pdf?iText是一個能夠快速產生PDF文件的java類庫。iText的java類對於那些要產生包含文本,表格,圖形的只讀文檔是很有用的。它的類庫尤其與java Servlet有很好的給合。使用iText與PDF能夠使你正確的控制Servlet的輸出。

在企業的信息系統中,報表處理一直佔比較重要的作用,Java組件——iText可以非常快速的生成PDF報表。iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、 Html文件轉化為PDF文件。通過在伺服器端使用Jsp或JavaBean生成PDF報表,客戶端採用超級連接顯示或下載得到生成的報表,這樣就很好的解決了B/S系統的報表處理問題了。
一、iText介紹
iText是著名的開放源碼的站點sourceforge一個項目,是用於生成PDF文檔的一個java類庫。通過iText不僅可以生成PDF或rtf的文檔,而且可以將XML、Html文件轉化為PDF文件。
iText的安裝非常方便,在http://www.lowagie.com/iText/download.html - download 網站上下載iText.jar文件後,只需要在系統的CLASSPATH中加入iText.jar的路徑,在程序中就可以使用iText類庫了。
二、建立第一個PDF文檔
用iText生成PDF文檔需要5個步驟:
①建立com.lowagie.text.Document對象的實例。
Document document = new Document();
②建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打開文檔。
document.open();
④向文檔中添加內容。
document.add(new Paragraph("Hello World"));
⑤關閉文檔。
document.close();
通過上面的5個步驟,就能產生一個Helloworld.PDF的文件,文件內容為"Hello World"。
建立com.lowagie.text.Document對象的實例
com.lowagie.text.Document對象的構建函數有三個,分別是:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
int marginRight,
int marginTop,
int marginBottom);
構建函數的參數pageSize是文檔頁面的大小,對於第一個構建函數,頁面的大小為A4,同Document(PageSize.A4)的效果一樣;對於第三個構建函數,參數marginLeft、marginRight、marginTop、marginBottom分別為左、右、上、下的頁邊距。
通過參數pageSize可以設定頁面大小、面背景色、以及頁面橫向/縱向等屬性。iText定義了A0-A10、AL、LETTER、 HALFLETTER、_11x17、LEDGER、NOTE、B0-B5、ARCH_A-ARCH_E、FLSA 和FLSE等紙張類型,也可以通過Rectangle pageSize = new Rectangle(144, 720);自定義紙張。通過Rectangle方法rotate()可以將頁面設置成橫向。
書寫器(Writer)對象
一旦文檔(document)對象建立好之後,需要建立一個或多個書寫器(Writer)對象與之關聯。通過書寫器(Writer)對象可以將具體文檔存檔成需要的格式,如com.lowagie.text.PDF.PDFWriter可以將文檔存成PDF文件, com.lowagie.text.html.HtmlWriter可以將文檔存成html文件。
設定文檔屬性
在文檔打開之前,可以設定文檔的標題、主題、作者、關鍵字、裝訂方式、創建者、生產者、創建日期等屬性,調用的方法分別是:
public boolean addTitle(String title)
public boolean addSubject(String subject)
public boolean addKeywords(String keywords)
public boolean addAuthor(String author)
public boolean addCreator(String creator)
public boolean addProcer()
public boolean addCreationDate()
public boolean addHeader(String name, String content)
其中方法addHeader對於PDF文檔無效,addHeader僅對html文檔有效,用於添加文檔的頭信息。
當新的頁面產生之前,可以設定頁面的大小、書簽、腳注(HeaderFooter)等信息,調用的方法是:
public boolean setPageSize(Rectangle pageSize)
public boolean add(Watermark watermark)
public void removeWatermark()
public void setHeader(HeaderFooter header)
public void resetHeader()
public void setFooter(HeaderFooter footer)
public void resetFooter()
public void resetPageCount()
public void setPageCount(int pageN)
如果要設定第一頁的頁面屬性,這些方法必須在文檔打開之前調用。
對於PDF文檔,iText還提供了文檔的顯示屬性,通過調用書寫器的setViewerPreferences方法可以控制文檔打開時Acrobat Reader的顯示屬性,如是否單頁顯示、是否全屏顯示、是否隱藏狀態條等屬性。
另外,iText也提供了對PDF文件的安全保護,通過書寫器(Writer)的setEncryption方法,可以設定文檔的用戶口令、只讀、可列印等屬性。
添加文檔內容
所有向文檔添加的內容都是以對象為單位的,如Phrase、Paragraph、Table、Graphic對象等。比較常用的是段落(Paragraph)對象,用於向文檔中添加一段文字。
三、文本處理
iText中用文本塊(Chunk)、短語(Phrase)和段落(paragraph)處理文本。
文本塊(Chunk)是處理文本的最小單位,有一串帶格式(包括字體、顏色、大小)的字元串組成。如以下代碼就是產生一個字體為HELVETICA、大小為10、帶下劃線的字元串:
Chunk chunk1 = new Chunk("This text is underlined", FontFactory.getFont(FontFactory.HELVETICA, 12, Font.UNDERLINE));
短語(Phrase)由一個或多個文本塊(Chunk)組成,短語(Phrase)也可以設定字體,但對於其中以設定過字體的文本塊 (Chunk)無效。通過短語(Phrase)成員函數add可以將一個文本塊(Chunk)加到短語(Phrase)中,如:phrase6.add(chunk);
段落(paragraph)由一個或多個文本塊(Chunk)或短語(Phrase)組成,相當於WORD文檔中的段落概念,同樣可以設定段落的字體大小、顏色等屬性。另外也可以設定段落的首行縮進、對齊方式(左對齊、右對齊、居中對齊)。通過函數setAlignment可以設定段落的對齊方式, setAlignment的參數1為居中對齊、2為右對齊、3為左對齊,默認為左對齊。
四、表格處理
iText中處理表格的類為:com.lowagie.text.Table和com.lowagie.text.PDF.PDFPTable,對於比較簡單的表格處理可以用com.lowagie.text.Table,但是如果要處理復雜的表格,這就需要 com.lowagie.text.PDF.PDFPTable進行處理。這里就類com.lowagie.text.Table進行說明。
類com.lowagie.text.Table的構造函數有三個:
①Table (int columns)
②Table(int columns, int rows)
③Table(Properties attributes)
參數columns、rows、attributes分別為表格的列數、行數、表格屬性。創建表格時必須指定表格的列數,而對於行數可以不用指定。
建立表格之後,可以設定表格的屬性,如:邊框寬度、邊框顏色、襯距(padding space 即單元格之間的間距)大小等屬性。下面通過一個簡單的例子說明如何使用表格,代碼如下:
1:Table table = new Table(3);
2:table.setBorderWidth(1);
3:table.setBorderColor(new Color(0, 0, 255));
4:table.setPadding(5);
5:table.setSpacing(5);
6:Cell cell = new Cell("header");
7:cell.setHeader(true);
8:cell.setColspan(3);
9:table.addCell(cell);
10:table.endHeaders();
11:cell = new Cell("example cell with colspan 1 and rowspan 2");
12:cell.setRowspan(2);
13:cell.setBorderColor(new Color(255, 0, 0));
14:table.addCell(cell);
15:table.addCell("1.1");
16:table.addCell("2.1");
17:table.addCell("1.2");
18:table.addCell("2.2");
19:table.addCell("cell test1");
20:cell = new Cell("big cell");
21:cell.setRowspan(2);
22:cell.setColspan(2);
23:table.addCell(cell);
24:table.addCell("cell test2");
運行結果如下:
header
example cell with colspan 1 and rowspan 2 1.1 2.1
1.2 2.2
cell test1 big cell
cell test2
代碼1-5行用於新建一個表格,如代碼所示,建立了一個列數為3的表格,並將邊框寬度設為1,顏色為藍色,襯距為5。
代碼6-10行用於設定表格的表頭,第7行cell.setHeader(true);是將該單元格作為表頭信息顯示;第8行 cell.setColspan(3);指定了該單元格佔3列;為表格添加表頭信息時,要注意的是一旦表頭信息添加完了之後,必須調用 endHeaders()方法,如第10行,否則當表格跨頁後,表頭信息不會再顯示。
代碼11-14行是向表格中添加一個寬度佔一列,長度佔二行的單元格。
往表格中添加單元格(cell)時,按自左向右、從上而下的次序添加。如執行完11行代碼後,表格的右下方出現2行2列的空白,這是再往表格添加單元格時,先填滿這個空白,然後再另起一行,15-24行代碼說明了這種添加順序。
五、圖像處理
iText中處理表格的類為com.lowagie.text.Image,目前iText支持的圖像格式有:GIF, Jpeg, PNG, wmf等格式,對於不同的圖像格式,iText用同樣的構造函數自動識別圖像格式。通過下面的代碼分別獲得gif、jpg、png圖像的實例。
Image gif = Image.getInstance("vonnegut.gif");
Image jpeg = Image.getInstance("myKids.jpg");
Image png = Image.getInstance("hitchcock.png");
圖像的位置
圖像的位置主要是指圖像在文檔中的對齊方式、圖像和文本的位置關系。IText中通過函數public void setAlignment(int alignment)進行處理,參數alignment為Image.RIGHT、Image.MIDDLE、Image.LEFT分別指右對齊、居中、左對齊;當參數alignment為Image.TEXTWRAP、Image.UNDERLYING分別指文字繞圖形顯示、圖形作為文字的背景顯示。這兩種參數可以結合以達到預期的效果,如setAlignment(Image.RIGHT|Image.TEXTWRAP)顯示的效果為圖像右對齊,文字圍繞圖像顯示。
圖像的尺寸和旋轉
如果圖像在文檔中不按原尺寸顯示,可以通過下面的函數進行設定:
public void scaleAbsolute(int newWidth, int newHeight)
public void scalePercent(int percent)
public void scalePercent(int percentX, int percentY)
函數public void scaleAbsolute(int newWidth, int newHeight)直接設定顯示尺寸;函數public void scalePercent(int percent)設定顯示比例,如scalePercent(50)表示顯示的大小為原尺寸的50%;而函數scalePercent(int percentX, int percentY)則圖像高寬的顯示比例。
如果圖像需要旋轉一定角度之後在文檔中顯示,可以通過函數public void setRotation(double r)設定,參數r為弧度,如果旋轉角度為30度,則參數r= Math.PI / 6。
六、中文處理
默認的iText字體設置不支持中文字體,需要下載遠東字體包iTextAsian.jar,否則不能往PDF文檔中輸出中文字體。通過下面的代碼就可以在文檔中使用中文了:
BaseFont bfChinese = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
com.lowagie.text.Font FontChinese = new com.lowagie.text.Font(bfChinese, 12, com.lowagie.text.Font.NORMAL);
Paragraph pragraph=new Paragraph("你好", FontChinese);

F. 使用Itext Doc怎麼轉換為PDF

用itext可以直接生成pdf或者rtf(doc),編程畝臘枯實迅洞現,生局物成的要轉換需要用軟體,比如Solid.Converter.PDF.Professional.v3.0 有綠色版

G. 如何利用iText文檔轉換為PDF文件

可以使用虛擬列印機來處理:
方法一:使用虛擬列印機pdf factory即可,而且其他格式文件只要回是能夠列印,選答擇這個虛擬列印機,都可以做成PDF文件,很簡單實用;
方法二:用其他虛擬列印機轉成PDF文件。
方法三:使用專門的轉換軟體,把文件轉成PDF文件。

H. 如何運用Java組件itext生成pdf

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

使用如下:

1、首先下載 JAR 包 : itext-2.0.8.jar core-render.jar

2、創建一個html頁面

<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<head>
<metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
<title>pdf</title>
</head>

<body>
這是html內容

</body>
</html>

3、編寫java代碼

publicclassPDFUtil{
publicvoidcreatePdf()throwsException{

StringinputFile="index.html";
Stringurl=newFile(inputFile).toURI().toURL().toString();
StringoutputFile="index.pdf";
System.out.println(url);


OutputStreamos=newFileOutputStream(outputFile);
org.xhtmlrenderer.pdf.ITextRendererrenderer=newITextRenderer();
renderer.setDocument(url);

//step3解決中文支持
org.xhtmlrenderer.pdf.ITextFontResolverfontResolver=renderer.getFontResolver();
fontResolver.addFont("c:/Windows/Fonts/simsun.ttc",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

renderer.layout();
renderer.createPDF(os);
os.close();

System.out.println("createpdfdone!!");
}


publicstaticvoidmain(String[]args)throwsException{
Appapp=newApp();
app.createPdf();
}
}

這樣就可以完成一個簡單PDF生成功能了。

I. itext生成word文檔方法

從事程序員職業的朋友可能都會遇到類似情況,經常需要將一個表格導出成word來供客戶列印,這個解決問題的方法可以用itext生成word文檔方法來實現。當然用excel導出也可以實現,但是如果表格主要是文字信息的話使用word文檔更合適。

需要的jar包:iTextAsian.jar,iText-rtf-2.1.3.jar,iText-2.1.3.jar;

參考代碼:

response.addHeader("Content-disposition", "attachment;filename="" + new String("信息處理表.doc".getBytes("gb2312"), "ISO8859-1"));

String letrId = request.getParameter("letrId");

// 創建word文檔,並設置紙張的大小

com.lowagie.text.Document document = new com.lowagie.text.Document(PageSize.A4);

try{

RtfWriter2.getInstance(document,"e:123.doc");

document.open();

Fontf=newFont();

Stringtopic="處理反饋表";

Paragraphp=newParagraph(topic,newFont(Font.NORMAL,18,Font.BOLDITALIC,newColor(0,0,0)));伏悄裂

p.setAlignment(1);

document.add(p);

//設置中文字體

BaseFontbfFont=BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);

//創建有三列的表格

Tabletable=newTable(3);

table.setBorderWidth(1);

table.setBorderColor(Color.BLACK);

table.setPadding(0);

table.setSpacing(0);

//添加表頭的元素

Cellcell=newCell(title);//單元格

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setHeader(運亂true);

cell.setColspan(3);//設置表格為三列

cell.setRowspan(2);//設置表格為2行

table.addCell(cell);

table.endHeaders();//表頭結束

cell=newCell("時間:"缺閉);

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph((vl.getLetrDate())。toString()));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setColspan(2);

table.addCell(cell);

cell=newCell("重要程度:");

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph((vl.getImptLevel())。toString()));

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

cell.setColspan(2);

table.addCell(cell);

//測試1

cell=newCell("測試1:");

cell.setRowspan(5);//當前單元格占兩行,縱向跨度

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

table.addCell("姓名:");

table.addCell(vl.getPernName());

table.addCell("性別:");

table.addCell(vl.getPernSex());

table.addCell("公司:");

table.addCell(vl.getPernUnit());

table.addCell("職務:");

table.addCell(vl.getPernPost());

table.addCell("地址:");

table.addCell(vl.getPernAddress());

//測試2

cell=newCell("測試2:");

cell.setRowspan(4);//當前單元格占兩行,縱向跨度

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

table.addCell("姓名:");

table.addCell(vl.getWrirName());

table.addCell("單位:");

table.addCell(vl.getWrirOrganization());

table.addCell("性別:");

table.addCell(vl.getWrirSex());

table.addCell("電話:");

table.addCell(vl.getWrirTnur());

//簡要內容

cell=newCell("簡要內容:");

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph(vl.getLetrNote()));

cell.setColspan(2);

table.addCell(cell);

//測試3

cell=newCell("測試3:");

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph(vl.getDeptVerifyOpinion()));

cell.setColspan(2);

table.addCell(cell);

//測試4

cell=newCell("測試4:");

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph(vl.getChargeVerifyOpinion()));

cell.setColspan(2);

table.addCell(cell);

//測試5

cell=newCell("測試5:");

cell.setHorizontalAlignment(Element.ALIGN_CENTER);

table.addCell(cell);

cell=newCell(newParagraph(vl.getHandleContent()));

cell.setColspan(2);

table.addCell(cell);

document.add(table);

document.add(newParagraph(""));

document.close();

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(DocumentExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

在word中可以按照你所要求的格式,利用itext的table、cell、圖片等“畫”出想要的文檔格式,然後再將數據往其中填充;如果想要將報表導出成pdf格式的,用itext也可以很輕松的實現。

J. 使用itext對pdf操作後,為什麼生成pdf文件變大了

檢查下有沒有document.newPage()使得把數據放到下一頁了。
如果沒有,那就需要分析代碼。但是換種思路也能解決你問題:
把數據作為附件一,附件二等形式生成在pdf的最後,那樣就沒人關注pdf內容的空白了。
用iText生成PDF文檔需要5個步驟:
①建立com.lowagie.text.Document對象的實例。
Document document = new Document();
②建立一個書寫器(Writer)與document對象關聯,通過書寫器(Writer)可以將文檔寫入到磁碟中。
PDFWriter.getInstance(document, new FileOutputStream("Helloworld.PDF"));
③打開文檔。
document.open();
④向文檔中添加內容。
document.add(new Paragraph("Hello World"));
⑤關閉文檔。
document.close();
通過上面的5個步驟,就能產生一個Helloworld.PDF的文件,文件內容為"Hello World"。
建立com.lowagie.text.Document對象的實例
com.lowagie.text.Document對象的構建函數有三個,分別是:
public Document();
public Document(Rectangle pageSize);
public Document(Rectangle pageSize,
int marginLeft,
int marginRight,
int marginTop,
int marginBottom);

閱讀全文

與itextwordpdf文件相關的資料

熱點內容
浦發app怎麼注銷銀行卡 瀏覽:525
編程和英語有什麼區別 瀏覽:156
源代碼交接清單 瀏覽:825
爐石應用程序異常 瀏覽:37
論文石油數據去哪個網站找 瀏覽:372
創造資料庫的兩種方式是什麼 瀏覽:958
linux使用ss代理伺服器 瀏覽:931
視易魔方機頂盒支持哪些文件 瀏覽:440
壓縮文件付費解壓 瀏覽:352
業務存期代碼 瀏覽:770
把文件下到d盤找不到了 瀏覽:171
蘋果手機有什麼拍攝app軟體推薦 瀏覽:673
網路次爪爪是什麼意思 瀏覽:269
小學教的編程學的什麼 瀏覽:178
cdrX9win10補丁 瀏覽:468
電視安裝提示文件系統未掛載 瀏覽:723
excel讀取文件中的字元串 瀏覽:397
手機文庫下載的文件 瀏覽:700
怎麼查網路的運營商的 瀏覽:127
win101607升級1703 瀏覽:225

友情鏈接