`
ynp
  • 浏览: 435312 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Printing Multiple Pages Using PrintDataGrid(Flex打印系列 转载)

阅读更多
In the previous tutorial, we knew How to Use Flex Build-in Print Class – FlexPrintJob. It’s simple to use. But you may face some challenges when print multiple pages. Here is why:

To start to print on a new page, you need to write a code like this:

printJob.addObject(targetComponent);

It  adds your component to FlexPrintJob. But how many pages is it going to take?


The typical example is DataGrid. It might contains 1 or 1000 records. There is no way we can predetermine the size when binding with a dynamic data provider. Plus how do you know where to put the page break, like record 1-17 on page 1,  18-30 on page 2 …?

Fortunately, Flex gives you a workaround to solve DataGrid printing issue. Here is how:

Things You Need

mx.printing.FlexPrintJob
mx.printing.PrintDataGrid
Steps You Do

1. Create a FlexPrintJob Instance    
   var flexPrintJob: FlexPrintJob = new FlexPrintJob();

2. Start FlexPrintJob
   flexPrintJob.start();

3. Create PrintDataGrid
   var myPrintData:PrintDataGrid = new PrintDataGrid();
   Application.application.addChild(myPrintData);

4. Set PrintDataGrid's DataProvider(from DataGrid), Width, and Height
   myPrintData.dataProvider = myData.dataProvider;
   myPrintData.width = printJob.pageWidth;
   myPrintData.height = printJob.pageHeight;

5. Loop PrintDataGrid to Generate Multiple Print Pages
   printJob.addObject(myPrintData);
   while (myPrintData.validNextPage) {
     myPrintData.nextPage();
      printJob.addObject(myPrintData);
   }

6. Print
   printJob.send();
Sample Code


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    layout="absolute"
    initialize="init()">

    <mx:Script>
    <![CDATA[
    import mx.printing.PrintDataGrid;
    import mx.printing.FlexPrintJob;
    import mx.collections.ArrayCollection;

    [Bindable]
    public var dataSource:ArrayCollection = new ArrayCollection();

        private var totalRecords:Number = 100;

    private function init():void {
for (var i:int = 1; i<=totalRecords; i++) {
      var dataObject:Object = new Object();
      dataObject.Name = "Name #" + i;
      dataObject.Phone = "Phone #" + i;
      dataObject.Address = "Address #" + i;
      dataSource.addItem(dataObject);
    }
    }

    private function doPrint():void {
    var printJob:FlexPrintJob = new FlexPrintJob();
       if (printJob.start()) {
         var myPrintData:PrintDataGrid = new PrintDataGrid();
          Application.application.addChild(myPrintData);
           myPrintData.dataProvider = myData.dataProvider;
         myPrintData.width = printJob.pageWidth;
         myPrintData.height = printJob.pageHeight;

         printJob.addObject(myPrintData);
         while (myPrintData.validNextPage) {
       myPrintData.nextPage();
       printJob.addObject(myPrintData);
         }
         Application.application.removeChild(myPrintData);

                  printJob.send();
            }
    }

    ]]>
    </mx:Script>

    <mx:Panel title="Flex Tutorial - PrintDataGrid"
    width="500" height="500"
    horizontalCenter="0" verticalCenter="0"
    horizontalAlign="center" verticalAlign="middle">

        <mx:DataGrid id="myData"
            dataProvider="{dataSource}"
            width="400" height="400" />

        <mx:Button label="Print" click="doPrint()"/>

    </mx:Panel>

</mx:Application>


Conclusion

Adobe Flex provides PrintDataGrid to solve the problem of multiple pages printing. It works fine when your application mainly contains one big DataGrid.

However, life is not that simple. In many cases, you need to have a combination of different contains and controls like Canvas, VBox, HBox, Label, Text, Text Area, Image, plus DataGrid. Unfortunately, until Flex 3,  Adobe has not provided any better solution beyond FlexPrintJob and PrintDataGrid.

So, is there any 3rd party solution? I have done a lot of research. Eventually it narrows down to an open source project. This leads to our next tutorial – Printing Multiple Pages using FlexReport.





source:http://flextutorial.org/2009/05/28/printing-multiple-pages-using-printdatagrid/

分享到:
评论

相关推荐

    flex打印操作(FlexPrintJob)还有分页打印操作具体实现

    尽管PrintDataGrid不是Flex3的内置组件,但在使用PrintAdvancedDataGrid时,要注意其对分页的处理,以便实现打印时的分页效果。 对于简单的打印操作,如果没有分页和下拉框的需求,可以直接将要打印的组件或容器...

    FlexReport打印

    报表布局:如果要在Flex中打印页面,你应该创建一个容器,并把它加入到FlexPrintJob中。它处理静态内容时没有什么大问题。但是如果创建动态报表,即你不知道在处理过程中会产生多少页的话,事情很快会变成噩梦。 多...

    flex 拖拽功能 中文文档

    Flex中有一系列内置拖放支持的控件,包括: - `DataGrid` - `HorizontalList` - `List` - `PrintDataGrid` - `TileList` - `Tree` 这些控件允许将可拖动的控件条目移动到可接收拖放的控件中。然而,如果要实现条目...

    播放器没有用的代码11

    1. **Flex打印API**:Flex提供了`flash.printing.PrintJob`类来实现打印功能。开发者可以通过创建PrintJob对象,添加要打印的内容,然后调用start()方法来启动打印任务。 2. **DataGrid的复制和转换**:由于直接...

    printDataGrid.js

    easyui快速打印datagrid内容,通过创建容纳所需要的打印内容(需要打印的表格通过方法组装成字符串放入DIV中),然后调用window.print打印;具体步骤如下:1.html页面需要有专门的DIV用于输出Dialog 2.在html页面...

    vb.net如何打印dataGridview中的数据

    在VB.NET环境中,打印DataGridView控件中的数据是一个常见的需求,特别是在报表或数据分析的应用场景中。以下将详细讲解如何实现这个功能。 首先,我们要理解DataGridView是.NET Framework提供的一种用于显示表格...

    Flex3组件拖放教程

    ### Flex3组件拖放教程知识点总结 #### 一、关于组件拖放 拖放功能是Flex3中一种非常实用的功能,它可以实现用户界面元素的直观交互。通过拖放操作,用户能够选择一个对象(例如`List`控件或Flex中的`Image`控件)...

    c#可以打印datagridview的类

    可以打印datagridview的类 调用方法: PrintDataGridView printDataGrid = new PrintDataGridView(); printDataGrid.PrintDatagridView(dataGridView1, true);

    DataGridView打印控件可以自定义列,标题名,打印预览

    最后,关于提供的文件"PrintDataGrid.sln"和"PrintDataGrid",它们很可能是一个示例解决方案和项目文件,包含了实现上述功能的代码。通过打开和分析这些文件,你可以直接看到如何在实际项目中应用这些概念和技术。 ...

    Flex UI组件使用全集

    - **FlexPrintJob、PrintDataGrid**: 打印相关的组件,支持打印DataGrid等内容。 #### Validatorsandformatters(验证器与格式化器) - **Validators**: 用于验证用户输入的数据是否符合规则。 - **CreditCard...

    DataGridView打印控件5.3版

    12、删除了PrintDataGrid函数(若要打印DataGrid,请先调用ConvertDataGridToDGV函数将DataGrid转换为DataGridView再以打印DGV的形式打印)和ChartGraph图表组件(图表打印请使用功能更强大且不依赖Excel的Chartlet...

    Flex 3 组件实例与应用(2009版)

    - **FlexPrintJob/PrintDataGrid** - 打印相关的组件。 - **PrintDataGridExample** - 打印数据网格的例子。 - **FormPrintView/FormPrintHeader/FormPrintFooter** - 表单打印视图及相关组件。 #### 五、验证器与...

    C# DataGridView打印

    C#中的打印功能通常依赖于.NET Framework提供的`System.Drawing.Printing`命名空间。这个命名空间包含了`PrintDocument`类,它是进行打印操作的基础。以下是一些关键步骤: 1. **创建PrintDocument对象**:首先,...

    jeasyui的dataGrid的打印和导出,jeasyui报表 table转成excel 实例下载

    --//打印--&gt; function CreateFormPage(strPrintName, printDatagrid) { var tableString = '&lt;div&gt;&lt;table width="100%"&gt;;"&gt;; font-weight:bold;"&gt;;"&gt; 年;"&gt; 半年广东省房屋市政工程安全生产文明施工示范工地申报...

    实现easyui的datagrid导出为excel的示例代码

    之前有介绍过如何实现easyui里datagrid内容的打印,今天给大家介绍下如何实现datagrid内容导出为excel文件。以下为代码实现: export.js function ChangeToTable(printDatagrid) { var tableString = '...

    智能优化算法及其应用 PDF

    同时,"PrintDataGrid.exe"可能是一个用于数据展示和打印的程序,配合书中的数据和结果,提供了直观的可视化工具。而"Northwind.mdb"则可能是一个包含示例数据的Access数据库文件,读者可以使用这些数据进行练习和...

    easyui的datagrid数据excel导出

    1. **获取 DataGrid 的列配置**:通过 `printDatagrid.datagrid("options").frozenColumns` 和 `printDatagrid.datagrid("options").columns` 获取固定列和所有列的配置信息。 2. **构建表头**:根据列配置构建 `...

Global site tag (gtag.js) - Google Analytics