【问题标题】:Populating PDF with XDP使用 XDP 填充 PDF
【发布时间】:2012-04-03 13:32:57
【问题描述】:

我有一个填充 PDF,它曾经使用一个简单的 XFDF 文件来填充。使用 VS.NET 2010,我阅读了 XFDF 文档并填充了所有必要的信息,并使用 ds.WriteXML(XFDFName) 填充了 PDF。 XFDF 通过 Process.Start(XFDFName) 编写和启动。这一切都在 WinForms 应用程序中。这种方法已经像冠军一样工作了几年。到现在……

我遇到的问题是我无法将数据导出为 XFDF 格式,因为该文件是在 Adob​​e LiveCycle 中创建的。我注意到导出选项是 (1) XML 或 (2) XDP。在过去,我能够导出到 XFDF。我认为没什么大不了的,只是另一种格式。但是,我一直在为这两种选择而苦苦挣扎。经过深思熟虑,我决定使用 XDP 格式。

我使用所需的所有信息构建了数据集,使用 Process.Start(XDPName) 打开新创建的 XDP 时收到错误消息。阅读器打开,我收到以下错误:"Adobe Reader could not open 'GUID_HERE.xdp' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)."

我尝试对 PDF 使用直接的 href,但也没有用。所以我选择将序列化的 PDF 粘贴在该部分的 XDP 中。

此 XDP 文件如下所示 (thanks Dean J):

<?xml version='1.0' encoding='UTF-8'?>
<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>
<xdp:xdp xmlns:xdp='http://ns.adobe.com/xdp/'>
    <xfa:datasets xmlns:xfa='http://www.xfa.org/schema/xfa-data/1.0/'>
        <xfa:data>
           XML is here - matching the dynamic fields in the PDF.
        </xfa:data>
    </xfa:datasets>
    <pdf xmlns=\"http://ns.adobe.com/xdp/pdf/\"><document>
       <chunk>
          Base64 encoded PDF
       </chunk>
    </document></pdf>
</xdp:xdp>

我认为我正在生成的 XDP 是伪造的 - 但更复杂的是 - 如果我打开阅读器,请单击工具 > 表单 > 更多表单选项 > 管理表单数据 > 导入数据并选择我生成的 XDP 文件,所有字段都按照我的预期填充。

因此,基本上,某处显然存在断开连接: 我有一个 XDP 文件,其中包含我需要的所有信息。 我有一个需要用 XDP 文件填充的 PDF 表单。 XDP 中的信息与 PDF 中的所有控件名称正确匹配。 但是当我启动 XDP 文件时,Reader 告诉我它已损坏/不受支持。据我了解,当您启动 XDP 文件时,它应该使用 Reader 正确启动/填充,对吗?

任何信息都会极大地帮助我。谢谢。

【问题讨论】:

    标签: .net winforms pdf adobe xdp-pdf


    【解决方案1】:

    我也遇到了问题......

    在 VB.net 中做类似的事情

    使用块时似乎不起作用,但如果我使用 href 和本地文件,它确实起作用...

    测试示例

    Public Sub BuildContent(ByVal slno As String)
    
        Dim strXML As String
    
        Dim fs As System.IO.FileStream = Nothing
        Dim bw As System.IO.BinaryWriter = Nothing
        Dim Buffer() As Byte
        'fs = New System.IO.FileStream("kpiAlert10.pdf", IO.FileMode.Create)
        'bw = New System.IO.BinaryWriter(fs)
        'Response.ContentType = "application/vnd.adobe.xdp+xml"
        '
        ' Constant XPD Header
        '
        strXML = "<?xml version='1.0' encoding='UTF-8'?>"
        strXML = strXML & "<?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>"
        strXML = strXML & "<xdp:xdp xmlns:xdp='http://ns.adobe.com/xdp/'>"
        strXML = strXML & "<xfa:datasets xmlns:xfa='http://www.xfa.org/schema/xfa-data/1.0/'>"
        strXML = strXML & "<xfa:data>"
        '
        ' Place code here to get the current Logged in user ID
        ' and perform a query to the back end database and filter by ID
        ' then generate the following XML Data using the resultant Recordset ...etc.
        '
    
        strXML = strXML & "<transaction><kpi><name>Ego ille</name><data><sn>Si manu vacuas</sn><amt>Apros tres et quidem</amt><delta>Mirum est</delta></data></kpi></transaction>"
        '
        '
        '
        strXML = strXML & "</xfa:data>"
        strXML = strXML & "</xfa:datasets>"
        '
        ' Point the XPD to the PDF Form created under Adobe LiveCycle Desinger
        '
        Dim contents As String
    
        contents = EncodeFile("kpiAlert.pdf")
        'Buffer = Convert.FromBase64String(contents)
        strXML = strXML & "<pdf xmlns='http://ns.adobe.com/xdp/pdf/'>"
        strXML = strXML & "<document>"
        strXML = strXML & "<chunk>" & contents & "=</chunk>"
        strXML = strXML & "</document>"
        strXML = strXML & "</pdf>"
    
        'strXML = strXML & "<pdf href='C:/kpiAlert.pdf' xmlns='http://ns.adobe.com/xdp/pdf/' />"
        '
        ' Close the XPD File
        '
        strXML = strXML & "</xdp:xdp>"
    
        Using outfile As New StreamWriter("kpiAlert_" & slno & ".pdf")
            outfile.Write(strXML.ToString())
        End Using
    
    End Sub
    
    Function EncodeFile(ByVal srcFile As String) As String
    
        Dim srcBT As Byte()
        Dim dest As String
        Dim sr As New IO.FileStream(srcFile, IO.FileMode.Open)
        ReDim srcBT(sr.Length)
        sr.Read(srcBT, 0, sr.Length)
        sr.Close()
        dest = EncodeByte(srcBT)
        Return dest
    
    End Function
    
    enter code here
    

    函数 EncodeByte(ByVal bt() As Byte) As String Dim enc As String enc = System.Convert.ToBase64String(bt) 返回编码 结束函数

    【讨论】:

      【解决方案2】:

      我还要感谢 Dean J 的出色回答。我以前使用过它,它比商业 iText 许可证或 LiveCycle 服务器为我节省了大量时间和金钱。在引用的代码中,有一个字符串被转义了:

      <pdf xmlns=\"http://ns.adobe.com/xdp/pdf/\">
      

      注意双引号前的反斜杠。这些不应该在您的 XML 中,因为它会使其无效。很有可能 Dean J 实际上拥有 C# 或 PHP 中的所有代码,因此需要为他转义引号。

      【讨论】:

        【解决方案3】:

        不确定这是否相关,但在不同情况下在 VBA 中处理合并子例程时,我无法打开 XDP 文件。有时它会起作用,有时它不会。

        最终我注意到当 Adob​​e Acrobat(正确 - 不是 Reader)已经打开或设置为 PDF 的默认值时发生这种情况,并且无论出于何种原因试图打开 XDP 而不是 Adob​​e Reader。

        以下 XML 适合我:

        <?xml version="1.0" encoding="UTF-8"?>
        <?xfa generator='AdobeDesigner_V7.0' APIVersion='2.2.4333.0'?>
        <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/">
          <xfa:datasets xmlns:xfa="http://www.xfa.org/schema/xfa-data/1.0/">
            <xfa:data>
              <PlannedCycles>999</PlannedCycles>
              <cyclenumber>1</cyclenumber>
              <ConsultantName>Dr Jonathon Hogan-Doran</ConsultantName>
              <Name>SMITH, Bob</Name>
              <URN>9795240</URN>
              <DOB>14/04/1901</DOB>
              <ward>CDCO</ward>
              <ht>100</ht>
              <wt>99</wt>
              <Diagnosis>Metastatic Adenocarcinoma</Diagnosis>
              <chemoD1>17/06/2015</chemoD1>
            </xfa:data>
          </xfa:datasets>
          <pdf xmlns="http://ns.adobe.com/xdp/pdf/" href="\\xxxxxxx.gov.au\Medical Oncology\Chemotherapy Scripts\S\SMITH, Bob- (75240) - dob 14.04.1901 - Capecitabine with Bevacizumab - Cycle 1.pdf"/>
        </xdp:xdp>
        

        【讨论】:

          猜你喜欢
          • 2011-10-05
          • 1970-01-01
          • 1970-01-01
          • 2022-07-20
          • 2014-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-20
          相关资源
          最近更新 更多