pdf_import/clip_imported_page
Clip imported PDF page.
Download PHP Code Switch to Java Code Show Output
<?php
/*
* Clip imported PDF page:
*
* Use the "matchbox" option with the "clipping" suboption to place some
* part of an imported PDF page. The following variants are demonstrated:
* - clip a rectangular area of the page and place it in its original scaling.
* - clip a rectangular area of the page and force-fit into a box with
* specified size.
*
* Required software: PDFlib+PDI/PPS 9
* Required data: input PDF
*/
/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
/* By default annotations are also imported. In some cases this
* requires the Noto fonts for creating annotation appearance streams.
* We therefore set the searchpath to also point to the font directory.
*/
$fontpath = dirname(__FILE__,3)."/resource/font";
$outfile = "";
$title = "Clip imported page";
$exitcode = 0;
$pdffile = "lionel.pdf";
try {
$llx = 50; $lly = 550;
$p = new pdflib();
$p->set_option("searchpath={" . $searchpath . "}");
$p->set_option("searchpath={" . $fontpath . "}");
/* This means we must check return values of load_font() etc. */
$p->set_option("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);
/* Load page of the input PDF document */
$doc = $p->open_pdi_document($pdffile, "");
if ($doc == 0)
throw new Exception("Error: " . $p->get_errmsg());
$page = $p->open_pdi_page($doc, 1, "");
if ($page == 0)
throw new Exception("Error: " . $p->get_errmsg());
$font = $p->load_font("NotoSerif-Regular", "unicode", "");
if ($font == 0)
throw new Exception("Error: " . $p->get_errmsg());
/* --------------------------------------------------------
* Output the original page with transparency as background
* --------------------------------------------------------
*/
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
$p->setfont($font, 10);
/* Output some descriptive text */
$text = "Place the full PDF page in its original size with transparency (for comparison)";
$p->fit_textline($text, $llx, 520, "");
/* Create an extended graphics state with transparency */
$gstate = $p->create_gstate("opacityfill=.4");
/* Fit the PDF page in its original size with the transparency gstate */
$p->fit_pdi_page($page, $llx, $lly-=500, "gstate=" . $gstate);
/* ----------------------------------------------
* Place the clipped page in its original size
* ----------------------------------------------
*/
/* Define a rectangular part of the PDF page with the "matchbox" option
* and the "clipping" suboption to set the lower left and upper right
* corners of the clipping rectangle.
*/
$c_llx = 100; $c_lly = 100; $c_urx = 350; $c_ury = 375;
$clipping_optlist = "matchbox={clipping={" .
$c_llx . " " . $c_lly . " " . $c_urx . " " . $c_ury . "}}";
/* Output some descriptive text */
$text = "Place the clipped PDF page in its original size:";
$p->fit_textline($text, $llx, 505, "");
$text = "p.fit_pdi_page(page, " . ($llx + $c_llx) . " , " . ($lly + $c_lly) .
", \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, 490, "");
/* Display the clipped area at the same position where it would
* appear when displaying the complete PDF page.
*/
$p->fit_pdi_page($page, $llx + $c_llx, $lly + $c_lly, $clipping_optlist);
$p->end_page_ext("");
/* -----------------------------------------------------
* Display the clipped area in a box with specified size
* -----------------------------------------------------
*/
$p->begin_page_ext(0, 0, "width=a4.height height=a4.width");
$p->setfont($font, 10);
$lly = 550;
/* Define a rectangular part of the PDF page with the "matchbox" option
* and the "clipping" suboption. The lower left and upper right corners
* of the rectangle are specified as percentages of the original PDF page size.
*/
$clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}}";
$text = "Place the clipped area in its original size:";
$p->fit_textline($text, $llx, $lly-=30, "");
$text = "p.fit_pdi_page(page, x, y, \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, $lly-=15, "");
/* Place the clipped PDF page */
$p->fit_pdi_page($page, $llx, $lly-=220, $clipping_optlist);
/* Display the clipped PDF page in a box of the specified size. Scale the
* clipped PDF page proportionally so that it fits into the specified box
* with "fitmethod=meet".
*/
$clipping_optlist = "matchbox={clipping={35% 35% 75% 75%}} " .
"boxsize={200 100} fitmethod=meet showborder";
/* Output some descriptive text */
$text = "Place the clipped area in a specified box:";
$p->fit_textline($text, $llx, $lly-=50, "");
$text = "p.fit_pdi_page(page, x, y, \"" . $clipping_optlist . "\");";
$p->fit_textline($text, $llx, $lly-=15, "");
$p->fit_pdi_page($page, $llx, $lly-=120, $clipping_optlist);
$p->end_page_ext("");
$p->close_pdi_page($page);
$p->close_pdi_document($doc);
$p->end_document("");
$buf = $p->get_buffer();
$len = strlen($buf);
header("Content-type: application/pdf");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=clip_imported_page.php");
print $buf;
}catch (PDFlibException $e) {
echo("PDFlib exception occurred in sample:\n" .
"[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
$e->get_errmsg() . "\n");
exit(1);
}
catch (Throwable $e) {
echo($e);
exit(1);
}
$p = 0;
?>