PDF Form Fill, Form Data Extraction with Forms Data Format (FDF) - Ruby Sample Code

Sample code for using Apryse SDK to programmatically merge forms data with the PDF in order to fill forms, or to extract form field data from the PDF. Apryse SDK has full support for Forms Data Format (FDF). Sample code provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB.

Learn more about our full PDF Data Extraction SDK Capabilities.

To start your free trial, get stated with Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# PDFNet includes a full support for FDF (Forms Data Format) and capability to merge/extract
14# forms data (FDF) with/from PDF. This sample illustrates basic FDF merge/extract functionality
15# available in PDFNet.
16#---------------------------------------------------------------------------------------
17
18 PDFNet.Initialize(PDFTronLicense.Key)
19
20 # Relative path to the folder containing the test files.
21 input_path = "../../TestFiles/"
22 output_path = "../../TestFiles/Output/"
23
24 # Example 1
25 # Iterate over all form fields in the document. Display all field names.
26
27 doc = PDFDoc.new(input_path + "form1.pdf")
28 doc.InitSecurityHandler()
29
30 itr = doc.GetFieldIterator()
31 while itr.HasNext() do
32 puts "Field name: " + itr.Current().GetName()
33 puts "Field partial name: " + itr.Current().GetPartialName()
34
35 print "Field type: "
36 type = itr.Current().GetType()
37 if type == Field::E_button
38 puts "Button"
39 elsif type == Field::E_check
40 puts "Check"
41 elsif type == Field::E_radio
42 puts "Radio"
43 elsif type == Field::E_text
44 puts "Text"
45 elsif type == Field::E_choice
46 puts "Choice"
47 elsif type == Field::E_signature
48 puts "Signiture"
49 elsif type == Field::E_null
50 puts "Null"
51 end
52
53 puts "------------------------------"
54 itr.Next()
55 end
56
57 doc.Close()
58 puts "Done."
59
60 # Example 2
61 # Import XFDF into FDF, then merge data from FDF into PDF
62
63 # XFDF to FDF
64 # form fields
65 puts "Import form field data from XFDF to FDF."
66
67 fdf_doc1 = FDFDoc.CreateFromXFDF(input_path + "form1_data.xfdf")
68 fdf_doc1.Save(output_path + "form1_data.fdf")
69
70 # annotations
71 puts "Import annotations from XFDF to FDF."
72
73 fdf_doc2 = FDFDoc.CreateFromXFDF(input_path + "form1_annots.xfdf")
74 fdf_doc2.Save(output_path + "form1_annots.fdf")
75
76 # FDF to PDF
77 # form fields
78 puts "Merge form field data from FDF."
79
80 doc = PDFDoc.new(input_path + "form1.pdf")
81 doc.InitSecurityHandler()
82 doc.FDFMerge(fdf_doc1)
83
84 # Refreshing missing appearances is not required here, but is recommended to make them
85 # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
86 doc.RefreshAnnotAppearances()
87
88 doc.Save(output_path + "form1_filled.pdf", SDFDoc::E_linearized)
89
90 # annotations
91 puts "Merge annotations from FDF."
92
93 doc.FDFMerge(fdf_doc2)
94 # Refreshing missing appearances is not required here, but is recommended to make them
95 # visible in PDF viewers with incomplete annotation viewing support. (such as Chrome)
96 doc.RefreshAnnotAppearances()
97 doc.Save(output_path + "form1_filled_with_annots.pdf", SDFDoc::E_linearized)
98 doc.Close()
99 puts "Done."
100
101
102 # Example 3
103 # Extract data from PDF to FDF, then export FDF as XFDF
104
105 # PDF to FDF
106 in_doc = PDFDoc.new(output_path + "form1_filled_with_annots.pdf")
107 in_doc.InitSecurityHandler()
108
109 # form fields only
110 puts "Extract form fields data to FDF."
111
112 doc_fields = in_doc.FDFExtract(PDFDoc::E_forms_only)
113 doc_fields.SetPDFFileName("../form1_filled_with_annots.pdf")
114 doc_fields.Save(output_path + "form1_filled_data.fdf")
115
116 # annotations only
117 puts "Extract annotations to FDF."
118
119 doc_annots = in_doc.FDFExtract(PDFDoc::E_annots_only)
120 doc_annots.SetPDFFileName("../form1_filled_with_annots.pdf")
121 doc_annots.Save(output_path + "form1_filled_annot.fdf")
122
123 # both form fields and annotations
124 puts "Extract both form fields and annotations to FDF."
125
126 doc_both = in_doc.FDFExtract(PDFDoc::E_both)
127 doc_both.SetPDFFileName("../form1_filled_with_annots.pdf")
128 doc_both.Save(output_path + "form1_filled_both.fdf")
129
130 # FDF to XFDF
131 # form fields
132 puts "Export form field data from FDF to XFDF."
133
134 doc_fields.SaveAsXFDF(output_path + "form1_filled_data.xfdf")
135
136 # annotations
137 puts "Export annotations from FDF to XFDF."
138
139 doc_annots.SaveAsXFDF(output_path + "form1_filled_annot.xfdf")
140
141 # both form fields and annotations
142 puts "Export both form fields and annotations from FDF to XFDF."
143
144 doc_both.SaveAsXFDF(output_path + "form1_filled_both.xfdf")
145
146 in_doc.Close()
147 puts "Done."
148
149 # Example 4
150 # Merge/Extract XFDF into/from PDF
151 in_doc = PDFDoc.new(input_path + "numbered.pdf")
152 in_doc.InitSecurityHandler()
153
154 puts "Merge XFDF string into PDF."
155
156 str = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?><xfdf xmlns=\"http://ns.adobe.com/xfdf\" xml:space=\"preserve\"><square subject=\"Rectangle\" page=\"0\" name=\"cf4d2e58-e9c5-2a58-5b4d-9b4b1a330e45\" title=\"user\" creationdate=\"D:20120827112326-07'00'\" date=\"D:20120827112326-07'00'\" rect=\"227.7814207650273,597.6174863387978,437.07103825136608,705.0491803278688\" color=\"#000000\" interior-color=\"#FFFF00\" flags=\"print\" width=\"1\"><popup flags=\"print,nozoom,norotate\" open=\"no\" page=\"0\" rect=\"0,792,0,792\" /></square></xfdf>"
157
158 fdoc = FDFDoc.CreateFromXFDF(str)
159 in_doc.FDFMerge(fdoc)
160 in_doc.Save(output_path + "numbered_modified.pdf", SDFDoc::E_linearized)
161 puts "Merge complete."
162
163 # Extract XFDF as string
164 puts "Extract XFDF as a string."
165
166 fdoc_new = in_doc.FDFExtract(PDFDoc::E_both)
167 XFDF_str = fdoc_new.SaveAsXFDF()
168 puts "Extracted XFDF: "
169 puts XFDF_str
170 in_doc.Close()
171 puts "Extract complete."
172
173 # Example 5
174 # Read FDF files directly
175
176 doc = FDFDoc.new(output_path + "form1_filled_data.fdf")
177
178 itr = doc.GetFieldIterator()
179 while itr.HasNext() do
180 puts "Field name: " + itr.Current().GetName()
181 puts "Field partial name: " + itr.Current().GetPartialName()
182 puts "------------------------------"
183 itr.Next()
184 end
185
186 doc.Close()
187 puts "Done."
188
189 # Example 6
190 # Direct generation of FDF
191 doc = FDFDoc.new()
192
193 # Create new fields (i.r. key/value pairs
194 doc.FieldCreate("Company", Field::E_text, "PDFTron Systems")
195 doc.FieldCreate("First Name", Field::E_text, "John")
196 doc.FieldCreate("Last Name", Field::E_text, "Doe")
197
198 doc.Save(output_path + "sample_output.fdf")
199 doc.Close()
200 PDFNet.Terminate
201 puts "Done. Results saved in sample_output.fdf"

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales