I have been dealing with pdf for the past two days and summarize the results of these two days. Here you need to note that itext uses itext 1.4, different versions may have different forms of api, may not be compatible, jar: http://cid-f41b44b9285d0b86.office.live.com/browse.aspx/%e5%85%ac%e5%bc%80%e6%96%87%e4%bb%b6%e5%a4%b9/J2EE%e5%ad%a6%e4%b9%a0%e8%b5%84%e6%96%99%e5%85%b1%e4%ba% AB
In the following example, a file index (similar to a word link) is generated, and a report is generated and the image is converted to a pdf file. The Code is as follows:
Public void GenerateAllParts (){
Document document = new Document ();
Try {
Using writer. getInstance (document, new FileOutputStream ("d: \ allvariables "));
// Generate the font
BaseFont bfChinese = BaseFont. createFont ("STSongStd-Light", "UniGB-UCS2-H", false );
// Title Font
Font f30 = new Font (bfChinese, 30, Font. NORMAL, Color. BLACK );
// Text font
Font f12 = new Font (bfChinese, 12, Font. NORMAL, Color. BLACK );
Font f6 = new Font (bfChinese, 6, Font. NORMAL, Color. BLACK );
Font f8 = new Font (bfChinese, 8, Font. NORMAL, Color. BLACK );
Document. open ();
// Title
Document. add (new Paragraph ("report instance", f30 ));
// Line feed
Document. add (new Chunk ("\ n "));
//
Document. add (
New Paragraph (
New Chunk ("...... click to view the report", f12)
. SetLocalGoto ("table ")));
// Line feed
Document. add (new Chunk ("\ n "));
Document. add (
New Paragraph (
New Chunk ("...... click to view the image", f12)
. SetLocalGoto ("image ")));
Document. add (new Chunk ("\ n \ n "));
//////////////////////////////////////// ///////////
// Report location
Document. add (new Chunk ("report instance", f12). setLocalDestination ("table "));
// Add a table instance
PdfPTable table = new PdfPTable (5 );
Table. setWidthPercentage (100 );
Table. setHorizontalAlignment (PdfPTable. ALIGN_LEFT );
PdfPCell cell = new PdfPCell ();
Cell. setBackgroundColor (new Color (213,141, 69 ));
Cell. setHorizontalAlignment (export pcell. ALIGN_CENTER );
// Table title
Cell. setPhrase (new Paragraph ("Title 1", f8 ));
Table. addCell (cell );
Cell. setPhrase (new Paragraph ("title 2", f8 ));
Table. addCell (cell );
Cell. setPhrase (new Paragraph ("Title 3", f8 ));
Table. addCell (cell );
Cell. setPhrase (new Paragraph ("Title 4", f8 ));
Table. addCell (cell );
Cell. setPhrase (new Paragraph ("Title 5", f8 ));
Table. addCell (cell );
// Table data
Export pcell newcell = new export pcell ();
Newcell. setHorizontalAlignment (effecpcell. ALIGN_CENTER );
Newcell. setPhrase (new Paragraph ("data 1", f8 ));
Table. addCell (newcell );
Newcell. setPhrase (new Paragraph ("Data 2", f8 ));
Table. addCell (newcell );
Newcell. setPhrase (new Paragraph ("data 3", f8 ));
Table. addCell (newcell );
Newcell. setPhrase (new Paragraph ("data 4", f8 ));
Table. addCell (newcell );
Newcell. setPhrase (new Paragraph ("Data five", f8 ));
Table. addCell (newcell );
Document. add (table );
//////////////////////////////////////// ////////////////
//////////////////////////////////////// //////////////////
// Add a connection
Document. add (new Chunk ("image instance", f12). setLocalDestination ("image "));
Image jpg = Image. getInstance ("d: \ 3.jpg ");
Document. add (jpg );
//////////////////////////////////////// //////////////////
Document. close ();
} Catch (Exception e ){
// TODO: handle exception
}
}
The example below demonstrates how to convert word to pdf, here is the use of openoffice to achieve, refer to: http://www.javaeye.com/topic/352103
3. Implementation Code:
/**
* Converting word2pdf
*/
Public void GeneratePdfFromWord (){
Date date = new Date ();
OpenOfficeConnection connection = new SocketOpenOfficeConnection (8100 );
Try {
Connection. connect ();
DocumentConverter converter = new OpenOfficeDocumentConverter (connection );
Converter. convert (new File ("d: \ test.doc"), new File ("d: \ word2pdf.pdf .pdf "));
} Catch (Exception e ){
// TODO: handle exception
} Finally {
If (connection! = Null ){
Connection. disconnect ();
Connection = null;
}
}
}
1. Install OpenOffice 3: Download path: http://zh.openoffice.org/new/zh_cn/downloads.html 2. Use the following command to start the OpenOffice service cd C: \ Program Files \ OpenOffice.org 3 \ program
Soffice-headless-accept = "socket, host = 127.0.0.1, port = 8100; urp;"-nofirststartwizard