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

转载地址: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.                         {
  24.                                 var printJob:FlexPrintJob=new FlexPrintJob();
  25.                                 if (printJob.start())
  26.                                 {
  27.                                         var myPrintData:PrintDataGrid=new PrintDataGrid();
  28.                                         Application.application.addChild(myPrintData);
  29.                                         myPrintData.dataProvider=myData.dataProvider;
  30.                                         myPrintData.width=printJob.pageWidth;
  31.                                         myPrintData.height=printJob.pageHeight;

  32.                                         printJob.addObject(myPrintData);
  33.                                         while(myPrintData.validNextPage)
  34.                                         {
  35.                                                 myPrintData.nextPage();
  36.                                                 printJob.addObject(myPrintData);
  37.                                         }
  38.                                         Application.application.removeChild(myPrintData);

  39.                                         printJob.send();
  40.                                 }
  41.                         }
  42.                 ]]>
  43.         </mx:Script>
  44.         <mx:Panel title="Flex Tutorial - PrintDataGrid" width="500" height="500" horizontalCenter="0" verticalCenter="0" horizontalAlign="center" verticalAlign="middle">
  45.                 <mx:DataGrid id="myData" dataProvider="{dataSource}" width="400" height="400"/>
  46.                 <mx:Button label="Print" click="doPrint()"/>
  47.         </mx:Panel>
  48. </mx:Application>
复制代码
结论:
Adobe  Flex provides PrintDataGrid 是为了解决多个页面的打印问题的.如果你的 应用 程序 差不多只包含一个大的DataGrid的话,它工作良好。 
但是,生活往往难如人愿,很多时候,你的应用程序是很多容器和组件(Canvas, VBox, HBox, Label, Text, Text Area, Image,DataGrid)的结合体。 不幸的是,即使到flex3,Adobe也没有拿出一个比FlexPrintJob 和PrintDataGrid更好的打印方法。  

那么,有没有任何第三方的解决方案?我已经做了很多研究,最终找到了一个开源项目。这就有了了我们的下一个教程 - 使用FlexReport打印多页。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Vue 项目中使用 DevExtreme 打印查看,您可以遵循以下步骤: 1. 安装 devextreme 和 devextreme-vue 包,您可以使用以下命令进行安装: ``` npm install --save devextreme devextreme-vue ``` 2. 导入所需的 DevExtreme 组件和 Vue 组件,例如: ``` import { DxDataGrid, DxPopup, DxButton } from 'devextreme-vue'; ``` 3. 在 Vue 组件中使用 DxDataGrid 组件和 DxPopup 组件来显示数据和打印预览: ``` <template> <div> <DxDataGrid :dataSource="dataSource"> <!-- 列定义 --> </DxDataGrid> <DxPopup :visible.sync="showPrintPreview"> <DxButton text="Print" @click="printDataGrid" /> <div id="printableDataGrid" ref="printableDataGrid"> <DxDataGrid :dataSource="dataSource"> <!-- 列定义 --> </DxDataGrid> </div> </DxPopup> </div> </template> <script> import { DxDataGrid, DxPopup, DxButton } from 'devextreme-vue'; export default { components: { DxDataGrid, DxPopup, DxButton }, data() { return { dataSource: [], // 数据源 showPrintPreview: false // 打印预览弹窗是否显示 }; }, methods: { printDataGrid() { // 打印预览 const printableContent = this.$refs.printableDataGrid.$el.innerHTML; const popupWin = window.open('', '_blank', 'width=1024,height=768'); popupWin.document.open(); popupWin.document.write(` <html> <head> <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/20.1.7/css/dx.common.css" /> <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/20.1.7/css/dx.light.css" /> </head> <body onload="window.print();window.close()">${printableContent}</body> </html> `); popupWin.document.close(); } } }; </script> ``` 在上面的代码中,我们使用 DxDataGrid 组件来显示数据,使用 DxPopup 组件和 DxButton 组件来实现打印预览。在打印预览中,我们将 DxDataGrid 组件包装在一个 div 元素中,并使用 ref 属性引用该元素。在打印预览按钮的 click 事件中,我们获取该 div 元素的 innerHTML,并在新窗口中打开该内容,从而实现打印预览。 注意,我们还需要在页面头部引入 DevExtreme 样式文件,例如: ``` <head> <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/20.1.7/css/dx.common.css" /> <link rel="stylesheet" href="https://cdn3.devexpress.com/jslib/20.1.7/css/dx.light.css" /> </head> ``` 这样就可以在 Vue 项目中使用 DevExtreme 打印查看了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值