iTextSharp need to repeat table rows with textfield dynamically

半腔热情 提交于 2020-01-16 04:04:29

问题


I am reading and filling a dynamic pdf template(having dynamic table with textfields in each cell) below way using iTextSharp [assuming I would be getting a single product info from source]:

FileStream fs = new FileStream("C:\\ABC\\DestinationFile.pdf", FileMode.Create);

PdfReader reader = new PdfReader("C:\\ABC\\SourceTemplateFile.pdf");
PdfStamper stamper = new PdfStamper(reader, fs);
xfa = stamper.AcroFields.Xfa;

string[] strProductList = productInfo.Product.Split('|');
string dName= string.Format("{0} {1}", strProductList[0], strProductList[1]);
string labeling = string.Format("{0} {1}/{2}", strProductList[4], strProductList[2], strProductList[3]);
string dID = strProductList[5];
xfa.SetNodeText(xfa.FindDatasetsNode("TextField1"), dID);
xfa.SetNodeText(xfa.FindDatasetsNode("TextField2"), dName);
xfa.SetNodeText(xfa.FindDatasetsNode("TextField3"), labeling);

TemplateFile:

And XFA XML is:

<?xml version="1.0" encoding="UTF-8"?>
<?xfa generator="AdobeLiveCycleDesigner_V11.0.0.20130303.1.892433" APIVersion="3.6.13061.0"?>
<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2014-12-05T21:49:58Z">
<template xmlns="http://www.xfa.org/schema/xfa-template/3.3/">
   <?formServer defaultPDFRenderFormat acrobat10.0dynamic?>
   <subform name="form1" layout="tb" locale="en_IN">
      <pageSet>
         <pageArea name="Page1" id="Page1">
            <contentArea x="0.25in" y="0.25in" w="576pt" h="756pt"/>
            <medium stock="default" short="612pt" long="792pt"/>
            <?templateDesigner expand 1?></pageArea>
         <?templateDesigner expand 1?></pageSet>
      <subform w="576pt" h="756pt">
         <subform name="Table1" layout="table" columnWidths="61.433mm 66.31mm 60.918mm" x="3.175mm" y="6.35mm">
            <border>
               <edge/>
            </border>
            <subform layout="row" name="HeaderRow" id="HeaderRow_ID">
               <assist role="TH"/>
               <draw h="10mm" name="Cell1">
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
                  <ui>
                     <textEdit/>
                  </ui>
                  <value>
                     <text>H1           </text>
                  </value>
                  <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
                  <para vAlign="middle" hAlign="center"/>
                  <font typeface="Myriad Pro"/>
               </draw>
               <draw h="10mm" name="Cell2">
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
                  <ui>
                     <textEdit/>
                  </ui>
                  <value>
                     <text>H2</text>
                  </value>
                  <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
                  <para vAlign="middle" hAlign="center"/>
                  <font typeface="Myriad Pro"/>
               </draw>
               <draw h="10mm" name="Cell3">
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
                  <ui>
                     <textEdit/>
                  </ui>
                  <value>
                     <text>H3</text>
                  </value>
                  <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
                  <para vAlign="middle" hAlign="center"/>
                  <font typeface="Myriad Pro"/>
               </draw>
               <border>
                  <edge presence="hidden"/>
               </border>
               <occur max="-1"/>
               <?templateDesigner expand 1?></subform>
            <subform layout="row" name="Row1">
               <assist role="TR"/>
               <field name="TextField1" w="61.433mm" minH="10mm">
                  <ui>
                     <textEdit>
                        <border presence="hidden"/>
                        <margin/>
                     </textEdit>
                  </ui>
                  <font typeface="Myriad Pro"/>
                  <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>
                  <para vAlign="middle"/>
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
               </field>
               <field name="TextField2" w="66.31mm" minH="10mm">
                  <ui>
                     <textEdit>
                        <border presence="hidden"/>
                        <margin/>
                     </textEdit>
                  </ui>
                  <font typeface="Myriad Pro"/>
                  <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>
                  <para vAlign="middle"/>
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
               </field>
               <field name="TextField3" w="60.918mm" minH="10mm">
                  <ui>
                     <textEdit>
                        <border presence="hidden"/>
                        <margin/>
                     </textEdit>
                  </ui>
                  <font typeface="Myriad Pro"/>
                  <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>
                  <para vAlign="middle"/>
                  <border>
                     <edge/>
                     <corner thickness="0.1778mm"/>
                  </border>
               </field>
               <border>
                  <edge presence="hidden"/>
               </border>
               <?templateDesigner expand 1?></subform>
            <keep intact="contentArea"/>
            <overflow leader="HeaderRow"/>
            <?templateDesigner rowpattern first:1, next:1, firstcolor:f0f0f0, nextcolor:ffffff, apply:0?>
            <?templateDesigner expand 1?></subform>
         <?templateDesigner expand 1?></subform>
      <proto/>
      <desc>
         <text name="version">11.0.0.20130303.1.892433.887364</text>
      </desc>
      <?templateDesigner expand 1?></subform>
   <?templateDesigner DefaultPreviewDynamic 1?>
   <?templateDesigner DefaultRunAt client?>
   <?templateDesigner Grid show:1, snap:1, units:0, color:ff8080, origin:(0,0), interval:(125000,125000)?>
   <?templateDesigner DefaultCaptionFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?>
   <?templateDesigner DefaultValueFontSettings face:Myriad Pro;size:10;weight:normal;style:normal?>
   <?templateDesigner DefaultLanguage JavaScript?>
   <?acrobat JavaScript strictScoping?>
   <?templateDesigner Zoom 102?>
   <?templateDesigner WidowOrphanControl 0?>
   <?templateDesigner FormTargetVersion 33?>
   <?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?>
   <?templateDesigner SaveTaggedPDF 1?>
   <?templateDesigner SavePDFWithEmbeddedFonts 1?></template>
<config xmlns="http://www.xfa.org/schema/xci/3.0/">
   <agent name="designer">
      <!--  [0..n]  -->
      <destination>pdf</destination>
      <pdf>
         <!--  [0..n]  -->
         <fontInfo/>
      </pdf>
   </agent>
   <present>
      <!--  [0..n]  -->
      <pdf>
         <!--  [0..n]  -->
         <fontInfo/>
         <version>1.7</version>
         <adobeExtensionLevel>8</adobeExtensionLevel>
      </pdf>
      <xdp>
         <packets>*</packets>
      </xdp>
   </present>
</config>
<localeSet xmlns="http://www.xfa.org/schema/xfa-locale-set/2.7/">
   <locale name="en_IN" desc="English (India)">
      <calendarSymbols name="gregorian">
         <monthNames>
            <month>January</month>
            <month>February</month>
            <month>March</month>
            <month>April</month>
            <month>May</month>
            <month>June</month>
            <month>July</month>
            <month>August</month>
            <month>September</month>
            <month>October</month>
            <month>November</month>
            <month>December</month>
         </monthNames>
         <monthNames abbr="1">
            <month>Jan</month>
            <month>Feb</month>
            <month>Mar</month>
            <month>Apr</month>
            <month>May</month>
            <month>Jun</month>
            <month>Jul</month>
            <month>Aug</month>
            <month>Sep</month>
            <month>Oct</month>
            <month>Nov</month>
            <month>Dec</month>
         </monthNames>
         <dayNames>
            <day>Sunday</day>
            <day>Monday</day>
            <day>Tuesday</day>
            <day>Wednesday</day>
            <day>Thursday</day>
            <day>Friday</day>
            <day>Saturday</day>
         </dayNames>
         <dayNames abbr="1">
            <day>Sun</day>
            <day>Mon</day>
            <day>Tue</day>
            <day>Wed</day>
            <day>Thu</day>
            <day>Fri</day>
            <day>Sat</day>
         </dayNames>
         <meridiemNames>
            <meridiem>AM</meridiem>
            <meridiem>PM</meridiem>
         </meridiemNames>
         <eraNames>
            <era>BC</era>
            <era>AD</era>
         </eraNames>
      </calendarSymbols>
      <datePatterns>
         <datePattern name="full">EEEE D MMMM YYYY</datePattern>
         <datePattern name="long">D MMMM YYYY</datePattern>
         <datePattern name="med">DD-MMM-YY</datePattern>
         <datePattern name="short">DD/MM/YY</datePattern>
      </datePatterns>
      <timePatterns>
         <timePattern name="full">h:MM:SS A Z</timePattern>
         <timePattern name="long">h:MM:SS A Z</timePattern>
         <timePattern name="med">h:MM:SS A</timePattern>
         <timePattern name="short">h:MM A</timePattern>
      </timePatterns>
      <dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>
      <numberPatterns>
         <numberPattern name="numeric">z,zz,zz9.zzz</numberPattern>
         <numberPattern name="currency">$ z,zz,zz9.99</numberPattern>
         <numberPattern name="percent">z,zz,zz9%</numberPattern>
      </numberPatterns>
      <numberSymbols>
         <numberSymbol name="decimal">.</numberSymbol>
         <numberSymbol name="grouping">,</numberSymbol>
         <numberSymbol name="percent">%</numberSymbol>
         <numberSymbol name="minus">-</numberSymbol>
         <numberSymbol name="zero">0</numberSymbol>
      </numberSymbols>
      <currencySymbols>
         <currencySymbol name="symbol">Rs.</currencySymbol>
         <currencySymbol name="isoname">INR</currencySymbol>
         <currencySymbol name="decimal">.</currencySymbol>
      </currencySymbols>
      <typefaces>
         <typeface name="Myriad Pro"/>
         <typeface name="Minion Pro"/>
         <typeface name="Courier Std"/>
         <typeface name="Adobe Pi Std"/>
         <typeface name="Adobe Hebrew"/>
         <typeface name="Adobe Arabic"/>
         <typeface name="Adobe Thai"/>
         <typeface name="Kozuka Gothic Pro-VI M"/>
         <typeface name="Kozuka Mincho Pro-VI R"/>
         <typeface name="Adobe Ming Std L"/>
         <typeface name="Adobe Song Std L"/>
         <typeface name="Adobe Myungjo Std M"/>
      </typefaces>
   </locale>
</localeSet>
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.2-c001 63.139439, 2011/06/07-10:39:26        ">
   <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" rdf:about="">
         <xmp:MetadataDate>2014-12-05T21:49:58Z</xmp:MetadataDate>
         <xmp:CreatorTool>Adobe LiveCycle Designer 11.0</xmp:CreatorTool>
      </rdf:Description>
      <rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/" rdf:about="">
         <pdf:Producer>Adobe LiveCycle Designer 11.0</pdf:Producer>
      </rdf:Description>
      <rdf:Description xmlns:desc="http://ns.adobe.com/xfa/promoted-desc/" rdf:about="">
         <desc:version rdf:parseType="Resource">
            <rdf:value>11.0.0.20130303.1.892433.887364</rdf:value>
            <desc:ref>/template/subform[1]</desc:ref>
         </desc:version>
      </rdf:Description>
   </rdf:RDF>
</x:xmpmeta></xdp:xdp>

But in certain cases I would be getting multiple product info from source. So, I need to repeat the rows dynamically to fill the form in "SourceTemplateFile.pdf"

Multiple row filling code:

FileStream fs = new FileStream("C:\\ABC\\DestinationFile.pdf", FileMode.Create);

        PdfReader reader = new PdfReader("C:\\ABC\\SourceTemplateFile.pdf");
        PdfStamper stamper = new PdfStamper(reader, fs);
        xfa = stamper.AcroFields.Xfa;

        int i = 1;
        foreach (ProductInfo n in iwbProds)
        {

            string[] strProductList = n.Product.Split('|');
            string dName= string.Format("{0} {1}", strProductList[0], strProductList[1]);
            string labeling = string.Format("{0} {1}/{2}", strProductList[4], strProductList[2], strProductList[3]);
            string dID = strProductList[5];
            xfa.SetNodeText(xfa.FindDatasetsNode(string.Format("TextField{0}", i++)), dID);
            xfa.SetNodeText(xfa.FindDatasetsNode(string.Format("TextField{0}", i++)), dName);
            xfa.SetNodeText(xfa.FindDatasetsNode(string.Format("TextField{0}", i++)), labeling);
        }

So, my question is how to repeat the rows dynamically in a given pdf temple? If I would get 3 product info from source then there should be 3 rows available to fill. Or Is dynamic pdf form filling supported by iTextSharp?

来源:https://stackoverflow.com/questions/27325577/itextsharp-need-to-repeat-table-rows-with-textfield-dynamically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!