alive pdf 基本用法
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">
<fx:Script>
<![CDATA[
import mx.collections.ArrayCollection;
import org.alivepdf.colors.RGBColor;
import org.alivepdf.data.Grid;
import org.alivepdf.data.GridColumn;
import org.alivepdf.display.Display;
import org.alivepdf.fonts.CoreFont;
import org.alivepdf.fonts.IFont;
import org.alivepdf.images.ColorSpace;
import org.alivepdf.layout.Align;
import org.alivepdf.layout.Layout;
import org.alivepdf.layout.Mode;
import org.alivepdf.layout.Orientation;
import org.alivepdf.layout.Position;
import org.alivepdf.layout.Resize;
import org.alivepdf.layout.Size;
import org.alivepdf.layout.Unit;
import org.alivepdf.links.HTTPLink;
import org.alivepdf.links.ILink;
import org.alivepdf.pages.Page;
import org.alivepdf.pdf.PDF;
import org.alivepdf.saving.Method;
private var myPDF :PDF;
[Bindable]
private var dp:ArrayCollection = new ArrayCollection();
[Embed( source="img/aa.jpg", mimeType="application/octet-stream" )]
private var imageBigStream:Class;
[Embed( source="img/cat.jpg", mimeType="application/octet-stream" )]
private var imageBigStream2:Class;
public function generatePDF(pEvt:MouseEvent):void{
//orientation:String = Portrait, unit:String = Mm, autoPageBreak:Boolean = true, pageSize:Size = null, rotation:int = 0
myPDF = new PDF ( Orientation.PORTRAIT, Unit.MM, Size.A4, 0);
//**添加内容时首先添加页**
myPDF.addPage();
//1.添加书签
myPDF.addBookmark("A bookmark",0,1);
myPDF.addBookmark("A.1 bookmark",1,30);
//2.添加文本
//a.addtext
myPDF.addText ('addText', 5, 5);
myPDF.textStyle( new RGBColor(0x990000), 1 );
//b.writetext
myPDF.writeText(1,"writeText");
//c.htmltext
myPDF.writeFlashHtmlText(8, " <b>Test </b ><br /> <br /> Test");
myPDF.addPage();
myPDF.addBookmark("B bookmark",0,1);
// a.普通文本
myPDF.addCell(20,5,'boder 1 ln 1 !',1,1,"1");
// b.链接文本
var iLink :ILink = new HTTPLink("http://www.alivepdf.org");
myPDF.addCell(50,70,'A clikable cell !', 1, 1, null, 0, iLink);
//4.添加表格
myPDF.addPage();
myPDF.addBookmark("C bookmark",0,1);
// a.数据获取
this.initData();
// b.创建表格头
var gridColumnAge:GridColumn = new GridColumn("City", "city", 30, Align.LEFT, Align.LEFT);
var gridColumnEmail:GridColumn = new GridColumn("E-Mail", "email", 40, Align.LEFT, Align.LEFT);
var gridColumnFirstName:GridColumn = new GridColumn("First Name", "firstName", 40, Align.LEFT, Align.LEFT);
var gridColumnLastName:GridColumn = new GridColumn("Last Name", "lastName", 45, Align.LEFT, Align.LEFT);
var columns:Array = new Array ( gridColumnAge, gridColumnEmail, gridColumnFirstName, gridColumnLastName );
// c.创建表格
var grid:Grid = new Grid( dp.toArray(), 200, 120, new RGBColor ( 0xCCCCCC ), new RGBColor (0xCCCCCC), true, new RGBColor(0x887711), .1, null, columns );
var font:IFont = new CoreFont();
// d.添加表格之前 必须设置这两个属性
myPDF.setFont( font, 14 );
myPDF.textStyle( new RGBColor(0x990000), 1 );
// e.添加表格
myPDF.addGrid( grid );
//5.添加图片
myPDF.addPage();
myPDF.addBookmark("D bookmark",0,80);
// a.定义文件流
var image:ByteArray = new imageBigStream2() as ByteArray;
//b.添加图片
myPDF.addImageStream( image, ColorSpace.DEVICE_RGB, new Resize ( Mode.RESIZE_PAGE, Position.CENTERED ) );
var p:Page = new Page(Orientation.LANDSCAPE);
//addImage 添加对象成为图片 画当前界面
myPDF.addPage(p);
myPDF.addBookmark("E bookmark",0,1);
myPDF.addImage( this );
//6.画图形
myPDF.addPage();
myPDF.addBookmark("F bookmark",0,1);
myPDF.lineStyle ( new RGBColor ( 0x998800 ), 0 );
myPDF.moveTo ( 100, 100 );
myPDF.lineTo ( 200, 200 );
myPDF.end();
myPDF.drawCircle( 100, 100, 50 );
myPDF.drawRect( new Rectangle ( 10, 10, 30, 30 ) );
//2个属性
var totalFonts:int = myPDF.totalFonts;
var totalPages:int = myPDF.totalPages;
trace("totalFonts:"+totalFonts);
trace("totalPages:"+totalPages);
//本地打印方式
var fs:FileStream = new FileStream();
// var f:File = File.applicationStorageDirectory.resolvePath(p);
var file:File = File.desktopDirectory.resolvePath("pdf/gennerated.pdf");
var bs:ByteArray = myPDF.save(Method.LOCAL);
fs.open( file, FileMode.WRITE);
fs.writeBytes(bs);
fs.close();
//打开预览窗口
var option:NativeWindowInitOptions= new NativeWindowInitOptions();
var rect:Rectangle = new Rectangle(0,100,1024,658);
var loader:HTMLLoader = HTMLLoader.createRootWindow(true,option,true,rect);
loader.window.document.title = "title";
var request:URLRequest = new URLRequest(file.url);
loader.load(request);
}
public function initData():void{
dp.addItem( { firstName : "Mike", lastName : "Chambers", email : "[email protected]", city : "San Francisco" } );
dp.addItem( { firstName : "Lee", lastName : "Brimelow", email : "[email protected]", city : "San Francisco" } );
dp.addItem( { firstName : "Kevin", lastName : "Hoyt", email : "[email protected]", city : "San Francisco" } );
dp.addItem( { firstName : "Serge", lastName : "Jespers", email : "[email protected]", city : "Belgium" } );
}
]]>
</fx:Script>
<s:Button x="263" y="251" label="打印" click="generatePDF(event)"/>
<s:RichText fontFamily="main" fontSize="20" kerning="off" lineHeight="120%"
whiteSpaceCollapse="preserve"
right ="10" bottom="10"
text ="Copyright 2013 by 0o晓月メ"/>
</s:WindowedApplication>