在PostScript語言程序
首先使用pdfmark運算,我會解決你的問題的一部分,1:通過使用Adobe Acrobat pdfmark運算表達式運行的PostScript語言程序創建這樣一個PDF表單文件,蒸餾器等。請參閱下面的示例程序。
您閱讀的「PDFMARK文檔」是pdfmark Reference,我接受它。我的副本的日期爲2006年11月。示例5.11 定義AcroForm字典文檔目錄和5.12 定義Widget註釋,該頁面也是此頁面的字段字典,頁面70-75。仔細閱讀代碼,將其複製到源文件並提取,並修改以適應。這就是我的示例所基於的。
請務必仔細閱讀第1章介紹(p.9-16),它將幫助您閱讀例5.11和5.12中的pdfmark語法。 命名對象(p。12-16)對於理解示例中的字體和編碼如何引用很重要。
但是您還需要閱讀PDF語言規範。 Adobe公司的規範的副本已被取代由ISO標準PDF 32000-1:2008 Document management — Portable document format — Part 1: PDF 1.7閱讀第12.7 互動形式(第430-456),這將經常提到你第12.5節註解(第381-414),用於控制在表單字段的外觀上。
要理解的重要一點是,大多數表單字段將由PostScript語言[ ... /ANN pdfmark
表達式中的鍵值對定義。鍵值對在PDF規範中指定,而不是pdfmark規範。但在閱讀PDF規範時編輯pdfmark表達式很容易。
以下程序提煉成PDF文件與表單字段:
% Simple pdfmark form example
% by Jim DeLaHunt, jdlh.com, 3. March 2012
%
% This PostScript language code, when converted to PDF by
% Adobe Acrobat Distiller or the like, creates a PDF file with form
% fields, and a Submit button which sends the values of the fields
% via an HTTP POST request.
%
% In reply to Stack Overflow question, "How can I create a PDF form
% using PDFMARK and/or PERL cpan module PDF::API2?",
% http://stackoverflow.com/questions/9440930/
%
% Define stubs for interpreters without pdfmark support
% From pdfMark reference, Example 1.2, p.10
/pdfmark where {
% pdfmark is built-in
% define {code} ?pdfmark - : exec modal PDF code.
pop globaldict /?pdfmark /exec load put
}{
globaldict begin
/?pdfmark /pop load def% pdfmark is absent: ignore code.
/pdfmark /cleartomark load def
end
} ifelse
%%EndProlog
% This code makes the example clearer. It isn't part of the AcroForm.
<</PageSize [300 300]>> setpagedevice % smaller page (~ 10cm x 10cm)
clippath 0.9 setgray fill % light grey background, to show widgets.
/Helvetica-Bold 12 selectfont 0 setgray % for labels
/showlabel { % (text) x y showlabel -
% shows (text) top-justified at x,y, moves down one line
-12 add moveto % x y -12 add moveto -
gsave show grestore
0 -3 rmoveto % leading (space between lines)
} def
/showcomment { % (text) showcomment -
% shows (text) top-justified at current point, moves down 1 line
0 -10 rmoveto
gsave /Helvetica 10 selectfont show grestore
0 -2 rmoveto % leading (space between lines)
} def
(Simple pdfmark form example) 10 290 showlabel
% AcroForm dictionary setup. From pdfmark Reference, Example 5.11, p.70
%
% Read also PDF specification PDF32000:2008
% "Document management — Portable document format — Part 1: PDF 1.7",
% Section 12.7 "Interactive Forms", p.430
% To understand the syntax: [/_objdef {foo} /type /dict /OBJ pdfmark
% read carefully pdfMark reference, "User-defined named objects", p.13.
% Define font objects for the widgets to use
[ /_objdef {ZaDb} /type /dict /OBJ pdfmark
[ {ZaDb} <<
/Type /Font
/Subtype /Type1
/Name /ZaDb
/BaseFont /ZapfDingbats
>> /PUT pdfmark
[ /_objdef {Helv} /type /dict /OBJ pdfmark
[ {Helv} <<
/Type /Font
/Subtype /Type1
/Name /Helv
/BaseFont /Helvetica
% /Encoding {pdfDocEncoding} % for simplicity, use font's own encoding
% pdfmark Reference, Example 5.11, p.70, provides an Encoding
% array defining PDFDocEncoding
>> /PUT pdfmark
[ /_objdef {aform} /type /dict /OBJ pdfmark
% Define Fields array of Acroform dictionary. It will contain entries for
% each of the widgets defined below.
% NOTE: It is not necessary to explicitly assign the widget annotations
% to the Fields array; Acrobat does it automatically when the file is
% opened.
[ /_objdef {afields} /type /array /OBJ pdfmark
[ {aform} <<
/Fields {afields}
/DR << /Font << /ZaDb {ZaDb} /Helv {Helv} >> >>
/DA (/Helv 0 Tf 0 g)
/NeedAppearances true
% "/NeedAppearances true" tells reader 'to construct appearance
% streams and appearance dictionaries for all widget annotations
% in the document...' --PDF32000:2008 Table 218 p.431
>> /PUT pdfmark
% Put Acroform entry in catalog dictionary
[ {Catalog} << /AcroForm {aform} >> /PUT pdfmark
% Example 5.12 Define the Widget annotations
% pdfmark Reference pp. 72-75, with clarifications by Jim DeLaHunt
%
% These Widget annotations are also field dictionaries for this form.
% This is the collection of all individual widget annotations.
% It is possible to have multiple instances of these sections, such as
% for defining a single widget on each instance.
%
% See PDF32000:2008 Section 12.5.6.19 "Widget Annotations", p.408
% especially tables 188 and 189.
(/Tx text field) 10 260 showlabel
(with Javascript) showcomment (validation) showcomment
[ /Subtype /Widget
/Rect [100 223 245 260]
/F 4
/T (SL Text)
/FT /Tx
/DA (/Helv 14 Tf 0 0 1 rg)
/V (5)
/AA <<
/K <<
/S /JavaScript
/JS (AFNumber_Keystroke\(2, 0, 0, 0, "$", true\);)
>>
/F <<
/S /JavaScript
/JS (AFNumber_Format\(2, 0, 0, 0, "$", true\);)
% pdfmark Reference p.72 seems to omit closing ")" above. Oops.
>>
>>
/ANN pdfmark
(/Btn field) 10 208 showlabel
(Check Box) showcomment
[ /Subtype /Widget
/Rect [100 172 136 208]
/F 4
/T (Check Box)
/FT /Btn
/DA (/ZaDb 0 Tf 0 g)
/AS /Off
/MK << /CA (4)>>
/AP << /N << /Oui /null >> >>
/ANN pdfmark
(/Ch field) 10 160 showlabel
(List Box) showcomment
[ /Subtype /Widget
/Rect [100 87 137 160]
/F 4
/T (List Box)
/FT /Ch
/DA (/Helv 10 Tf 1 0 0 rg)
/Opt [(1)(2)(3)(4)(5)]
/DV (3)
/V (3)
/MK <<
/BG [1 1 1] % Background: white
/BC [0 0 0] % Border colour: black
>>
% MK is 'An appearance characteristics dictionary (see Table 189)'
% --PDF32000:2008 Table 188 p.408
% Without an /MK entry, Acrobat Pro 8.0 complains of an error
% when displaying this field, and some entries aren't displayed.
/ANN pdfmark
(/Btn button) 160 85 showlabel
(/S /SubmitForm action) showcomment
% Example of how the /MK dictionary is used.
[ /Subtype /Widget
/Rect [ 130 10 270 50 ]
/F 4
/T (Submit)
/FT /Btn
/H /P
/A <<
/S /SubmitForm
/F (http://posttestserver.com/post.php) % Thanks, Henry!
/Flags 16#6 % IncludeNoValueFields, ExportFormat
>>
/DA (/HeBo 18 Tf 0 0 1 rg)
/Ff 65536
/MK <<
/BC [ 1 0 0 ]
/BG [ 0.75 0.45 0.75 ]
/CA (Submit)
/AC (Submitted!)
>>
/BS <<
/W 3
/S /I
>>
/ANN pdfmark
% When pressing the Submit button in Adobe Acrobat Professional 8.3.1,
% there is a pause, and then Acrobat displays a PDF file with the
% results of the HTTP POST operation. This is text like,
% Successfully dumped 3 post variables. View it at
% http://www.posttestserver.com/data/2012/03/04/01.29.271751566025
% Post body was 0 chars long.
%
% Browse to that URL, and you will see diagnostics with the following:
% ....
% Post Params:
% key: 'Check_Box' value: 'Oui'
% key: 'List_Box' value: '4'
% key: 'SL_Text' value: '3.14'
% Empty post body.
showpage % make sure a page gets produced
開啓在PDF閱讀器生成的PDF文件。更改表單中的值。請注意,頂部文本字段將您限制爲一個帶有兩位小數的數字,並且它會在該數字的前面放置一個$
符號。它使用Javascript代碼來做到這一點。有一個複選框,它在選中時發送值Oui
。
當您單擊Submit
按鈕時,PDF閱讀器會爲每個表單字段和該字段的值提供鍵,並通過HTTP POST
調用將它們發送到URL。我使用的目的地是Henry's HTTP Post Dumping Server。服務器向POST返回文本回復,給出一個URL,您可以在其中看到服務器收到的內容。 Adobe Acrobat Professional將此回覆轉換爲PDF文件,可能需要幾秒鐘。訪問該URL,您可以看到您所尋找的field_name/value對。
使用Perl模塊PDF :: API2
我不是很有經驗的Perl和我從來沒有使用PDF::API2 module。但是,從代碼檢查中,我懷疑用PDF :: API2生成PDF表單並不容易。
該PDF::API2::Annot子模塊看起來很有前途。但爲了使用上述示例程序的方法,您必須創建/Subtype /Widget
的PDF註釋。它似乎PDF :: API2 :: Annot不支持此操作。
查看source code of PDF::API2::Annot,可以看到它如何處理其他註釋類型的示例。例如,鏈接註釋類型部分由類似的代碼創建:
$self->{Subtype}=PDFName('Link');
是被支持的Widget註釋,一個希望看到像$self->{Subtype}=PDFName('Widget')
語句。但在此模塊(版本2.019)中沒有字符串Widget
的實例。因此,它可能需要對此模塊進行擴展才能支持Widget註釋。
現在註釋只是具有特定內容的PDF字典。可能使用PDF::API2::Basic::PDF::Dict從基本部分構建Widget註釋。不過,我猜測這將比擴展PDF :: API2 :: Annot更有用。
重視這個問題,並且賞金它吸引,作爲一個標誌:會有多大的欣喜,如果有人添加了Widget註釋支持PDF :: API2 ::阿諾,或許使得PDF :: API2: :表格模塊!
也許[這個討論](http://stackoverflow.com/questions/4346590/submit-pdf-form-fields-to-a-http-post-request)會有幫助嗎? – raina77ow 2012-02-25 03:35:01
澄清:您希望生成的PDF是可填寫的表單? – 2012-02-27 18:56:29