Insert an Image into your PDF header with AlivePDF, well sort of.
I've been trying to figure out how to insert an image into the header of each of my PDF pages with the AlivePDF library hosted on google code, my current version is 1.4.9. The problem is that in the current release of the library there's no way to do that, at least I haven't found a way. There is a header function which you can override but the header function is called automatically before any content is inserted. You can't predict when an Image is going to load, more than likely it will be after the headers and even the pages are already created, as in my case.
So what do you do?
Something like this works well for me.
public var myPDF:PDF;
public var imgLdr:Loader;
public function createPDF() : void {
myPDF = new PDF();
myPDF.addPage();
imgLdr = new Loader();
imgLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, ImageLoadComplete);
var req:URLRequest = new URLRequest(IMAGEPATH);
imgLdr.load(req);
}
public function ImageLoadComplete( e : Event ) : void
{
// We need to manually add the image to the first page
myPDF.addImage(imgLdr, 170, 2);
// then as a page is added the anonymous function below will insert the image into each additional page
myPDF.addEventListener(PageEvent.ADDED,
function ( event : PageEvent )
{
myPDF.addImage(imgLdr, 170, 2); });
}
... Your PDF Content here
}
This routine works for manually inserted pages as well as dynamic page creation. So as you can see this doesn't actually add the image to the header it only adds an image to the top of the page as it's created.
AlivePDF is an awesome library developed by Thibault Imbert.
In my opinion this is an essential library to add to your Flex development tools.
For more Information visit the AlivePDF website for tutorials, user forums and documentation on the library.
Hope this helps
Keith

