PDFlib Cookbook

cookbook

blocks/dump_block_properties

Dump Block information from an existing PDF document.

Download PHP Code  Switch to Java Code  Show Output  Show Input (announcement_blocks.pdf) 

<?php
/* 
 * Dump block information from an existing PDF document.
 *
 * Required software: PDFlib+PDI/PPS 9
 * Required data: PDF input file
 */

/* This is where the data files are. Adjust as necessary. */
$searchpath = dirname(__FILE__,3)."/input";
$pdfinput = "announcement_blocks.pdf";
$docoptlist = "requiredmode=minimum";

function getProperty($p, $doc, $info) {
    $output = "";
    $objtype = $p->pcos_get_string($doc, "type:" . $info );
    if ($objtype == "name" || $objtype == "string" || $objtype == "boolean" ) {
        $output .= $p->pcos_get_string($doc, $info);
    } elseif ($objtype == "number") {
        $output .= sprintf("%.2f", $p->pcos_get_number($doc, $info));
    } elseif ($objtype == "array") {
        $output .= getArrayPropertyAsString($p, $doc, $info);
    } elseif ($objtype == "dict") {
        $output .= getDictPropertyAsString($p, $doc, $info);
    } else {
        $output .= "[ $info unknown type ($objtype)]";
    }
    return $output;
}

function getDictPropertyAsString($p, $doc, $path) {
    $output = "";
    $length = $p->pcos_get_number($doc, "length:". $path);
    for ($i=0; $i <$length; $i++) {
        $info = $path . "[" . $i . "].key";
        $key = $p->pcos_get_string($doc, $info);
        $output .= $key ." [";
        $info = $path . "[" . $i . "]";
        $output .= getProperty($p, $doc, $info);
        $output .= "] ";
    }
    return "[ " . $output . "]";
}

function getArrayPropertyAsString($p, $doc, $path) {
    $output = "";
    $length = $p->pcos_get_number($doc, "length:" . $path);
    for ($i=0; $i <$length; $i++) {
        $info = $path . "[" . $i . "]";
        $output .= getProperty($p, $doc, $info);
        $output .= " ";
    }
    return "[ " . $output . "]";
}

try {
    $title = 'Block properties for "' . $pdfinput . '":';
    print $title . "\n\n";
    
    $p = new PDFlib();

    // This means we must check return values of load_font() etc.
    $p->set_option("errorpolicy=return");

    // all strings are expected as utf8

    $p->set_option("SearchPath={{" . $searchpath . "}}");

    /* We do not create any output document, so no call to
     * begin_document() is required.
     */

    /* Open the input document */
    $doc = $p->open_pdi_document($pdfinput, $docoptlist);
    if ($doc == 0) {
        die("Error: " . $p->get_errmsg());
    }
    $pagecount = $p->pcos_get_number($doc, "length:pages");
    for ($page = 0; $page <$pagecount; $page++) {

        $count = $p->pcos_get_number($doc, "length:pages[" . $page . "]/blocks");

        for ($i=0; $i < $count; $i++) {
            $info = "type:pages[". $page . "]/blocks[" . $i . "]";
            $objtype = $p->pcos_get_string($doc, $info);

            $info = "pages[". $page . "]/blocks[" . $i . "].key";
            $key = $p->pcos_get_string($doc, $info);
            print($key . ":\n");

            $pcospath=  "pages[". $page . "]/blocks/" . $key;
            $length = $p->pcos_get_number($doc, "length:" . $pcospath);
            for ($j = 0; $j <$length; $j++) {
                $info = $pcospath . "[" . $j . "].key";
                $key = $p->pcos_get_string($doc, $info);
                $blockproppath = $pcospath . "/" . $key;
                $objtype = $p->pcos_get_string($doc, "type:" . $blockproppath);
                printf("  %s(%s\t%s\t%s)\n", $objtype, $blockproppath, $key, 
            getProperty($p, $doc, $blockproppath));
            }

            print "\n";
        }
    }


    $p->close_pdi_document($doc);
}
catch (PDFlibException $e) {
    echo("PDFlib exception occurred in dump_block sample:<br/>" .
        "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
        $e->get_errmsg() . "<br/>");
    exit(1);
}
catch (Throwable $e) {
    echo("PHP exception occurred: " . $e->getMessage() . "\n");
    exit(1);
}

$p = 0;
?>