A PDF import library for AlivePDF

Sometime ago I decided to create a PDF converter that allowed users to select the sections of PDF to be included in the final PDF. The idea is quite similar to PaperCrop, and actually I modified PaperCrop first to extract text and not only create images (if you want a version of my modified version contact me ^^). Anyway, I felt that PaperCrop was an excellent software (I still does), but it is too difficult to manage C++ code for me, and the lack of versions for Linux and MacOSX, motivated me to create a new software to do that.

Due to my background, my first thought was Java. I am (modestly apart) an expert in Java, and I feel quite comfortable using it. However, the PDF rendering tools available for Java where not good at all. I tried three: Multivalent, SUN’s PDF-Renderer and JPedal. All of them presented some problems at rendering, so I drop them… what to do then, go back to PaperCrop and C++ or try something different? I decided to try something different. I always liked the idea of mixing the desk and the web, so I decided to do it in Adobe AIR. Why AIR and not SilverLight or JavaFX? Well, as I already discussed, Java’s PDF renderers are not good. And I’m not familiar to Silverlight. Actionscript is quite similar to Java in structure, and I already programmed some stuff in Flash long time ago. So there we go, Adobe AIR was selected.

Now, my problem was what to use to develop such software. My first trouble, how to generate PDF files from actionscript. AlivePDF is THE solution for this in Adobe AIR (and Flex). And actually is quite good, and very simple to use. The only problem was that AlivePDF does not accept an existing PDF as input for a new one. The solution: to program a PDF importer ^^

AlivePDF is based on FPDF, a PHP PDF library, which is quite easy to extend. One of the extension, FPDi, allows you to import existing PDF files into a new PDF as a template. I decided to port this extension into AlivePDF, and it took me a couple of days to do so. You can download the AlivePDFTemplate library here:

Please be aware that this is a beta release. I tested with few cases only. It is licensed under the Apache 2.0 license, so you can use it and modify it the way you want.

To use it, you can use the following example as a reference, or check the FPDi website for more examples.

//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 + "FPDF_TPL-Manual-1.1.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();  

I will upload this as a new project in google code or some other place, to have a better development control. I hope you enjoy it!!!!

  1. No comments yet.

  1. October 20th, 2009
  2. January 10th, 2012
    Trackback from : Alive pdf – Flashforum

WP-SpamFree by Pole Position Marketing