It is built using Flash Builder 4 and the following steps:
Setup
- create a new Air Project
- add the .swc from Alive PDF v. 0.1.4.9 (0.1.5 is incompatible with AlivePDFTemplate at the moment)
- add the cl folder from src of AlivePDF Template into your src folder
Import
add the following import statements, just after [CDATA[ in your <fx:Script> tag
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
import org.alivepdf.fonts.*;
import org.alivepdf.pages.Page;
import org.alivepdf.display.Display;
import org.alivepdf.layout.*;
import cl.huerta.pdf.PDFTemplate;
import cl.huerta.pdf.PDFi;
import cl.huerta.pdf.PDFParser;
import cl.huerta.pdf.decoders.ASCII85Decode;
import cl.huerta.pdf.PDFiPDFParser;
import cl.huerta.pdf.decoders.LZWDecode;
import cl.huerta.pdf.PDFContext;
Button
Add a button to control the process from the Design view (click on Design tab on top of your source). Click and drag the button from the available elements, then add a clickhandler under properties (on your right, click on the lightning bolt)
Code
Add the following code under the button click handler (based on the example given for AlivePDF Template).
//Must create a new PDFi instance (not PDF)
var myPDF : PDFi = new PDFi ( Orientation.PORTRAIT, Unit.MM, Size.LETTER );
// Import a file!!!! Just set the source file to an existing PDF
var pagecount:Number = myPDF.setSourceFile(File.desktopDirectory.nativePath + File.separator + "test.pdf" );
// Import a page. The first argument is the page number, the second the way it will be imported
var tplidx:Number = myPDF.importPage(1, '/MediaBox');
//This is important, you MUST add a page first!!!!
myPDF.addPage();
//Use the template in the current page
myPDF.useTemplate(tplidx, 10, 10, 90);
// This is not part of the original PDF. Just a test ^^
myPDF.setFont( FontFamily.ARIAL, Style.BOLD );
myPDF.setFontSize ( 18 );
myPDF.setXY( 10, 40 );
myPDF.addMultiCell ( 300, 1, "This is my PDF Headline" );
//Save the file
var f : FileStream = new FileStream();
var file : File = File.desktopDirectory.resolvePath("MyPDFi.pdf");
f.open( file, FileMode.WRITE);
var bytes : ByteArray = myPDF.save(Method.LOCAL);
f.writeBytes(bytes);
f.close();
Compile & Run
It will show an AIR window, with a button in it. On clicking the button the file "test.pdf" from your desktop (you should have placed one there!) will be added as a template to a new file MyPDFi.pdf.
Download
Download the project as exported (and zipped) by me http://blog.ideaday.de/max/wp-content/uploads/2011/10/ptair.zip