Flex 自定义打印控件编写

打印历来是web应用一个比较棘手的问题,幸好flex web应用是运行在flash player上的,flash player可以访问打印机,所以flex 应用可以实现比较强大的打印功能。Flex 自身提供的printjobflexprintjob相对来说用起来不是很方便,对于有严格纸张设置、翻页等打印需求,略显功能不足,因此需要基于printjobflexprintjob来完善flex的打印功能,及自定义打印控件。

本控件核心为:

1PrintManager,打印控制核心类

2IPrintableDocument,打印文档设置,打印文档有3部分组成,分别是:PrintableHeaderPrintableBodyPrintableFooter

因此,开发人员只要设计好printabledocument,重点排版好headerbodyfooter,然后调用PrintManager的打印即可打印printabledocument了。

<print:PrintableDocument

printableBody="{p_body}" printableFooter="{p_footer}" printableHeader="{p_header}">

<print:PrintableHeader width="100%" id="p_header" x="0" y="0" height="81" >

<mx:Label id="pageHeader" text="Sample Accounts" styleName="printTitle" width="100%" textAlign="center"/>

<mx:Label id="cityHeader" text="City Of Somewheretown" width="100%" textAlign="center" />

<mx:Label id="entityHeader" text="General Accounts" width="100%" textAlign="center"/>

</print:PrintableHeader>

<print:PrintableBody id="p_body" width="100%" bottom = "28" x="0" top="81">

<mx:PrintAdvancedDataGrid id="accountsDataGrid" designViewDataType="flat"

dataProvider="{accounts}" horizontalGridLines="false" verticalGridLines="false"

width="100%" height="100%" borderStyle="none" sortExpertMode="true" headerHeight="0"

rowHeight="30">

<mx:columns>

<mx:AdvancedDataGridColumn headerText="Account Number" dataField="account" textAlign="center" width="100"/>

<mx:AdvancedDataGridColumn headerText="Control Account" dataField="control" textAlign="center" width="100"/>

<mx:AdvancedDataGridColumn headerText="Description" dataField="description" width="300"/>

</mx:columns>

</mx:PrintAdvancedDataGrid>

</print:PrintableBody>

<print:PrintableFooter id="p_footer" width="100%" height="28" bottom="0" left="0" printType="perpage">

<mx:Label text="Prepared by: Authorname" width="33%" textAlign="center"/>

<mx:Label text="Page {pageNumber} of {pageCount}" width="34%" textAlign="right"/>

<mx:Label id="pageFooter" text="Sample Accounts" width="33%"/>

</print:PrintableFooter>

</print:PrintableDocument>

附上控件核心类的相关代码。

1PrintManager

public class PrintManager

{

public function PrintManager()

{

}

public function print(document:IPrintableDocument, parent:UIComponent = null):Boolean

{

var printJob:PrintJob = new PrintJob();

if (parent != null)

parent.addChild(document.displayObject());

var printed:Boolean = printInternal(document, printJob);

if (parent != null)

parent.removeChild(document.displayObject());

return printed;

}

private function unscaleDocument(document:IPrintableDocument):void

{

document.scaleX = 1.0;

document.scaleY = 1.0;

document.validateNow();

}

/**

* adjust print orientation

* scale the printdocument

*/

private function rotateDocumentForPaperOrientation(document:IPrintableDocument, printJob:PrintJob):void

{

document.x = 0;

document.y = 0;

var scaleFactor:Number = 1.0;

//

document.scaleX = scaleFactor;

document.scaleY = scaleFactor;

document.validateNow();

}

private function printPage(document:IPrintableDocument, printJob:PrintJob):void

{

var width:Number = document.desiredWidth;

var height:Number = document.desiredHeight;

var page:Sprite = Sprite(document);

var printableArea:Rectangle = new Rectangle(0, 0, width, height);

printJob.addPage(page, printableArea);

}

public function printInternal(document:IPrintableDocument, printJob:PrintJob):Boolean

{

//start printer

if (!printJob.start()) {

return false;

}

document.moveToFirstPage();

unscaleDocument(document);

//adjust paper orientation

rotateDocumentForPaperOrientation(document, printJob);

//add first print page

printPage(document, printJob);

while(document.validNextPage)

{

document.nextPage();

printPage(document, printJob);

}

printJob.send();

Courier Ne

分享到:
评论
isiqi
  • 浏览: 4186456 次
  • 性别: Icon_minigender_1
  • 来自: 济南
社区版块
存档分类
最新评论

你可能感兴趣的:(Web,Flex,Flash)