Place a sub-table in one table cell. Create a template which contains a table consisting of three rows and two columns. Create a table consisting of three rows and three columns. Add the cell in the second column of the second row so that it contain the template, e.g. the contained table, as a sub-table.
Download Java Code Show Output PDF
* Nested tables:
* Place a sub-table in one table cell.
*
* Create a template which contains a table consisting of three rows and two
* columns. Create a table consisting of three rows and three columns. Add a
* cell in the second column and the second row of the table so that the cell
* contains the template, e.g. the contained table, as a sub-table.
*
* Required software: PDFlib/PDFlib+PDI/PPS 7
* Required data: none
*/
package com.pdflib.cookbook.pdflib.table;
import com.pdflib.pdflib;
import com.pdflib.PDFlibException;
public class nested_tables
{
public static void main (String argv[])
{
/* This is where the data files are. Adjust as necessary. */
String searchpath = "../input";
String outfile = "nested_tables.pdf";
String title = "Nested Tables";
pdflib p = null;
/* Option list for the template, i.e. the sub-table */
final String taboptlist1 = "stroke={{line=other linewidth=0.1 }} " +
"fill={{area=table fillcolor={rgb 1 0.9 0.9}}} ";
/* Option list for the outer table */
final String taboptlist2 = "stroke={{line=other linewidth=0.1 } " +
"{line=frame linewidth=1.0 }} " +
"fill={{area=table fillcolor={rgb 0.9 0.9 1}}} ";
String addoptlist1, addoptlist2, fitoptlist1, textoptlist;
double fontsize = 14.0;
double margin = 5.0;
double tabwidth1, tabheight1;
int font, templ, tbl;
final double pagewidth = 595;
final double pageheight = 500;
int x = 20, y = 470, yoff = 10;
try {
p = new pdflib();
p.set_parameter("SearchPath", searchpath);
/* This means we must check return values of load_font() etc. */
p.set_parameter("errorpolicy", "return");
if (p.begin_document(outfile, "") == -1)
throw new Exception("Error: " + p.get_errmsg());
p.set_info("Creator", "PDFlib Cookbook");
p.set_info("Title", title + " $Revision: 1.4.4.2 $");
/* Load the font */
font = p.load_font("Helvetica", "unicode", "");
if (font == -1)
throw new Exception("Error: " + p.get_errmsg());
/* ---------------------------------------------------------------------
* Create a template which contains a table consisting of three rows and
* two columns. It can be pretty large, since it will be clipped later
* when placing it
* ---------------------------------------------------------------------
*/
templ = p.begin_template_ext(1000, 1000, "");
/* Add some cells for the template sub-table and fit them into the
* template
*/
addoptlist1 = "fittextline={font=" + font + " fontsize=" + fontsize +
"} margin=" + margin;
tbl = p.add_table_cell(-1, 1, 1, "tab 1, cell A", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 2, 1, "tab 1, cell B", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 1, 2, "tab 1, cell C", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 2, 2, "tab 1, cell D", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 1, 3, "tab 1, cell E", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 2, 3, "tab 1, cell F", addoptlist1);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
p.fit_table(tbl, 0, 0, 1000, 1000, taboptlist1);
/* Retrieve the width and height of the template sub-table */
tabwidth1 = p.info_table(tbl, "width");
tabheight1 = p.info_table(tbl, "height");
/* Finish the template */
p.end_template();
/* Start page */
p.begin_page_ext(pagewidth, pageheight, "");
/* ------------------------------------------------------------------
* Place the sub-table.
* To illustrate the extent of the sub-table first output it
* individually. "rewind=1" is only required here for illustration
* purposes. It resets the table to the state before last fit_table()
* call which has been used to place the table into the template.
* Otherwise we would not be able to place the table a second time.
* ------------------------------------------------------------------
*/
/* Output some descriptive text */
textoptlist = "font=" + font + " fontsize=" + fontsize;
p.fit_textline("Inner table 1 (placed in a template):", x, y,
textoptlist);
y -= tabheight1 + yoff;
fitoptlist1 = taboptlist1 + "rewind=1";
p.fit_table(tbl, x + tabwidth1, y, x + 2 * tabwidth1, y + tabheight1,
fitoptlist1);
p.delete_table(tbl, "");
/* -----------------------------------------------------------------
* Create the outer table consisting of three rows and three columns
* -----------------------------------------------------------------
*/
/* Output some descriptive text */
y -= yoff*4;
textoptlist = "font=" + font + " fontsize=" + fontsize;
p.fit_textline("Outer table 2:", x, y, textoptlist);
/* Add some cells to the outer table.
* The column width is set to the table width of the sub-table which
* has been retrieved above. The row height is set to the height of
* the sub-table which has been retrieved above.
*/
addoptlist2 = "fittextline={font=" + font + " fontsize=" + fontsize +
"} margin=" + margin + " colwidth=" + tabwidth1 + " rowheight=" +
tabheight1;
tbl = p.add_table_cell(-1, 1, 1, "tab 2, cell 1", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 2, 1, "tab 2, cell 2", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 3, 1, "tab 2, cell 3", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 1, 2, "tab 2, cell 4", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 3, 2, "tab 2, cell 5", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 1, 3, "tab 2, cell 6", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 2, 3, "tab 2, cell 7", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
tbl = p.add_table_cell(tbl, 3, 3, "tab 2, cell 8", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Add another cell containing the template sub-table as well as some
* gray text. The "image" option supplies the template.
* The "fitimage" option uses the "matchbox={clipping ...}" suboption to
* clip the template to the actual extent of the template sub-table
* whose height and width has been retrieved above.
*/
addoptlist2 = "image=" + templ + " fitimage={matchbox=" +
"{clipping={0.0 " + (1000.0-tabheight1) + " " +
tabwidth1 + " 1000.0}}} fittextline={font=" + font +
" position={center 70} fontsize=26 fillcolor={gray 0.8}}";
tbl = p.add_table_cell(tbl, 2, 2, "tab 2, cell 9", addoptlist2);
if (tbl == -1)
throw new Exception("Error: " + p.get_errmsg());
/* Place the outer table */
y -= yoff;
p.fit_table(tbl, x, 20, pagewidth - x, y, taboptlist2);
p.end_page_ext("");
p.end_document("");
} catch (PDFlibException e) {
System.err.print("PDFlib exception occurred:\n");
System.err.print("[" + e.get_errnum() + "] " + e.get_apiname() +
": " + e.get_errmsg() + "\n");
} catch (Exception e) {
System.err.println(e.toString());
} finally {
if (p != null) {
p.delete();
}
}
}
}