0

我有一个具有 DropDownList 的动态 PDF 表单。我正在使用 iTextSharp 尝试修改 PDF 中的值,然后再将其发送给客户端。根据这个问题的答案,这就是我正在尝试的:

PdfReader reader = new PdfReader(myPdfPath);
XmlDocument xdoc = reader.AcroFields.Xfa.DomDocument;
XmlNode dropdown = xdoc.SelectSingleNode("/*/template/subform[@name='form1']/subform[@name='form2']/field[@name='DropDownList1']");

但无论我使用什么 XPath 表达式(例如://subformfield[@name='DropDownList1']),SelectSingleNode总是返回null,并SelectNodes返回一个空列表。

我在这里做错了吗?如果有更好的方法可以做到这一点,我很想知道。

这是一些xml(我想要字段节点):

<?xml version="1.0" encoding="UTF-8"?>

<xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2013-01-18T13:22:31Z" uuid="3c6141a4-56b4-4f6e-8a2d-4519050f1c69">  

<template xmlns="http://www.xfa.org/schema/xfa-template/3.0/">  
<?formServer defaultPDFRenderFormat acrobat9.1dynamic?>  
    <subform name="form1" layout="tb" locale="en_US" restoreState="auto"> 
        <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 0?> 
            </pageArea>
            <?templateDesigner expand 0?> 
        </pageSet>  
        <subform w="576pt" h="756pt" name="form2"> 
            <field name="DropDownList1" y="22.225mm" x="6.35mm" w="62mm" h="9mm"> 
                <ui> 
                    <choiceList> 
                        <border> 
                            <edge stroke="lowered"/> 
                        </border>  
                        <margin/> 
                    </choiceList> 
                </ui>  
                <font typeface="Myriad Pro"/>  
                <margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/>  
                <para vAlign="middle"/>  
                <caption reserve="25mm"> 
                    <para vAlign="middle"/>  
                    <value> 
                        <text>Drop-down List</text> 
                    </value> 
                </caption>  
                <items save="1"> 
                    <text>Item 1</text>  
                    <text>Item 2</text>  
                    <text>Item 3</text> 
                </items> 
            </field>  
        </subform>  
        <proto/>  
        <desc> 
            <text name="version">10.0.2.20120224.1.869952.867557</text> 
        </desc>
        <?templateDesigner expand 1?>
        <?renderCache.subset "Myriad Pro" 0 0 ISO-8859-1 4 112 28 0001000E001200130023002400250027002D0033003500420044004500460049004A004C004D004F00500051005300540055005600580059 -12BCDFLRTacdehiklnoprstuwx?>
    </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 WidowOrphanControl 0?>
    <?templateDesigner SaveTaggedPDF 1?>
    <?templateDesigner SavePDFWithEmbeddedFonts 1?>
    <?templateDesigner FormTargetVersion 30?>
    <?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?>
    <?templateDesigner Zoom 92?>
</template>  
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
    <annots/>
</xfdf>
<datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/"></datasets>
</xdp:xdp>
4

1 回答 1

1

您的 XPath 不起作用的原因是大多数 XML 都在 namespace 中http://www.xfa.org/schema/xfa-template/3.0/

在这种情况下,适当的做法是创建XmlNamespaceManager并声明命名空间,然后在 XPath 中使用它们。请参阅此帖子以获取解释。

PdfReader reader = new PdfReader(myPdfPath);
XmlDocument xdoc = reader.AcroFields.Xfa.DomDocument;
XmlNamespaceManager nsm = new XmlNamespaceManager(xdoc.NameTable);
nsm.AddNamespace("xfa", "http://www.xfa.org/schema/xfa-template/3.0/");
XmlNode dropdown = 
  xdoc.SelectSingleNode("/*/xfa:template/xfa:subform[@name='form1']/xfa:subform[@name='form2']/xfa:field[@name='DropDownList1']",
  nsm);

我也没有看到subform名为“form2”的元素。那是您刚刚没有包含在您的 XML 示例中的部分吗?

于 2013-01-22T14:36:32.127 回答