AIR生成PDF(不通过打印机)

来源:互联网 发布:mac滴管粉底液 nc15 编辑:程序博客网 时间:2024/06/09 14:12

 新建air project,引入AlivePDF.swc 可在http://alivepdf.bytearray.org下载

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  3.     <mx:Script>
  4.         <![CDATA[
  5.         
  6.             import org.alivepdf.pdf.PDF;
  7.             import org.alivepdf.layout.Orientation;
  8.             import org.alivepdf.layout.Size;
  9.             import org.alivepdf.layout.Unit;
  10.             import org.alivepdf.display.Display;
  11.             import org.alivepdf.saving.Method;
  12.             import org.alivepdf.fonts.FontFamily;
  13.             import org.alivepdf.fonts.Style;
  14.             import org.alivepdf.colors.RGBColor;
  15.             import org.alivepdf.layout.Layout;
  16.             import org.alivepdf.images.ImageFormat;
  17.             import org.alivepdf.images.ResizeMode;
  18.             
  19.             public function generatePDF ( e:MouseEvent ):void
  20.             {
  21.             
  22.             var myPDF:PDF = new PDF ( Orientation.PORTRAIT, Unit.MM, Size.LETTER );
  23.         
  24.             myPDF.setDisplayMode( Display.FULL_PAGE, Layout.SINGLE_PAGE );
  25.             
  26.             myPDF.addPage();
  27.             
  28.             // add a background image
  29.             myPDF.addImage(dc_main,1,1,0,0,ImageFormat.JPG ,100,160,ResizeMode.FIT_TO_PAGE,BlendMode.DARKEN,false,"");
  30.             
  31.             
  32.             myPDF.setFont( FontFamily.HELVETICA, Style.BOLD );
  33.             myPDF.setFontSize ( 18 );
  34.             myPDF.setXY( 1040 );
  35.             myPDF.addMultiCell ( 3001"This is my PDF Headline" );
  36.             
  37.             // add text message
  38.         
  39.             myPDF.setFont( FontFamily.HELVETICA, Style.BOLD );
  40.             myPDF.setFontSize ( 14 );
  41.             myPDF.setXY( 1050 );
  42.             myPDF.addMultiCell ( 3004"This is my text....lots of text..." );
  43.             
  44.             // save PDF to the desktop
  45.             var f : FileStream = new FileStream();
  46.             var file : File = File.desktopDirectory.resolvePath("C:/MyPDF.pdf");
  47.             f.open( file, FileMode.WRITE);
  48.             var bytes : ByteArray = myPDF.save(Method.LOCAL);
  49.             f.writeBytes(bytes);
  50.             f.close();       
  51.             }
  52.    
  53.         ]]>
  54.     </mx:Script>
  55.             <mx:Button horizontalCenter="0" click="generatePDF(event)" label="Generate PDF" id="generate_btn" />
  56.  
  57.         <mx:Panel layout="absolute" title="Draw On Me" backgroundColor="#ffffff" borderThicknessBottom="10" left="20" right="20" top="20" bottom="20">
  58.             <mx:Canvas x="0" y="0" width="100%" height="100%" backgroundColor="#ffffff">
  59.                 <mx:Label x="265" y="242" text="nihao"/>
  60.                 <mx:Image x="79" y="97" id="dc_main" source="assets/LC_bottom_558x120.jpg"/>
  61.                 
  62.             </mx:Canvas>
  63.  
  64.         </mx:Panel>
  65. </mx:WindowedApplication>
原创粉丝点击