加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flex打印系列教程二使用PrintDataGrid打印多页内容

发布时间:2020-12-15 01:13:03 所属栏目:百科 来源:网络整理
导读:转载地址:http://bbs.9ria.com/viewthread.php?tid=69835 在上一篇 教程 中,我们知道了如何使用 flex 自带的打印类-FlexPrintJob。它很容易使用。但是你在打印多页内容时可能无法达到预期的 效果 ,原因如下: 为了开始打印,你需要写如下 代码 :? printJ

转载地址:http://bbs.9ria.com/viewthread.php?tid=69835

在上一篇教程中,我们知道了如何使用flex自带的打印类-FlexPrintJob。它很容易使用。但是你在打印多页内容时可能无法达到预期的效果,原因如下:
为了开始打印,你需要写如下代码:?
printJob.addObject(targetComponent);
在这句里,你将你的组件加到FlexPrintJob里面,但是你到底要打印多少页呢?
最明显的例子是?DataGrid.它可能有1到1000条记录。没有什么办法能让我们预先确定一个动态绑定数据源的大小.,再加上我们不知道在哪里将打印的页面分隔,就行1-17条记录在第一页,18-30条记录在第二页等等 。
幸运的是,flex为你提供了一个解决DataGrid打印问题的方法。方法如下:?
你需要的flex类
?mx.printing.FlexPrintJob
?mx.printing.PrintDataGrid
操作步骤:
1. 创建一个FlexPrintJob实例??
? ?var flexPrintJob: FlexPrintJob = new FlexPrintJob();
2.启动FlexPrintJob
? ?flexPrintJob.start();
3. 创建一个PrintDataGrid

  1. ? ?var myPrintData:PrintDataGrid = new PrintDataGrid();
  2. ? ?Application.application.addChild(myPrintData);
复制代码
4. 依据DataGrid设置PrintDataGrid的 DataProvider,Width,and Height
? ?myPrintData.dataProvider = myData.dataProvider;
? ?myPrintData.width = printJob.pageWidth;
? ?myPrintData.height = printJob.pageHeight;
5. 循环PrintDataGrid来生成打印的多个页面
? ?printJob.addObject(myPrintData);
? ?while (myPrintData.validNextPage) {
? ?? ? ? ?? ?myPrintData.nextPage();
? ? ? ? ? ?? ?printJob.addObject(myPrintData);
? ?}
6. 打印
? ?printJob.send();

例子代码:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">
  3. ? ? ? ? <mx:Script>
  4. ? ? ? ? ? ? ? ? <![CDATA[
  5. ? ? ? ? ? ? ? ? ? ? ? ? import mx.printing.PrintDataGrid;
  6. ? ? ? ? ? ? ? ? ? ? ? ? import mx.printing.FlexPrintJob;
  7. ? ? ? ? ? ? ? ? ? ? ? ? import mx.collections.ArrayCollection;

  8. ? ? ? ? ? ? ? ? ? ? ? ? [Bindable]
  9. ? ? ? ? ? ? ? ? ? ? ? ? public var dataSource:ArrayCollection=new ArrayCollection();
  10. ? ? ? ? ? ? ? ? ? ? ? ? private var totalRecords:Number=100;
  11. ? ? ? ? ? ? ? ? ? ? ? ? private function init():void
  12. ? ? ? ? ? ? ? ? ? ? ? ? {
  13. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? for(var i:int=1; i <= totalRecords; i++)
  14. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
  15. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var dataObject:Object=new Object();
  16. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dataObject.Name="Name #" + i;
  17. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dataObject.Phone="Phone #" + i;
  18. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dataObject.Address="Address #" + i;
  19. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? dataSource.addItem(dataObject);
  20. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
  21. ? ? ? ? ? ? ? ? ? ? ? ? }
  22. ? ? ? ? ? ? ? ? ? ? ? ? private function doPrint():void
  23. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var printJob:FlexPrintJob=new FlexPrintJob();
  24. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (printJob.start())
  25. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? var myPrintData:PrintDataGrid=new PrintDataGrid();
  26. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Application.application.addChild(myPrintData);
  27. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myPrintData.dataProvider=myData.dataProvider;
  28. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myPrintData.width=printJob.pageWidth;
  29. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myPrintData.height=printJob.pageHeight;
  30. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printJob.addObject(myPrintData);
  31. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? while(myPrintData.validNextPage)
  32. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? {
  33. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myPrintData.nextPage();
  34. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printJob.addObject(myPrintData);
  35. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
  36. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Application.application.removeChild(myPrintData);
  37. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printJob.send();
  38. ? ? ? ? ? ? ? ? ]]>
  39. ? ? ? ? </mx:Script>
  40. ? ? ? ? <mx:Panel title="Flex Tutorial - PrintDataGrid" width="500" height="500" horizontalCenter="0" verticalCenter="0" horizontalAlign="center" verticalAlign="middle">
  41. ? ? ? ? ? ? ? ? <mx:DataGrid id="myData" dataProvider="{dataSource}" width="400" height="400"/>
  42. ? ? ? ? ? ? ? ? <mx:Button label="Print" click="doPrint()"/>
  43. ? ? ? ? </mx:Panel>
  44. </mx:Application>
结论:
Adobe ?Flex provides PrintDataGrid 是为了解决多个页面的打印问题的.如果你的 应用 程序 差不多只包含一个大的DataGrid的话,它工作良好。?
但是,生活往往难如人愿,很多时候,你的应用程序是很多容器和组件(Canvas,VBox,HBox,Label,Text,Text Area,Image,DataGrid)的结合体。 不幸的是,即使到flex3,Adobe也没有拿出一个比FlexPrintJob 和PrintDataGrid更好的打印方法。??
那么,有没有任何第三方的解决方案?我已经做了很多研究,最终找到了一个开源项目。这就有了了我们的下一个教程 - 使用FlexReport打印多页。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读