table/fit_annotation_into_cell
Fit link annotation into a table cell.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Fit annotation into cell
*
* Use add_table_cell() with the "annotationtype" and "fitannotation"
* options to define an image with a Link annotation in a table cell.
*
* Required software: PDFlib/PDFlib+PDI/PPS 10
* Required data: none
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = "../input";
$outfile = "";
$title = "Fit Annotation into Cell";
$tbl = 0;
$margin = 4;
class planedata {
function __construct($text,
$image,
$url) {
$this->text = $text;
$this->image = $image;
$this->url = $url;
}
}
$data = array(
new planedata(
"<fillcolor=red>Long Distance Glider<fillcolor=black>\n" .
"With this paper rocket you can send all your " .
"messages even when sitting in a hall or in the cinema pretty near " .
"the back.\n",
"plane1.png",
"https://www.pdflib.com"),
new planedata(
"<fillcolor=red>Giant Wing<fillcolor=black>\n" .
"An unbelievable sailplane! It is amazingly robust and " .
"can even do aerobatics. But it is best suited to gliding.\n" .
"This paper arrow can be thrown with big swing. " .
"We launched it from the roof of a hotel. It stayed in the air a " .
"long time and covered a considerable distance.\n",
"plane2.png",
"https://www.pdflib.com"),
new planedata(
"<fillcolor=red>Super Dart<fillcolor=black>\n" .
"The super dart can fly giant loops with a radius of 4 " .
"or 5 meters and cover very long distances. Its heavy cone point is " .
"slightly bowed upwards to get the lift required for loops.\n",
"plane3.png",
"https://www.pdflib.com")
);
/* Coordinates of the lower left corner of the table fitbox */
$llx = 30; $lly = 100; $urx = 565; $ury = 800;
try {
$p = new pdflib();
$p->set_option("searchpath={" . $searchpath . "}");
/* This means an exception occurs in load_font() etc. */
$p->set_option("errorpolicy=exception");
$p->begin_document($outfile, "");
$p->set_info("Creator", "PDFlib Cookbook");
$p->set_info("Title", $title);
$p->begin_page_ext(0, 0, "width=a4.width height=a4.height");
/*
* Add a header row spanning three columns
*/
$optlist = "fittextline={position={center top} fontname=NotoSerif-Bold "
. " fontsize=20} rowheight=30 margin=" . $margin . " colspan=2 ";
$tbl = $p->add_table_cell($tbl, 1, 1,
"Overview of our planes (click on plane for more information)",
$optlist);
for ($i = 0; $i < count($data); $i++ ) {
/* Left cell contains a Textflow */
$tf = $p->create_textflow($data[$i]->text, "fontname=NotoSerif-Regular"
. " fontsize=12 leading=125%");
$tbl = $p->add_table_cell($tbl, 1, $i+2, "", "textflow=" . $tf
. " margin=2 colwidth=300 rowheight=12");
/* Right cell contains an image with a Link annotation which
* is populated with a URI action. The table cell is used
* as annotation rectangle.
*/
$image = $p->load_image("auto", $data[$i]->image, "");
$action = $p->create_action("URI", "url={" . $data[$i]->url . "}");
$tbl = $p->add_table_cell($tbl, 2, $i+2, "", "image=" . $image
. " fitimage={position center fitmethod=meet} colwidth=150 "
. " annotationtype=Link fitannotation={action={activate " . $action . "} linewidth=0}");
}
/*
* ------------- Fit the table -------------
* With "header=1" the table header consists of the first row.
* Using "line=horother linewidth=0.3" the ruling is specified with
* a line width of 0.3 for all horizontal lines.
*/
$optlist = "header=1 stroke={ {line=horother linewidth=0.3}}";
$result = $p->fit_table($tbl, $llx, $lly, $urx, $ury, $optlist);
/* Check the result; "_stop" means all is ok */
if ($result != "_stop") {
if ($result == "_error"){
throw new Exception("Error: " . $p->get_errmsg());
} else {
/* Other return values require dedicated code to deal with */
}
}
$p->end_page_ext("");
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=fit_annotation_into_cell.pdf");
print $buf;
}
catch (PDFlibException $e) {
echo ("PDFlib exception occurred:\n" . "[" . $e->get_errnum() . "] " .
$e->get_apiname() . ": " . $e->get_errmsg() . "\n");
exit(1);
}
catch (Throwable $e) {
echo ("PHP exception occurred: " . $e->getMessage() . "\n");
exit(1);
}
$p = 0;
?>