Flex 分页预览,分页打印

本文介绍如何解决Flex打印时遇到的问题,如表格单元格合并支持不足。通过自定义PrintDataGrid实现类似HTML表格的Grid,支持单元格合并,并详细阐述了分页预览和打印的算法,涉及内容包括根据页面高度拆分Grid,处理RowSpan大于1的单元格合并情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Flex 打印常使用PrintDataGrid,但会有一些问题,不支持表格单元格合并,有的可能需要打印一些flex组件和容器(VBox, HBox, Text, Image)等。重写flex的Grid表格,Grid类似HTML的table, 能很好支持单元格合并,单元格中加入组件和容器。

自定义PrintDataGrid:

public class PrintDataGrid extends Grid
    {
       public static const pageHeight:int = 1023;
 
       private var _realWidth:int;
 
       public function PrintDataGrid()
       {
           this.horizontalScrollPolicy = ScrollPolicy.OFF;
           this.verticalScrollPolicy = ScrollPolicy.OFF;
 
           super();
       }
 
       public function addGridRow(gridRow:RowGrid):void {
           addCells(gridRow.getAvailableCell(), gridRow.height);
       }
 
       private function addCells(cells:Array, height:Number):void {
           var gridRow:GridRow = new GridRow();
           for each(var cell:GridCell in cells) {
              gridRow.addChild(cell);
           }
           gridRow.height = height;
           this.addChild(gridRow);
       }
 
       public function get realWidth():int
       {
           return _realWidth;
       }
 
       public function set realWidth(value:int):void
       {
           _realWidth = value;
       }
    }

Adobe对于多页打印或打印预览都没有提供一个很好的解决办法,但可以根据Grid中GridRow的height,计算自动拆分表格,将一个大的Grid拆分每一个页面一个Grid,进行分页预览和打印。大概的算法:根据每页的height值计算一个页面能存放多少Grid的GridRow,超过的拆分到下一个页面,如果需要拆分的单元格存在被合并的情况,也就是GridItem 中RowSpan > 1, 则需要将一个GridItem,分为两个GridItem,而这两个的GridItem的rowSpan之和为原来的GridItem的RowSpan。

合并单元格拆分:

public function splitRowSpan(gridCellArray:GridCellArray):void {
           if(cells.length == 0) {
               return;
           }
           for(var i:int = 0; i < cells.length; i++) {
              var gridCell:GridCell = cells[i] as GridCell;
              if(gridCell == null) {
                  continue;
              }
              var splitCell:GridCell = null;
              if (gridCell.isRemove) {
                  splitCell = gridCell.mergerCell;
              }
              if (splitCell == null) {
                  continue;
              }
              if (gridCell.rNum == splitCell.rNum) {
                  continue;
              }
              var orgRowSpan:int = splitCell.rowSpan;
              var extRowSpan:int = gridCell.rNum - splitCell.rNum;
             
              var content:DisplayObject = splitCell.getChildAt(0);
             
              var topCellHeight:Number = getExtRowSpanHeight(splitCell.rNum, splitCell.rNum + extRowSpan, gridCellArray);
              var bottomCellHeight:Number = getExtRowSpanHeight(splitCell.rNum + extRowSpan, splitCell.rNum + splitCell.rowSpan, gridCellArray);
             
              splitCell.rowSpan = extRowSpan;
              splitCell.height = topCellHeight;
              if(topCellHeight < bottomCellHeight) {
                  splitCell.removeAllChildren();
              }
             
              var  bottomCell:GridCell = splitCell.newGridCell();
              bottomCell.rowSpan = orgRowSpan - extRowSpan;
              bottomCell.colSpan = splitCell.colSpan;
              bottomCell.height = bottomCellHeight;
              bottomCell.rNum = gridCell.rNum;
              bottomCell.cNum = gridCell.cNum;
              if(topCellHeight < bottomCellHeight) {
                  bottomCell.addChild(content);
              }
             
              var gridRow:RowGrid = gridCellArray.getRowGrid(gridCell.rNum);
              gridRow.setCell(bottomCell, bottomCell.cNum);
              if (bottomCell.rowSpan > 1 || bottomCell.colSpan > 1) {
                  gridCellArray.mergerGridCell(bottomCell);
              }
           }
       }

原文出处:http://www.anyrt.com/blog/list/201609131739.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值