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);              }           }       }

原文出处: