简体   繁体   English

用XDP填充PDF

[英]Populating PDF with XDP

I have a fill-in PDF that used to use a simple XFDF file to populate. 我有一个用于使用简单XFDF文件填充的PDF填充文件。 Using VS.NET 2010 I read in the XFDF document and populate all the necessary information and populate the PDF using ds.WriteXML(XFDFName). 使用VS.NET 2010,我阅读了XFDF文档,并使用ds.WriteXML(XFDFName)填充了所有必要的信息并填充了PDF。 The XFDF gets written and launched via Process.Start(XFDFName). XFDF通过Process.Start(XFDFName)编写并启动。 This is all in a WinForms application. 这一切都在WinForms应用程序中。 This method has worked like a champ for a few years now. 几年来,这种方法一直是冠军。 Until now... 到现在...

The problem I have run into is I was unable to export the data to an XFDF format due to the file having been created in Adobe LiveCycle. 我遇到的问题是由于在Adobe LiveCycle中创建了文件,因此无法将数据导出为XFDF格式。 I noticed that the export options were either (1) XML or (2) XDP. 我注意到导出选项是(1)XML或(2)XDP。 In the past I'd been able to export to XFDF. 过去,我可以导出到XFDF。 No big deal I thought, just another format. 我认为没什么大不了的,只是另一种格式。 However, I have been struggling with both of these options. 但是,我一直在为这两种选择而苦苦挣扎。 After some deliberating I decided to use the XDP format. 经过深思熟虑后,我决定使用XDP格式。

I have the dataset built with all of the information I need, I am receiving an error when opening the newly created XDP using Process.Start(XDPName). 我已经用所需的所有信息构建了数据集,使用Process.Start(XDPName)打开新创建的XDP时收到错误。 Reader opens and I am greeted with the following error: "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)." Reader打开,出现以下错误: “ Adob​​e Reader无法打开'GUID_HERE.xdp',因为它不是受支持的文件类型,或者由于文件已损坏(例如,它作为电子邮件附件发送,并且没有正确解码)。”

I tried using a straight up href to the PDF, that didn't work either. 我尝试使用直接指向PDF的href,但这也不起作用。 So I opted to stick the serialized PDF in the XDP in the section. 因此,我选择在该部分中将序列化的PDF保留在XDP中。

This XDP file looks like this ( thanks Dean J ): 这个XDP文件如下所示( 感谢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>

I would think the XDP I'm generating is bogus - but to further complicate things - if I open Reader click Tools > Forms > More Form Options > Manage Form Data > Import Data and choose the XDP file that I've generated, all the fields are populated as I expect them to be. 我认为我正在生成的XDP是伪造的-但进一步使事情复杂化-如果我打开Reader,请单击工具>表单>更多表单选项>管理表单数据>导入数据,然后选择我生成的XDP文件,所有字段按我期望的那样填充。

So basically, there is obviously a disconnect somewhere: I have a XDP file with all the information in it I need. 因此,基本上,显然某个地方存在断开连接:我有一个XDP文件,其中包含我需要的所有信息。 I have a PDF form that I need to populate with the XDP file. 我有一个PDF表单,需要用XDP文件填充。 The information in the XDP properly matches all of the control names from the PDF. XDP中的信息正确匹配PDF中的所有控件名称。 But when I fire up the XDP file, Reader is telling me it's broken/not supported. 但是当我启动XDP文件时,Reader告诉我它已损坏/不受支持。 As I understand it, when you launch an XDP file it should properly launch/populate using Reader, correct? 据我了解,启动XDP文件时,应使用Reader正确启动/填充文件,对吗?

Any information would help me out immensely. 任何信息都会极大地帮助我。 Thanks. 谢谢。

I am also having a problem getting this done.... 我在完成此操作时也遇到问题。

Doing a similiar thing in VB.net 在VB.net中做类似的事情

does not seem to work when using chunk but it does work if I use href and a local file... 使用块时似乎不起作用,但是如果我使用href和本地文件,它确实起作用...

Testing Example 测试例

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

Function EncodeByte(ByVal bt() As Byte) As String Dim enc As String enc = System.Convert.ToBase64String(bt) Return enc End Function 函数EncodeByte(ByVal bt()作为字节)作为字符串Dim enc作为字符串enc = System.Convert.ToBase64String(bt)返回enc结束函数

I also want to thank Dean J for the excellent answer. 我还要感谢Dean J的出色回答。 I've used it before and it saved me lots of time and money over a commercial iText license or LiveCycle server. 我以前使用过它,它比商业iText许可证或LiveCycle服务器节省了很多时间和金钱。 In the code referenced, there is a string that is escaped: 在引用的代码中,有一个转义的字符串:

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

Note the backslashes before the double quotes. 注意双引号前的反斜杠。 Those should not be in your XML as it will make it invalid. 那些不应包含在您的XML中,因为它将使其无效。 Chances are that Dean J actually had all that code in C# or PHP so the quotes needed to be escaped for him. 可能Dean J实际上具有C#或PHP中的所有代码,因此需要为他转义引号。

Not sure if this was relevant, but I had inconsistency in getting XDP files to open when working on a merge subroutine in VBA on different occasions. 不知道这是否相关,但是在不同情况下在VBA中处理合并子例程时,无法打开XDP文件。 Sometimes it would work, other times it wouldn't. 有时它会起作用,而其他时候则不会。

Eventually I noticed this happening when Adobe Acrobat (proper - not Reader) is open already or set as default for PDF, and it was for whatever reason trying to open the XDP rather than Adobe Reader. 最终,我注意到当Adobe Acrobat(正确-不是Reader)已打开或设置为PDF的默认值时发生了这种情况,并且出于某种原因试图打开XDP而不是Adobe Reader。

The following XML works for me: 以下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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM