How can I merge data into an XDP file and return a PDF (via .NET)?

mbrion picture mbrion · Nov 18, 2009 · Viewed 8.6k times · Source

My team has a template (XDP) that we've created with the Adobe LiveCycle designer.

The situation:

  • We are replacing an old Acrobat form (XFDF format) with this LiveCycle form as part of a much larger upgrade

  • The current Acrobat form is dynamically populated with basic data and delivered to the user as a PDF (the user clicks a link and a PDF opens on their machine pre-populated with basic demographic details)

  • The current process looks something like: user clicks, .NET app retrieves data, .NET app retrieves PDF from file system, .NET app serializes data into template in XFDF form, ASP.NET app sends an HTTP response back to the user in XFDF format, the user views the PDF in Adobe Reader

We would like to maintain a roughly analogous process with the new XDP format.

For the record, we DO have access to the LiveCycle services that do this type of work (e.g. the Render service). That said, we would like to create this type of serialization in .NET for a few reasons: we have limited LiveCycle licenses and will struggle to access the LiveCycle services due to their location in the enterprise.

Any thoughts (even "stop doing that, you're insane") are appreciated.

Answer

Dean J picture Dean J · Jan 22, 2010

Hey, sorry for the slow reply, didn't see this.

Create the PDF. Encode it as base64.

Create a text file, foo.xdp:

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

Insert the XML here that is your dynamic data, matching the dynamic fields in the PDF.

        </xfa:data>
    </xfa:datasets>
    <pdf xmlns=\"http://ns.adobe.com/xdp/pdf/\"><document>
       <chunk>

Without putting any characters after the tag, insert the base64 encoded PDF.

        </chunk>
    </document></pdf>
</xdp:xdp>

And, lo and behold, you have a valid XDP. Adobe Reader will accept this and display the PDF with your values in it.

In Java, if you have a Properties with the connection details in it, the following code can use Livecycle to turn an XDP to a PDF, although your mileage might vary in C#.

        // Create a ServiceClientFactory object
        ServiceClientFactory myFactory = ServiceClientFactory
                .createInstance(connectionProperties);

        // Create a PDF Utility client
        PDFUtilityServiceClient pdfClient = new PDFUtilityServiceClient(myFactory);

        // Specify an XDP file to convert to a PDF document
        ByteArrayInputStream bais = new ByteArrayInputStream(xdp.getBytes("ASCII"));
        Document inDoc = new Document(bais);

        // Convert the XDP file to a PDF document
        Document pdf = pdfClient.convertXDPtoPDF(inDoc);

        // Return the PDF as an InputStream.
        return pdf.getInputStream();