Showing posts with label alivepdf. Show all posts
Showing posts with label alivepdf. Show all posts

Friday, May 8, 2009

AlivePDF Tutorial : How to create a PDF file with Flex/AIR

Here, we are going to see how to use the alivepdf library written by Thibault Imbert. This library allows us to create pdf content with Flex. In this tutorial, we will be using Adobe AIR in order to save the file. So lets get started. First create a new Flex project and select Desktop Application as the type. Download the AlivePDF library here. Copy the swc file and paste it in the libs folder. Now in the main application file, type the following code.


<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
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.*;

private var pdf:PDF;
private var file:File;
[Embed( source="/assets/o-png24.png", mimeType="application/octet-stream" )]
private var pngBytes:Class;
public function generate ():void
{
var pdf:PDF = new PDF( Orientation.PORTRAIT, Unit.MM, Size.A4 );
pdf.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
var newPage:Page = new Page ( Orientation.PORTRAIT, Unit.MM, Size.A4 );
pdf.addPage( newPage );
pdf.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
pdf.addText("This is a sample text",5,15);
pdf.drawCircle(25,35,15);
pdf.addPage();
pdf.addImageStream( new pngBytes() as ByteArray );
var fs:FileStream = new FileStream();
file = File.desktopDirectory.resolvePath("testPage.pdf");
fs.open( file, FileMode.WRITE);
var bytes:ByteArray = pdf.save(Method.LOCAL);
fs.writeBytes(bytes);
fs.close();
}
]]>
</mx:Script>
<mx:Button click="generate()" label="Generate PDF" horizontalCenter="0" verticalCenter="0"/>
</mx:WindowedApplication>

and Run the application.

The main class here is PDF. The addPage method adds a new page. Similarly, there are a lot of options such as addImage, addText and also graphic methods such as drawCircle. drawRect,etc.You can look at the documentation for this given in the zip file.

Happy experimenting :D