Itext PDF line-by-line color change itext5 Add Table background color
New requirements, line-by-line color change, itext in action is a good instruction, according to the English reading, very simple implementation, the idea is as follows:
1. Create a pdfptable object, generate a cell in the PDF table, and add an event for color separation. Insert the event before adding the pdfptable to the Document Object.
2. you need to write a Java class to define the background color and the length, width, and height of each line. The essence is to read all rows and columns on the page after the PDF table is generated, create a rectangle and add the background to the cell to overwrite the background color.
The line-by-line color change Java class: to implement the pdfptableevent interface, otherwise there will be no more.
Alternatingbackground. Java
/*** Project name: Report * file name: alternatingbackground. java * package name: COM. riambsoft. report. util * Date: 11:08:30 * copyright (c) 2013, riambsoft All Rights Reserved. **/package COM. itext. me; import Java. AWT. color; import Java. lang. management. managementfactory; import javax. management. instancealreadyexistsexception; import javax. management. mbeanregistrationexception; import javax. management. mbeanserver; import javax. management. malformedobjectnameexception; import javax. management. notcompliantmbeanexception; import javax. management. objectname; import COM. itextpdf. text. basecolor; import COM. itextpdf. text. rectangle; import com.itext;.text. using contentbyte; import com.itext;.text=. pdfptable; import com.itext;.text=. pdfptableevent;/*** classname: alternatingbackground <br/> function: Todo add function. <br/> reason: Todo add reason. <br/> date: 11:08:30 <br/> ** @ author administrator * @ version * @ since JDK 1.5 * @ see */public class alternatingbackground implements pdfptableevent {public void tablelayout (pdfptable table, float [] [] widths, float [] heights, int headerrows, int rowstart, comment contentbyte [] canvases) {int columns; rectangle rect; // the appropriate color: (235,235,235) int footer = widths. length-table. getfooterrows (); int header = table. getheaderrows ()-table. getfooterrows () + 1; for (int row = header; row <footer; row + = 2) {columns = widths [row]. length-1; rect = new rectangle (widths [row] [0], heights [row], widths [row] [columns], heights [row + 1]); rect. setbackgroundcolor (New basecolor (235,235,235); rect. setborder (rectangle. no_border); canvases [pdfptable. basecanvas]. rectangle (rect );}}}
Then, in your PDF generation class, add a new event to the pdftable file. Look
/*** Project name: testitext * file name: testpdf. java * package name: COM. itext. me * Date: 03:38:17 * copyright (c) 2013, riambsoft All Rights Reserved. **/package COM. itext. me; import Java. io. fileoutputstream; import Java. io. ioexception; import Java. util. random; import COM. itextpdf. text. document; import COM. itextpdf. text. extends entexception; import COM. itextpdf. text. element; import COM. itextpd F. text. font; import COM. itextpdf. text. phrase; import com.itext;.text=. basefont; import com.itext;.text=. export pcell; import com.itext;.text=. pdfptable; import com.itext;.text=. pdfptableevent; import com.itext;.text=. using writer;/*** classname: testpdf <br/> function: Todo add function. <br/> reason: Todo add reason. <br/> date: 03:38:17 <br/> ** @ author administrator * @ Version * @ since JDK 1.5 * @ see */public class testpdf {/*** main :( here I will describe the role of this method in one sentence ). <br/> ** @ Param ARGs * @ throws ioexception * @ throws define entexception * @ since JDK 1.5 */public static void main (string [] ARGs) throws ioexception, incluentexception {string filedesc = ""; fileoutputstream outr = NULL; // create an output stream // generate a random number random = new random (); int x = random. nextint (); filedesc = "C :\\ Using test \ "+" _ "+ x + ". PDF "; // The path must contain this file basefont basefontchinese = basefont. createfont ("stsong-light", "UniGB-UCS2-H", basefont. not_embedded); font fontchinese = new font (basefontchinese, 12, Font. normal); // Step 1 Document document = new document (); // Step 2 try {outr = new fileoutputstream (filedesc); Using writer. getinstance (document, outr); // step 3 document. open (); // Step 4 pdfptable Tab Le = new pdfptable (3); // The cell object contains pcell cell; // We add a cell with colspan3 cell = new cell pcell (new phrase ("cell with colspan 3"); cell. setcolspan (3); table. addcell (cell); // now we add a cell with rowspan2 cell = new pdfpcell (new phrase ("cell with rowspan 2 interbank", fontchinese); cell. setrowspan (2); // cell. sethorizontalalignment (element. align_middle); cell. setverticalalignment (element. Align_middle); table. addcell (cell); // we add the four remaining cells with addcell () table. addcell ("Row 1-1; cell 1"); table. addcell ("Row 1-2; Cell 2"); cell = new nested pcell (new phrase ("cell with rowspan 2", fontchinese); table. addcell ("Row 2-1; cell 1"); table. addcell ("Row 2-2; Cell 2"); table. addcell ("Row 3-1; cell 1"); table. addcell ("Row 3-2; Cell 2"); table. addcell ("Row 3-3; cell 3"); tabl E. addcell ("Row 4-1; cell 1"); table. addcell ("Row 4-2; Cell 2"); table. addcell ("Row 4-3; cell 3"); table. addcell ("Row 5-1; cell 1"); table. addcell ("Row 5-2; Cell 2"); table. addcell ("Row 5-3; cell 3"); table. addcell ("Row 6-1; cell 1"); table. addcell ("Row 6-2; Cell 2"); table. addcell ("Row 6-3; cell 3"); table. addcell ("Row 7-1; cell 1"); table. addcell ("Row 7-2; Cell 2"); table. addcell ("Row 7-3; cell 3 "); Table. addcell ("Row 8-1; cell 1"); table. addcell ("Row 8-2; Cell 2"); table. addcell ("Row 8-3; cell 3"); table. addcell ("row 9-1; cell 1"); table. addcell ("row 9-2; Cell 2"); table. addcell ("row 9-3; cell 3"); // Add the color change event pdfptableevent event = new alternatingbackground (); table. settableevent (event); // end document. add (table); document. close ();} catch (incluentexception E1) {e1.printstacktrace ();} Finally {If (outr! = NULL) {outr. close ();} string cmd = "\" C: \ Program Files \ Foxit Software \ Foxit Reader \ Foxit reader.exe \""; // This is the command line to open the Fuxin reader. You can copy one of my installation paths. System. out. println (CMD + "\" "+ filedesc +" \ "" + "-n "); runtime.getruntime(cmd.exe C (CMD + "\" "+ filedesc +" \ "" + "-n"); // open a PDF }}}
If the Layout looks bad, please move Baidu space: http://hi.baidu.com/ae6623/item/4a9012214366cfd10f37f914
All source code + jar package + Doc documentation download: http://download.csdn.net/detail/ae6623/5471531
Rain
May 29, 2013 9:19:03
Qq 1, 394263788