HDRI Community Site

Welcome to HDRI Community Site Sign in | Join | Help
in Search

Ray's blog

  • Adnug Code Camp Presentations

    We really enjoyed being presenters and attendees at the Adnug Code Camp. Jeffrey Palermo did an awesome job of organizing the event!
    You can download the presentations that Chad and I gave here: http://community.hdri.net/files/4/adnug_code_camp_presentations/default.aspx
  • JavaScript String.format()

    Here's a little bit of javascript that I came up with to aid in formatting strings.

    String.prototype.format = function()

    {

        var str = this;

     

        for(var i=0;i<arguments.length;i++)

        {

            var re = new RegExp('\\{' + (i) + '\\}','gm');

            str = str.replace(re, arguments[i]);

        }

     

        return str;

    }

    This will allow you to call the format() method on any string and pass in replacement parameters.  Here's an example:

    var firstName = 'Ray';

    var lastName = 'Houston';

    var hello = 'Hello. My name is {0} {1}.'.format( firstName, lastName );


    If you don't like the way that looks (and want a more .NET looking syntax), you can create a static method on the String type like so:

    String.format = function()

    {

        if( arguments.length == 0 )

            return null;

     

        var str = arguments[0];

     

        for(var i=1;i<arguments.length;i++)

        {

            var re = new RegExp('\\{' + (i-1) + '\\}','gm');

            str = str.replace(re, arguments[i]);

        }

     

        return str;

    }


    Then the example can be written as:

    var firstName = 'Ray';

    var lastName = 'Houston';

    var hello = String.format('Hello. My name is {0} {1}.', firstName, lastName);


    One thing this method doesn't do is that it doesn't allow for escaping '{' or '}' characters. It will still replace {{0}} instead of skipping it like .NET does.
  • Serving Fillable PDF Forms

    Recently had a requirement to have a web application serve up a Fillable PDF Form that was to be filled in using data from a database. I found that this could be done by sending data in Adobe’s XDP format to the client browser. XDP is an XML version of a PDF document.  It also allows a linked PDF to be supplied with embedded data.  I could never locate an official schema definition, but a simple one looks something like:

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

    <?xfa generator="XFA2_4" APIVersion="2.4.5234.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>

                <!-- Place data here to bind to PDF -->

            </xfa:data>

        </xfa:datasets>

        <pdf href="http://localhost/PdfForms/MyForm.pdf"

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

    </xdp:xdp>

    You can see the link to the actual PDF in the href attribute of the <pdf> tag.  This is the location of the “to be filled” PDF.

    By sending this to the browser with a content type of "application/vnd.adobe.xfdf", Acrobat Reader will then open the PDF specified and attempt to fill it with the data provided in the <xfa:data> tags. You can simply supply the PDF field names and values to fill in data (<firstname>Ray</firstname>), but in my case I had more complex forms with repeatable sections so I wanted something a little more structured. It turns out that Adobe Designer 7 allows you to databind fields against an XSD.  You can drag and drop the XSD elements directly on fields in the PDF. Your XDP may then contain data in the format specified by your XSD like so:

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

    <?xfa generator="XFA2_4" APIVersion="2.4.5234.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>

                <MyForm xmlns:xsd="http://www.w3.org/2001/XMLSchema"

                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

                    xmlns="http://hdri.net/MyForm.xsd">

                    <FirstName>Ray</FirstName>

                    <LastName>Houston</LastName>

                </MyForm>

            </xfa:data>

        </xfa:datasets>

        <pdf href="http://localhost/PdfForms/MyForm.pdf"

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

    </xdp:xdp>

    MyForm is a simple object that contains properties for each field of data.  By using the XSD.exe utility, I was able to generate the XSD I needed to do my databinding in Adobe Designer.

    At runtime, I serialized MyForm to XML and did an XSLT transformation to the XDP format which I served to the client using a custom HttpHandler.

This Blog

Post Calendar

<July 2008>
SuMoTuWeThFrSa
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789

Syndication

Powered by Community Server, by Telligent Systems