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
Hi, I am using your code snippet given above , but still I am not able to get as I am getting error:
Cannot access a property or method of a null object reference
Since I am using 0.1.5
, I changed the image function.Can you help me out and send me correct code or correct my code. Also I ahve ato insert page numbers for all the pages created,Can you tell me how to do that. Your help is highly appreciated. I am struggling on this for my project at the client.
pdf.addPage();
imgLdr = new Loader();
imgLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, ImageLoadComplete);
var req:URLRequest = new URLRequest(“/assets/123.jpg”);
imgLdr.load(req);
public function ImageLoadComplete( e : Event ) : void
{
pdf.addImage(imgLdr,new Resize(Mode.NONE,Position.RIGHT),0,0,10,5,0,1,false,ImageFormat.JPG,100,BlendMode.LIGHTEN);
// then as a page is added the anonymous function below will insert the image into each additional page
pdf.addEventListener(PageEvent.ADDED,
function ( event : PageEvent )
{ pdf.addImage(imgLdr,new Resize(Mode.NONE,Position.RIGHT),0,0,10,5,0,1,false,ImageFormat.JPG,100,BlendMode.LIGHTEN); }
);
}
hi can anyone send me the full code for printing images in the header