Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Generate XFDF data for use in filling a PDF form, with XQuery; see http://joewiz.org/2014/02/13/filling-pdf-forms-with-pdftk-xfdf-and-xquery/
xquery version "3.0";
import module namespace functx="http://www.functx.com";
(: Prepare XFDF data to use with PDFtk to populate a blank form SF702,
e.g., http://www.archives.gov/isoo/security-forms/sf702.pdf,
with data for each month of the year :)
let $data-collection := xmldb:create-collection('/db', 'sf702')
let $year := 2014
let $months := (1 to 12) ! functx:pad-integer-to-length(., 2)
return
for $month in $months
let $first-day := xs:date(string-join(($year, $month, '01'), '-'))
let $days := (1 to functx:days-in-month($first-day)) ! functx:pad-integer-to-length(., 2)
let $month-year := $month || '/' || substring($year, 3, 2)
let $xfdf :=
<xfdf xmlns="http://ns.adobe.com/xfdf/">
<fields>
<field name="form1[0].#subform[0].MONTHYEAR[0]">
<value>{$month-year}</value>
</field>
<field name="form1[0].#subform[0].MONTHYEAR2[0]">
<value>{$month-year}</value>
</field>
{
for $day at $n in $days
let $field-name :=
if ($n le 22) then
'form1[0].#subform[0].Date' || $n || '[0]'
else
'form1[0].#subform[0].Date' || $n - 22 || '[1]'
return
<field name="{$field-name}">
<value>{$day}</value>
</field>
}
</fields>
</xfdf>
let $filename := string-join(('sf702', $year, $month), '-') || '.xml'
return
xmldb:store($data-collection, $filename, $xfdf)
<xfdf xmlns="http://ns.adobe.com/xfdf/">
<fields>
<field name="form1[0].#subform[0].MONTHYEAR[0]">
<value>01/14</value>
</field>
<field name="form1[0].#subform[0].MONTHYEAR2[0]">
<value>01/14</value>
</field>
<field name="form1[0].#subform[0].Date1[0]">
<value>01</value>
</field>
<field name="form1[0].#subform[0].Date2[0]">
<value>02</value>
</field>
<field name="form1[0].#subform[0].Date3[0]">
<value>03</value>
</field>
<field name="form1[0].#subform[0].Date4[0]">
<value>04</value>
</field>
<field name="form1[0].#subform[0].Date5[0]">
<value>05</value>
</field>
<field name="form1[0].#subform[0].Date6[0]">
<value>06</value>
</field>
<field name="form1[0].#subform[0].Date7[0]">
<value>07</value>
</field>
<field name="form1[0].#subform[0].Date8[0]">
<value>08</value>
</field>
<field name="form1[0].#subform[0].Date9[0]">
<value>09</value>
</field>
<field name="form1[0].#subform[0].Date10[0]">
<value>10</value>
</field>
<field name="form1[0].#subform[0].Date11[0]">
<value>11</value>
</field>
<field name="form1[0].#subform[0].Date12[0]">
<value>12</value>
</field>
<field name="form1[0].#subform[0].Date13[0]">
<value>13</value>
</field>
<field name="form1[0].#subform[0].Date14[0]">
<value>14</value>
</field>
<field name="form1[0].#subform[0].Date15[0]">
<value>15</value>
</field>
<field name="form1[0].#subform[0].Date16[0]">
<value>16</value>
</field>
<field name="form1[0].#subform[0].Date17[0]">
<value>17</value>
</field>
<field name="form1[0].#subform[0].Date18[0]">
<value>18</value>
</field>
<field name="form1[0].#subform[0].Date19[0]">
<value>19</value>
</field>
<field name="form1[0].#subform[0].Date20[0]">
<value>20</value>
</field>
<field name="form1[0].#subform[0].Date21[0]">
<value>21</value>
</field>
<field name="form1[0].#subform[0].Date22[0]">
<value>22</value>
</field>
<field name="form1[0].#subform[0].Date1[1]">
<value>23</value>
</field>
<field name="form1[0].#subform[0].Date2[1]">
<value>24</value>
</field>
<field name="form1[0].#subform[0].Date3[1]">
<value>25</value>
</field>
<field name="form1[0].#subform[0].Date4[1]">
<value>26</value>
</field>
<field name="form1[0].#subform[0].Date5[1]">
<value>27</value>
</field>
<field name="form1[0].#subform[0].Date6[1]">
<value>28</value>
</field>
<field name="form1[0].#subform[0].Date7[1]">
<value>29</value>
</field>
<field name="form1[0].#subform[0].Date8[1]">
<value>30</value>
</field>
<field name="form1[0].#subform[0].Date9[1]">
<value>31</value>
</field>
</fields>
</xfdf>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment