flex 保存成pdf 文件格式

本文介绍了一个使用Flex和AlivePDF库创建并保存PDF文件的例子。该示例展示了如何从用户输入生成一个简单的保险理赔表单,并将它导出为PDF文件。代码中包括了设置页面布局、添加文本和图片等内容。

<?xml version="1.0" encoding="utf-8"?>
<mx:Application  xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"
     viewSourceURL="srcview/index.html" backgroundColor="0x000000" layout="absolute">
 
    <mx:Script>
        <![CDATA[
            import org.alivepdf.pages.Page;
            import org.alivepdf.pdf.PDF;
            import org.alivepdf.layout.Orientation;
            import org.alivepdf.layout.Size;
            import org.alivepdf.layout.Unit;
            import org.alivepdf.display.Display;
            import org.alivepdf.saving.Method;
            import org.alivepdf.fonts.FontFamily;
            import org.alivepdf.fonts.Style;
            import org.alivepdf.colors.RGBColor;
           
            import mx.utils.UIDUtil;
           
            protected var claimPDF:PDF;
           
            [Embed(source="assets/Insuricorp-Logo.jpg", mimeType="application/octet-stream" )]
            protected var jpgBytes:Class;
           
            protected function savePDF(e:MouseEvent):void
            {
                claimPDF = new PDF(Orientation.LANDSCAPE, Unit.MM, Size.LETTER);
                claimPDF.setDisplayMode (Display.FULL_WIDTH);
 
                claimPDF.addPage();
                claimPDF.addImageStream(new jpgBytes() as ByteArray, 5, 5, 0, 0, 1);
               
                claimPDF.setFont(FontFamily.ARIAL , Style.NORMAL, 12);
                claimPDF.addText("Claimant Name: " + this.firstName.text + " " + lastName.text,10,40);
                claimPDF.addText("Date: " + this.date.text,10,50);
                claimPDF.addTextNote(48,45,100,2,"Claim Filed on: " + this.date.text + " today's date: " + new Date());
                claimPDF.addText("Policy #: " + this.policyNum.text,10,60);
                claimPDF.addText("Contact #: " + this.contact.text,10,70);
                claimPDF.addText(this.claimNum.text,10,80);
                claimPDF.addText("Claim Description:",10,90);
                claimPDF.setXY(10,95);
                claimPDF.addMultiCell(200,5,desc.text);
               
                // HERE IS HOW TO SAVE FROM FLASH PLAYER 10 OR GREATER. THIS IS USED FOR THIS SAMPLE.
                var bytes:ByteArray = claimPDF.save(Method.LOCAL);
                var f:FileReference = new FileReference();
                f.save(bytes,"tourdeflex-pdf-sample.pdf");
               
                // HERE IS HOW TO SAVE FROM AN AIR APP
                /*
                var fs:FileStream = new FileStream();
                var file: File = File.desktopDirectory.resolvePath("tourdeflex-pdf-sample.pdf");
                fs.open(file, FileMode.WRITE);
                var pdfBytes:ByteArray = claimPDF.savePDF(Method.LOCAL);
                fs.writeBytes(pdfBytes);
                fs.close();
                */     
                
                 // HERE IS HOW TO SAVE FROM FLEX APP RUNNING FLASH PLAYER < 10
                 /* NOTE: To save the PDF from a Flex application if running Flash Player version < 10, you need to call a server-side
                          script as the 2nd parameter of the save call. More information about this can be found here:
                          http://alivepdf.bytearray.org/?p=17
                claimPDF.save( ethod.REMOTE, "http://alivepdf.bytearray.org/wp-content/demos/create.php", "tourdeflex-pdf-sample.pdf");*/
             }
           
            protected function submitClaimNum():void
            {
                claimNum.text='Claim # Assigned: '+ UIDUtil.createUID();//just generate a random id for sample purposes
                this.savePDFBtn.visible=true;
            }
        ]]>
    </mx:Script>
   
    <mx:Style>
        FormItem, Label, Button {
            color: #FFFFFF;
        }
        TextInput, DateField {
            color: #000000;
        }
    </mx:Style>   
    <mx:Label top="10" left="10" text="Input information and press 'File Claim' to get assigned claim #, then press Save to PDF." color="0xCCCCCC"/>   
     <mx:Label fontSize="18" text="Insurance Claim Form" left="10" top="30"/>
    <mx:HBox width="100%" height="100%" top="40" left="10" color="0x000000">
        <mx:Form id="form1" width="283" height="300">
            <mx:FormItem label="First Name:">
                <mx:TextInput id="firstName"/>
            </mx:FormItem>
            <mx:FormItem label="Last Name:">
                <mx:TextInput id="lastName"/>
            </mx:FormItem>
            <mx:FormItem label="Date:">
                <mx:DateField id="date" />
            </mx:FormItem>
            <mx:FormItem label="Policy #:">
                <mx:TextInput id="policyNum" />
            </mx:FormItem>
            <mx:FormItem label="Contact #:">
                <mx:TextInput id="contact" maxChars="12"/>
            </mx:FormItem>
        </mx:Form>   
   
        <mx:Canvas top="10" left="10" width="300" height="100%">
            <mx:Label text="Description:" y="2"/>
            <mx:TextArea id="desc" y="18" height="133" width="205"/>   
        </mx:Canvas>
    </mx:HBox>
   
    <mx:HBox top="205" left="185">
        <mx:Button label="File Claim" click="submitClaimNum()"/>
        <mx:Button horizontalCenter="0" click="savePDF(event)" label="Save to PDF" id="savePDFBtn" visible="false"/>
    </mx:HBox>
    <mx:Label id="claimNum" top="238" left="83"/>
   
</mx:Application>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值