简体   繁体   中英

Populating PDF with XDP

I have a fill-in PDF that used to use a simple XFDF file to populate. Using VS.NET 2010 I read in the XFDF document and populate all the necessary information and populate the PDF using ds.WriteXML(XFDFName). The XFDF gets written and launched via Process.Start(XFDFName). This is all in a WinForms application. 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. I noticed that the export options were either (1) XML or (2) XDP. In the past I'd been able to export to 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.

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). 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)."

I tried using a straight up href to the PDF, that didn't work either. So I opted to stick the serialized PDF in the XDP in the section.

This XDP file looks like this ( 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>

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.

So basically, there is obviously a disconnect somewhere: I have a XDP file with all the information in it I need. I have a PDF form that I need to populate with the XDP file. The information in the XDP properly matches all of the control names from the PDF. But when I fire up the XDP file, Reader is telling me it's broken/not supported. As I understand it, when you launch an XDP file it should properly launch/populate using Reader, correct?

Any information would help me out immensely. Thanks.

I am also having a problem getting this done....

Doing a similiar thing in VB.net

does not seem to work when using chunk but it does work if I use href and a local file...

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

I also want to thank Dean J for the excellent answer. I've used it before and it saved me lots of time and money over a commercial iText license or LiveCycle server. 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. Chances are that Dean J actually had all that code in C# or PHP so the quotes needed to be escaped for him.

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. 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.

The following XML works for me:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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