2

我正在使用Pdftkpdftk-php(基于 forge-fdf)。我想生成一个可以与 PDF 合并的 FDF,以填写具有多个选项的下拉文本框。pdftk-php 默认的工作方式,它只会用单个值填充下拉列表;传递给它一个数组是行不通的。

这是 pdftk 文件名 dump_data_fields 的相关输出。

FieldType: Choice
FieldName: VehicleIDData1
FieldFlags: 4587520
FieldJustification: Left

根据 PDF 1.2 规范中的这个示例,下拉菜单似乎需要 FDF 中的 /Opt 选项。

%FDF-1.2
1 0 obj <<
/FDF <<
/Fields
[
<<
/T (My Children)
/V (Tali)
/Opt [(Maya) (Adam) (Tali)]
November 12, 1996
: 379
>>
]
/F (Dependents.pdf)
>>
>>
endobj
trailer
<</Root 1 0 R>>
%%EOF

1.7规范中第 717 页的表格支持这一点

Opt
array
(Required; choice fields only) An array of options to be presented to the user. Each element of the array can take either of two forms:
•A text string representing one of the available options
•A two-element array consisting of a text string representing one of the available options and a default appearance string for constructing the item’s appearance dynamically at viewing time (see “Variable Text” on page 677)

pdftk-php 不使用 /Opt;如果您将一个字段的值数组传递给它,它将使用 /Kids,它不会填写其他下拉条目。我尝试修改程序以改用 /Opt ,但生成的 PDF 没有区别。我究竟做错了什么?

protected function forge_fdf_fields( &$fdf,
          &$fdf_data,
          &$fields_hidden,
          &$fields_readonly,
          $accumulated_name,
          $strings_b ) // true <==> $fdf_data contains string data
    //
    // string data is used for text fields, combo boxes and list boxes;
    // name data is used for checkboxes and radio buttons, and
    // /Yes and /Off are commonly used for true and false
{
    if( 0< strlen( $accumulated_name ) ) {
        $accumulated_name.= '.'; // append period seperator
    }

    foreach( $fdf_data as $key => $value ) {
    // we use string casts to prevent numeric strings from being silently converted to numbers
        $fdf.= "<< "; // open dictionary

        if( gettype($value)== 'array' ) { // parent; recurse
            $fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") "; // partial field name
            $fdf .= "/V (" . $this->escape_pdf_string((string)$value[0]) . ") ";

            $fdf .= "/Opt [";
            foreach ($value as $option) {
                $fdf .= "(" . $this->escape_pdf_string((string)$option) . ") ";
            }

            /* This is the older code I'm replacing.
            $fdf.= "/Kids [ ";                                    // open Kids array

            // recurse
            $this->forge_fdf_fields( $fdf,
                $value,
                $fields_hidden,
                $fields_readonly,
                $accumulated_name. (string)$key,
                $strings_b );
            */

            $fdf.= "] "; // close Kids array
        } else {

            // field name
            $fdf.= "/T (".$this->escape_pdf_string( (string)$key ).") ";

            // field value
            if( $strings_b ) { // string
                $fdf.= "/V (".$this->escape_pdf_string( (string)$value ).") ";
            }
            else { // name
                $fdf.= "/V /".$this->escape_pdf_name( (string)$value ). " ";
            }

            // field flags
            $this->forge_fdf_fields_flags( $fdf,
                        $accumulated_name. (string)$key,
                        $fields_hidden,
                        $fields_readonly );
        }
        $fdf.= ">> \x0d"; // close dictionary
    }
}
4

0 回答 0