import { IGrid , PdfExportProperties , PdfHeader , PdfFooter , PdfHeaderFooterContent , Theme , PdfQueryCellInfoEventArgs } from '../base/interface' ; import { Column } from './../models/column' ; import { Row } from './../models/row' ; import * as events from '../base/constant' ; import { PdfDocument , PdfPage , PdfGrid , PdfBorders , PdfPen , PdfFont } from '@syncfusion/ej2-pdf-export' ; import { PdfGridRow , PdfStandardFont , PdfFontFamily , PdfFontStyle , PdfBitmap } from '@syncfusion/ej2-pdf-export' ; import { PdfStringFormat , PdfTextAlignment , PdfColor , PdfSolidBrush } from '@syncfusion/ej2-pdf-export' ; import { PdfVerticalAlignment , PdfGridCell , RectangleF , PdfPageTemplateElement } from '@syncfusion/ej2-pdf-export' ; import { PointF , PdfPageNumberField , PdfCompositeField , PdfSection } from '@syncfusion/ej2-pdf-export' ; import { PdfPageCountField , SizeF , PdfPageSettings , PdfPageOrientation } from '@syncfusion/ej2-pdf-export' ; import { ExportHelper , ExportValueFormatter } from './export-helper' ; import { Data } from '../actions/data' ; import { ReturnType } from '../base/type' ; import { SummaryModelGenerator , GroupSummaryModelGenerator , CaptionSummaryModelGenerator } from '../services/summary-model-generator' ; import { AggregateColumnModel } from '../models/aggregate-model' ; import { compile , getEnumValue , isNullOrUndefined } from '@syncfusion/ej2-base' ; import { CellType , PdfPageSize , PdfDashStyle , PdfPageNumberType } from '../base/enum' ; import { DataManager , Query } from '@syncfusion/ej2-data' ; import { getValue } from '@syncfusion/ej2-base' ; import { Grid } from '../base/grid' ; hidden export class PdfExport { private parent : IGrid ; private isExporting : boolean ; private data : Data ; private pdfDocument : PdfDocument ; private hideColumnInclude : boolean = false ; private currentViewData : boolean = false ; private customDataSource : boolean = false ; private exportValueFormatter : ExportValueFormatter ; private gridTheme : Theme ; private isGrouping : boolean = false ; private helper : ExportHelper ; private isBlob : boolean ; private blobPromise : Promise < { blobData : Blob ; } > ; hidden constructor ( parent ? ) { this . parent = parent ; this . helper = new ExportHelper ( parent ) ; } /**
     * For internal use only - Get the module name.
     */ private getModuleName ( ) { return 'PdfExport' ; } private init ( parent ) { this . exportValueFormatter = new ExportValueFormatter ( parent . locale ) ; this . pdfDocument = undefined ; this . hideColumnInclude = false ; this . currentViewData = false ; this . parent = parent ; let gObj = parent ; let can = 'cancel' ; this . isGrouping = false ; this . isExporting = true ; let args = { requestType : 'beforePdfExport' , gridObject : gObj , cancel : false } ; gObj . trigger ( events . beforePdfExport , args ) ; if ( args [ can ] === true ) { return ; } } return { void } public Map ( parent ? , pdfExportProperties ? , isMultipleExport ? , pdfDoc ? , isBlob ? ) { this . data = new Data ( this . parent ) ; this . isBlob = isBlob ; if ( ! isNullOrUndefined ( pdfExportProperties ) && ! isNullOrUndefined ( pdfExportProperties . dataSource ) && pdfExportProperties . dataSource instanceof DataManager ) { return new Promise ( ( resolve , reject ) => { new DataManager ( { url : ( pdfExportProperties . dataSource as DataManager ) . dataSource . url , adaptor : ( pdfExportProperties . dataSource as DataManager ) . adaptor } ) . executeQuery ( new Query ( ) ) . then ( ( returnType ) => { this . init ( parent ) ; if ( ! isNullOrUndefined ( pdfDoc ) ) { this . pdfDocument = < PdfDocument > pdfDoc ; } else { this . pdfDocument = new PdfDocument ( ) ; } this . processExport ( parent , returnType , pdfExportProperties , isMultipleExport ) ; this . isExporting = false ; parent . trigger ( events . pdfExportComplete , this . isBlob ? { promise : this . blobPromise } : { } ) ; resolve ( this . pdfDocument ) ; } ) ; } ) ; } else { let allPromise = [ ] ; allPromise . push ( this . data . getData ( { } , ExportHelper . getQuery ( parent , this . data ) ) ) ; allPromise . push ( this . helper . getColumnData ( < Grid > parent ) ) ; return Promise . all ( allPromise ) . then ( ( e ) => { this . init ( parent ) ; if ( ! isNullOrUndefined ( pdfDoc ) ) { this . pdfDocument = < PdfDocument > pdfDoc ; } else { this . pdfDocument = new PdfDocument ( ) ; } this . processExport ( parent , e [ 0 ] , pdfExportProperties , isMultipleExport ) ; this . isExporting = false ; parent . trigger ( events . pdfExportComplete , this . isBlob ? { promise : this . blobPromise } : { } ) ; return this . pdfDocument ; } ) as any ; } } private processExport ( gObj , returnType , pdfExportProperties , isMultipleExport ) { if ( ! isNullOrUndefined ( pdfExportProperties ) ) { this . gridTheme = pdfExportProperties . theme ; } let columns = gObj . columns ; let dataSource = returnType . result ; let section = this . pdfDocument . sections . add ( ) as PdfSection ; let result = this . processExportProperties ( pdfExportProperties , dataSource , section ) ; dataSource = result . dataSource ; if ( ! isNullOrUndefined ( ( dataSource as { GroupGuid ? : string ; level ? : number ; childLevels ? : number ; records ? : any ; } ) . GroupGuid ) ) { this . isGrouping = true ; } section = result . section ; let pdfPage = section . pages . add ( ) ; let pdfGrid = new PdfGrid ( ) ; let headerThemeStyle = this . getHeaderThemeStyle ( ) ; let border = headerThemeStyle . border ; let headerFont = headerThemeStyle . font ; let headerBrush = headerThemeStyle . brush ; let returnValue = this . helper . getHeaders ( columns , this . hideColumnInclude ) ; let rows = returnValue . rows ; let gridColumns = returnValue . columns ; pdfGrid = this . processGridHeaders ( dataSource . childLevels , pdfGrid , rows , gridColumns , border , headerFont , headerBrush ) ; this . setColumnProperties ( gridColumns , pdfGrid ) ; let captionThemeStyle = this . getSummaryCaptionThemeStyle ( ) ; if ( ! isNullOrUndefined ( dataSource ) && dataSource . length > 0 ) { if ( this . isGrouping ) { this . processGroupedRecords ( pdfGrid , dataSource , gridColumns , gObj , border , 0 , captionThemeStyle . font , captionThemeStyle . brush , captionThemeStyle . backgroundBrush , returnType ) ; } else { this . processRecord ( border , gridColumns , gObj , dataSource , pdfGrid ) ; } if ( ! isNullOrUndefined ( returnType . aggregates ) ) { let summaryModel = new SummaryModelGenerator ( gObj ) ; let sRows ; if ( this . customDataSource ) { sRows = summaryModel . generateRows ( dataSource , < SummaryData > returnType . aggregates ) ; } else if ( this . currentViewData ) { sRows = summaryModel . generateRows ( this . parent . getCurrentViewRecords ( ) , < SummaryData > returnType . aggregates ) ; } else if ( this . isGrouping ) { sRows = summaryModel . generateRows ( dataSource . records , < SummaryData > returnType . aggregates ) ; } else { sRows = summaryModel . generateRows ( returnType . result , < SummaryData > returnType . aggregates ) ; } this . processAggregates ( sRows , pdfGrid , border , captionThemeStyle . font , captionThemeStyle . brush , captionThemeStyle . backgroundBrush , false ) ; } } else { let row = pdfGrid . rows . addRow ( ) ; row . style . setBorder ( border ) ; } pdfGrid . draw ( pdfPage , 20 , 20 ) ; if ( ! isMultipleExport ) { if ( ! this . isBlob ) { if ( ! isNullOrUndefined ( pdfExportProperties ) && pdfExportProperties . fileName ) { this . pdfDocument . save ( pdfExportProperties . fileName ) ; } else { this . pdfDocument . save ( 'Export.pdf' ) ; } } else { this . blobPromise = this . pdfDocument . save ( ) ; } this . pdfDocument . destroy ( ) ; } } private getSummaryCaptionThemeStyle ( ) { if ( ! isNullOrUndefined ( this . gridTheme ) && ! isNullOrUndefined ( this . gridTheme . caption ) && this . gridTheme . caption !== null ) { let fontSize = ! isNullOrUndefined ( this . gridTheme . caption . fontSize ) ? this . gridTheme . caption . fontSize : 9.75 ; let pdfColor = new PdfColor ( ) ; if ( ! isNullOrUndefined ( this . gridTheme . caption . fontColor ) ) { let penBrushColor = this . hexToRgb ( this . gridTheme . caption . fontColor ) ; pdfColor = new PdfColor ( penBrushColor . r , penBrushColor . g , penBrushColor . b ) ; } return { font : new PdfStandardFont ( PdfFontFamily . Helvetica , 10.5 ) , brush : new PdfSolidBrush ( new PdfColor ( pdfColor ) ) , backgroundBrush : new PdfSolidBrush ( new PdfColor ( 246 , 246 , 246 ) ) } ; } else { return { font : new PdfStandardFont ( PdfFontFamily . Helvetica , 9.75 ) , brush : new PdfSolidBrush ( new PdfColor ( 0 , 0 , 0 ) ) , backgroundBrush : new PdfSolidBrush ( new PdfColor ( 246 , 246 , 246 ) ) } ; } } private getHeaderThemeStyle ( ) { let border = new PdfBorders ( ) ; if ( ! isNullOrUndefined ( this . gridTheme ) && ! isNullOrUndefined ( this . gridTheme . header ) ) { if ( ! isNullOrUndefined ( this . gridTheme . header . borders ) && ! isNullOrUndefined ( this . gridTheme . header . borders . color ) ) { let borderColor = this . hexToRgb ( this . gridTheme . header . borders . color ) ; border . all = new PdfPen ( new PdfColor ( borderColor . r , borderColor . g , borderColor . b ) ) ; } let fontSize = ! isNullOrUndefined ( this . gridTheme . header . fontSize ) ? this . gridTheme . header . fontSize : 10.5 ; let pdfColor = new PdfColor ( ) ; if ( ! isNullOrUndefined ( this . gridTheme . header . fontColor ) ) { let penBrushColor = this . hexToRgb ( this . gridTheme . header . fontColor ) ; pdfColor = new PdfColor ( penBrushColor . r , penBrushColor . g , penBrushColor . b ) ; } return { border : border , font : new PdfStandardFont ( PdfFontFamily . Helvetica , fontSize ) , brush : new PdfSolidBrush ( pdfColor ) } ; } else { border . all = new PdfPen ( new PdfColor ( 234 , 234 , 234 ) ) ; return { border : border , font : new PdfStandardFont ( PdfFontFamily . Helvetica , 10.5 ) , brush : new PdfSolidBrush ( new PdfColor ( 102 , 102 , 102 ) ) } ; } } private processGroupedRecords ( pdfGrid , dataSource , gridColumns , gObj , border , level , font , brush , backgroundBrush , returnType ) { let groupIndex = level ; for ( let dataSourceItems of dataSource ) { let row = pdfGrid . rows . addRow ( ) ; let col = gObj . getColumnByField ( dataSourceItems . field ) ; let args = { value : dataSourceItems . key , column : col , style : undefined , isForeignKey : col . isForeignColumn ( ) , } ; let value = this . parent . getColumnByField ( dataSourceItems . field ) . headerText + ': ' + this . exportValueFormatter . formatCellValue ( args ) + ' - ' + dataSourceItems . count + ( dataSource . count > 1 ? ' items' : ' item' ) ; row . cells . getCell ( groupIndex ) . value = value ; row . cells . getCell ( groupIndex + 1 ) . style . stringFormat = new PdfStringFormat ( PdfTextAlignment . Left ) ; row . style . setBorder ( border ) ; row . style . setFont ( font ) ; row . style . setTextBrush ( brush ) ; row . style . setBackgroundBrush ( backgroundBrush ) ; let sRows ; let captionSummaryModel = new CaptionSummaryModelGenerator ( gObj ) ; if ( ! isNullOrUndefined ( dataSourceItems . items . records ) ) { sRows = captionSummaryModel . generateRows ( dataSourceItems . items . records , dataSourceItems ) ; } else { sRows = captionSummaryModel . generateRows ( dataSourceItems . items , dataSourceItems ) ; } if ( ! isNullOrUndefined ( sRows ) && sRows . length === 0 ) { row . cells . getCell ( groupIndex + 1 ) . columnSpan = pdfGrid . columns . count - ( groupIndex + 1 ) ; } if ( ! isNullOrUndefined ( dataSource . childLevels ) && dataSource . childLevels > 0 ) { this . processAggregates ( sRows , pdfGrid , border , font , brush , backgroundBrush , true , row , groupIndex ) ; this . processGroupedRecords ( pdfGrid , dataSourceItems . items , gridColumns , gObj , border , ( groupIndex + 1 ) , font , brush , backgroundBrush , returnType ) ; let groupSummaryModel = new GroupSummaryModelGenerator ( gObj ) ; sRows = groupSummaryModel . generateRows ( dataSourceItems . items . records , dataSourceItems ) ; this . processAggregates ( sRows , pdfGrid , border , font , brush , backgroundBrush , false ) ; } else { this . processAggregates ( sRows , pdfGrid , border , font , brush , backgroundBrush , true , row , groupIndex ) ; this . processRecord ( border , gridColumns , gObj , dataSourceItems . items , pdfGrid , ( groupIndex + 1 ) ) ; let groupSummaryModel = new GroupSummaryModelGenerator ( gObj ) ; sRows = groupSummaryModel . generateRows ( dataSourceItems . items , dataSourceItems ) ; this . processAggregates ( sRows , pdfGrid , border , font , brush , backgroundBrush , false ) ; } } } private processGridHeaders ( childLevels , pdfGrid , rows , gridColumns , border , headerFont , headerBrush ) { let columnCount = gridColumns . length ; let gObj = this . parent ; if ( this . isGrouping ) { columnCount += ( childLevels + 1 ) ; } pdfGrid . columns . add ( columnCount ) ; if ( this . isGrouping ) { for ( let i = 0 ; i < ( childLevels + 1 ) ; i ++ ) { pdfGrid . columns . getColumn ( i ) . width = 20 ; } } pdfGrid . headers . add ( rows . length ) ; for ( let i = 0 ; i < rows . length ; i ++ ) { let gridHeader = pdfGrid . headers . getHeader ( i ) ; gridHeader . style . setBorder ( border ) ; gridHeader . style . setFont ( headerFont ) ; gridHeader . style . setTextBrush ( headerBrush ) ; let cellIndex = this . isGrouping ? ( childLevels + 1 ) : 0 ; if ( rows [ i ] . cells . length === 0 ) { for ( let j = 0 ; j < gridHeader . cells . count ; j ++ ) { let cell = gridHeader . cells . getCell ( j ) ; cell . value = '' ; } } else { for ( let j = 0 ; j < cellIndex ; j ++ ) { let cell = gridHeader . cells . getCell ( j ) ; cell . value = '' ; } for ( let j = 0 ; j < rows [ i ] . cells . length ; j ++ ) { let cell = gridHeader . cells . getCell ( cellIndex ) ; let args = { cell : cell , gridCell : rows [ i ] . cells [ j ] , style : cell . style } ; gObj . trigger ( events . pdfHeaderQueryCellInfo , args ) ; if ( cell . value !== null ) { cell . value = rows [ i ] . cells [ j ] . column . headerText ; if ( ! isNullOrUndefined ( rows [ i ] . cells [ j ] . column . headerTextAlign ) ) { cell . style . stringFormat = this . getHorizontalAlignment ( rows [ i ] . cells [ j ] . column . headerTextAlign ) ; } if ( ! isNullOrUndefined ( rows [ i ] . cells [ j ] . rowSpan ) ) { cell . rowSpan = rows [ i ] . cells [ j ] . rowSpan ; cell . style . stringFormat = this . getVerticalAlignment ( 'Bottom' , cell . style . stringFormat , rows [ i ] . cells [ j ] . column . textAlign ) ; for ( let k = 1 ; k < rows [ i ] . cells [ j ] . rowSpan ; k ++ ) { pdfGrid . headers . getHeader ( i + k ) . cells . getCell ( cellIndex ) . value = null ; } } if ( ! isNullOrUndefined ( rows [ i ] . cells [ j ] . colSpan ) ) { cell . columnSpan = rows [ i ] . cells [ j ] . colSpan ; } cellIndex += cell . columnSpan ; } else { cell . value = '' ; cellIndex += cell . columnSpan ; j = j - 1 ; } } } } if ( pdfGrid . columns . count >= 6 ) { pdfGrid . style . allowHorizontalOverflow = true ; } return pdfGrid ; } private processExportProperties ( pdfExportProperties , dataSource , section ) { if ( ! isNullOrUndefined ( pdfExportProperties ) ) { if ( ! isNullOrUndefined ( pdfExportProperties . theme ) ) { this . gridTheme = pdfExportProperties . theme ; } if ( ! isNullOrUndefined ( pdfExportProperties . pageOrientation ) || ! isNullOrUndefined ( pdfExportProperties . pageSize ) ) { let pdfPageSettings = new PdfPageSettings ( ) ; pdfPageSettings . orientation = ( pdfExportProperties . pageOrientation === 'Landscape' ) ? PdfPageOrientation . Landscape : PdfPageOrientation . Portrait ; pdfPageSettings . size = this . getPageSize ( pdfExportProperties . pageSize ) ; section . setPageSettings ( pdfPageSettings ) ; } let clientSize = this . pdfDocument . pageSettings . size ; if ( ! isNullOrUndefined ( pdfExportProperties . header ) ) { let header = pdfExportProperties . header ; let position = new PointF ( 0 , header . fromTop ) ; let size = new SizeF ( ( clientSize . width - 80 ) , ( header . height * 0.75 ) ) ; let bounds = new RectangleF ( position , size ) ; this . pdfDocument . template . top = this . drawPageTemplate ( new PdfPageTemplateElement ( bounds ) , header ) ; } if ( ! isNullOrUndefined ( pdfExportProperties . footer ) ) { let footer = pdfExportProperties . footer ; let position = new PointF ( 0 , ( ( clientSize . width - 80 ) - ( footer . fromBottom * 0.75 ) ) ) ; let size = new SizeF ( ( clientSize . width - 80 ) , ( footer . height * 0.75 ) ) ; let bounds = new RectangleF ( position , size ) ; this . pdfDocument . template . bottom = this . drawPageTemplate ( new PdfPageTemplateElement ( bounds ) , footer ) ; } if ( ! isNullOrUndefined ( pdfExportProperties . includeHiddenColumn ) && ! this . isGrouping ) { this . hideColumnInclude = pdfExportProperties . includeHiddenColumn ; } if ( ! isNullOrUndefined ( pdfExportProperties . dataSource ) ) { if ( ! ( pdfExportProperties . dataSource instanceof DataManager ) ) { dataSource = pdfExportProperties . dataSource ; } this . customDataSource = true ; this . currentViewData = false ; } else if ( ! isNullOrUndefined ( pdfExportProperties . exportType ) ) { if ( pdfExportProperties . exportType === 'CurrentPage' ) { dataSource = this . parent . currentViewData ; this . currentViewData = true ; this . customDataSource = false ; } else { this . currentViewData = false ; this . customDataSource = false ; } } else { this . currentViewData = false ; this . customDataSource = false ; } } else { this . currentViewData = false ; this . customDataSource = false ; } return { dataSource : dataSource , section : section } ; } private drawPageTemplate ( template , element ) { for ( let content of element . contents ) { this . processContentValidation ( content ) ; switch ( content . type ) { case 'Text' : if ( content . value === '' || content . value === undefined || content . value === null || typeof content . value !== 'string' ) { throw new Error ( 'please enter the valid input value in text content...' ) ; } this . drawText ( template , content ) ; break ; case 'PageNumber' : this . drawPageNumber ( template , content ) ; break ; case 'Image' : if ( content . src === undefined || content . src === null || content . src === '' ) { throw new Error ( 'please enter the valid base64 string in image content...' ) ; } this . drawImage ( template , content ) ; break ; case 'Line' : this . drawLine ( template , content ) ; break ; default : throw new Error ( 'Please set valid content type...' ) ; } } return template ; } private processContentValidation ( content ) { if ( content . type === undefined || content . type === null ) { throw new Error ( 'please set valid content type...' ) ; } else { if ( content . type === 'Line' ) { if ( content . points === undefined || content . points === null ) { throw new Error ( 'please enter valid points in ' + content . type + ' content...' ) ; } else { if ( content . points . x1 === undefined || content . points . x1 === null || typeof content . points . x1 !== 'number' ) { throw new Error ( 'please enter valid x1 co-ordinate in ' + content . type + ' points...' ) ; } if ( content . points . y1 === undefined || content . points . y1 === null || typeof content . points . y1 !== 'number' ) { throw new Error ( 'please enter valid y1 co-ordinate in ' + content . type + ' points...' ) ; } if ( content . points . x2 === undefined || content . points . x2 === null || typeof content . points . x2 !== 'number' ) { throw new Error ( 'please enter valid x2 co-ordinate in ' + content . type + ' points...' ) ; } if ( content . points . y2 === undefined || content . points . y2 === null || typeof content . points . y2 !== 'number' ) { throw new Error ( 'please enter valid y2 co-ordinate in ' + content . type + ' points...' ) ; } } } else { if ( content . position === undefined || content . position === null ) { throw new Error ( 'please enter valid position in ' + content . type + ' content...' ) ; } else { if ( content . position . x === undefined || content . position . x === null || typeof content . position . x !== 'number' ) { throw new Error ( 'please enter valid x co-ordinate in ' + content . type + ' position...' ) ; } if ( content . position . y === undefined || content . position . y === null || typeof content . position . y !== 'number' ) { throw new Error ( 'please enter valid y co-ordinate in ' + content . type + ' position...' ) ; } } } } } private drawText ( pageTemplate , content ) { let font = this . getFont ( content ) ; let brush = this . getBrushFromContent ( content ) ; let pen = null ; if ( ! isNullOrUndefined ( content . style . textPenColor ) ) { let penColor = this . hexToRgb ( content . style . textPenColor ) ; pen = new PdfPen ( new PdfColor ( penColor . r , penColor . g , penColor . b ) ) ; } if ( brush == null && pen == null ) { brush = new PdfSolidBrush ( new PdfColor ( 0 , 0 , 0 ) ) ; } let value = content . value . toString ( ) ; let x = content . position . x * 0.75 ; let y = content . position . y * 0.75 ; let format ; let result = this . setContentFormat ( content , format ) ; if ( result !== null && ! isNullOrUndefined ( result . format ) && ! isNullOrUndefined ( result . size ) ) { pageTemplate . graphics . drawString ( value , font , pen , brush , x , y , result . size . width , result . size . height , result . format ) ; } else { pageTemplate . graphics . drawString ( value , font , pen , brush , x , y , format ) ; } } private drawPageNumber ( documentHeader , content ) { let font = this . getFont ( content ) ; let brush = null ; if ( ! isNullOrUndefined ( content . style . textBrushColor ) ) { let brushColor = this . hexToRgb ( content . style . textBrushColor ) ; brush = new PdfSolidBrush ( new PdfColor ( brushColor . r , brushColor . g , brushColor . b ) ) ; } else { brush = new PdfSolidBrush ( new PdfColor ( 0 , 0 , 0 ) ) ; } let pageNumber = new PdfPageNumberField ( font , brush ) ; pageNumber . numberStyle = this . getPageNumberStyle ( content . pageNumberType ) ; let compositeField ; let format ; if ( ! isNullOrUndefined ( content . format ) ) { if ( ( content . format as string ) . indexOf ( '$total' ) !== - 1 && ( content . format as string ) . indexOf ( '$current' ) !== - 1 ) { let pageCount = new PdfPageCountField ( font ) ; if ( ( content . format as string ) . indexOf ( '$total' ) > ( content . format as string ) . indexOf ( '$current' ) ) { format = ( content . format as string ) . replace ( '$current' , '0' ) ; format = format . replace ( '$total' , '1' ) ; } else { format = ( content . format as string ) . replace ( '$current' , '1' ) ; format = format . replace ( '$total' , '0' ) ; } compositeField = new PdfCompositeField ( font , brush , format , pageNumber , pageCount ) ; } else if ( ( content . format as string ) . indexOf ( '$current' ) !== - 1 && ( content . format as string ) . indexOf ( '$total' ) === - 1 ) { format = ( content . format as string ) . replace ( '$current' , '0' ) ; compositeField = new PdfCompositeField ( font , brush , format , pageNumber ) ; } else { let pageCount = new PdfPageCountField ( font ) ; format = ( content . format as string ) . replace ( '$total' , '0' ) ; compositeField = new PdfCompositeField ( font , brush , format , pageCount ) ; } } else { format = '{0}' ; compositeField = new PdfCompositeField ( font , brush , format , pageNumber ) ; } let x = content . position . x * 0.75 ; let y = content . position . y * 0.75 ; let result = this . setContentFormat ( content , compositeField . stringFormat ) ; if ( result !== null && ! isNullOrUndefined ( result . format ) && ! isNullOrUndefined ( result . size ) ) { compositeField . stringFormat = result . format ; compositeField . bounds = new RectangleF ( x , y , result . size . width , result . size . height ) ; } compositeField . draw ( documentHeader . graphics , x , y ) ; } private drawImage ( documentHeader , content ) { let x = content . position . x * 0.75 ; let y = content . position . y * 0.75 ; let width = ( ! isNullOrUndefined ( content . size ) ) ? ( content . size . width * 0.75 ) : undefined ; let height = ( ! isNullOrUndefined ( content . size ) ) ? ( content . size . height * 0.75 ) : undefined ; let image = new PdfBitmap ( content . src ) ; if ( ! isNullOrUndefined ( width ) ) { documentHeader . graphics . drawImage ( image , x , y , width , height ) ; } else { documentHeader . graphics . drawImage ( image , x , y ) ; } } private drawLine ( documentHeader , content ) { let x1 = content . points . x1 * 0.75 ; let y1 = content . points . y1 * 0.75 ; let x2 = content . points . x2 * 0.75 ; let y2 = content . points . y2 * 0.75 ; let pen = this . getPenFromContent ( content ) ; if ( ! isNullOrUndefined ( content . style ) && content . style !== null ) { if ( ! isNullOrUndefined ( content . style . penSize ) && content . style . penSize !== null && typeof content . style . penSize === 'number' ) { pen . width = content . style . penSize * 0.75 ; } pen . dashStyle = this . getDashStyle ( content . style . dashStyle ) ; } documentHeader . graphics . drawLine ( pen , x1 , y1 , x2 , y2 ) ; } private processAggregates ( sRows , pdfGrid , border , font , brush , backgroundBrush , isCaption , captionRow ? , groupIndex ? ) { for ( let row of sRows ) { let startIndex = 0 ; let leastCaptionSummaryIndex = - 1 ; let index = 0 ; let isEmpty = true ; let value = [ ] ; for ( let i = 0 ; i < pdfGrid . columns . count ; i ++ ) { let cell = row . cells [ index ] ; if ( ! this . hideColumnInclude ) { while ( cell . visible === undefined ) { if ( ! isNullOrUndefined ( captionRow ) ) { if ( ! isNullOrUndefined ( captionRow . cells . getCell ( i ) . value ) ) { value . push ( '' ) ; value . push ( captionRow . cells . getCell ( i ) . value ) ; isEmpty = false ; i += 1 ; } else { value . push ( '' ) ; } } else { value . push ( '' ) ; } i += 1 ; index = index + 1 ; cell = row . cells [ index ] ; } while ( ! isNullOrUndefined ( cell . visible ) && ! cell . visible ) { index = index + 1 ; cell = row . cells [ index ] ; } } if ( cell . isDataCell ) { let templateFn = { } ; if ( ! isNullOrUndefined ( cell . column . footerTemplate ) || ! isNullOrUndefined ( cell . column . groupCaptionTemplate ) || ! isNullOrUndefined ( cell . column . groupFooterTemplate ) ) { let result = this . getTemplateFunction ( templateFn , i , leastCaptionSummaryIndex , cell . column ) ; templateFn = result . templateFunction ; leastCaptionSummaryIndex = result . leastCaptionSummaryIndex ; let txt = ( templateFn [ getEnumValue ( CellType , cell . cellType ) ] ( row . data [ cell . column . field ? cell . column . field : cell . column . columnName ] ) ) ; value . push ( ( < Text > txt [ 0 ] ) . wholeText ) ; isEmpty = false ; } else { let result = this . getSummaryWithoutTemplate ( row . data [ cell . column . field ] ) ; if ( ! isNullOrUndefined ( result ) ) { value . push ( result ) ; } } } else { value . push ( '' ) ; } if ( isEmpty && value [ i ] !== '' && ! isNullOrUndefined ( value [ i ] ) && value [ i ] !== null ) { isEmpty = false ; } index += 1 ; } if ( ! isEmpty ) { if ( ! isCaption ) { let gridRow = pdfGrid . rows . addRow ( ) ; gridRow . style . setBorder ( border ) ; gridRow . style . setFont ( font ) ; gridRow . style . setTextBrush ( brush ) ; gridRow . style . setBackgroundBrush ( backgroundBrush ) ; for ( let i = 0 ; i < pdfGrid . columns . count ; i ++ ) { gridRow . cells . getCell ( i ) . value = value [ i ] . toString ( ) ; } } else { for ( let i = 0 ; i < pdfGrid . columns . count ; i ++ ) { captionRow . cells . getCell ( i ) . value = value [ i ] . toString ( ) ; if ( i === ( groupIndex + 1 ) && leastCaptionSummaryIndex !== - 1 ) { captionRow . cells . getCell ( i ) . columnSpan = leastCaptionSummaryIndex - ( groupIndex + 1 ) ; } else if ( i === ( groupIndex + 1 ) && leastCaptionSummaryIndex === - 1 ) { captionRow . cells . getCell ( i ) . columnSpan = pdfGrid . columns . count - ( groupIndex + 1 ) ; } } } } } } private getTemplateFunction ( templateFn , index , leastCaptionSummaryIndex , column ) { if ( ! isNullOrUndefined ( column . footerTemplate ) ) { templateFn [ getEnumValue ( CellType , CellType . Summary ) ] = compile ( column . footerTemplate ) ; } else if ( ! isNullOrUndefined ( column . groupCaptionTemplate ) ) { if ( leastCaptionSummaryIndex === - 1 ) { leastCaptionSummaryIndex = index ; } templateFn [ getEnumValue ( CellType , CellType . CaptionSummary ) ] = compile ( column . groupCaptionTemplate ) ; } else { templateFn [ getEnumValue ( CellType , CellType . GroupSummary ) ] = compile ( column . groupFooterTemplate ) ; } return { templateFunction : templateFn , leastCaptionSummaryIndex : leastCaptionSummaryIndex } ; } private getSummaryWithoutTemplate ( data ) { if ( ! isNullOrUndefined ( data . Sum ) ) { return data . Sum ; } else if ( ! isNullOrUndefined ( data . Average ) ) { return data . Average ; } else if ( ! isNullOrUndefined ( data . Max ) ) { return data . Max ; } else if ( ! isNullOrUndefined ( data . Min ) ) { return data . Min ; } else if ( ! isNullOrUndefined ( data . Count ) ) { return data . Count ; } else if ( ! isNullOrUndefined ( data . TrueCount ) ) { return data . TrueCount ; } else if ( ! isNullOrUndefined ( data . FalseCount ) ) { return data . FalseCount ; } else if ( ! isNullOrUndefined ( data . Custom ) ) { return data . Custom ; } } private setColumnProperties ( gridColumns , pdfGrid ) { let startIndex = this . isGrouping ? ( pdfGrid . columns . count - gridColumns . length ) : 0 ; for ( let i = 0 ; i < gridColumns . length ; i ++ ) { if ( ! isNullOrUndefined ( gridColumns [ i ] . textAlign ) ) { pdfGrid . columns . getColumn ( i + startIndex ) . format = this . getHorizontalAlignment ( gridColumns [ i ] . textAlign ) ; } if ( pdfGrid . style . allowHorizontalOverflow && ! isNullOrUndefined ( gridColumns [ i ] . width ) ) { pdfGrid . columns . getColumn ( i + startIndex ) . width = typeof gridColumns [ i ] . width === 'number' ? gridColumns [ i ] . width as number * 0.75 : this . helper . getConvertedWidth ( gridColumns [ i ] . width as string ) * 0.75 ; } } } private private setRecordThemeStyle ( row , border ) { if ( ! isNullOrUndefined ( this . gridTheme ) && ! isNullOrUndefined ( this . gridTheme . record ) && this . gridTheme . record !== null ) { let pdfColor = new PdfColor ( ) ; if ( ! isNullOrUndefined ( this . gridTheme . record . fontColor ) ) { let penBrushColor = this . hexToRgb ( this . gridTheme . record . fontColor ) ; pdfColor = new PdfColor ( penBrushColor . r , penBrushColor . g , penBrushColor . b ) ; } row . style . setTextBrush ( new PdfSolidBrush ( pdfColor ) ) ; } else { row . style . setTextBrush ( new PdfSolidBrush ( new PdfColor ( 0 , 0 , 0 ) ) ) ; } row . style . setBorder ( border ) ; return row ; } private private processRecord ( border , columns , gObj , dataSource , pdfGrid , groupIndex ? ) { let startIndex = this . isGrouping ? groupIndex : 0 ; for ( let items of ( dataSource as { } [ ] ) ) { let gridRow = this . setRecordThemeStyle ( pdfGrid . rows . addRow ( ) , border ) ; for ( let j = 0 ; j < columns . length ; j ++ ) { let value = ( ! isNullOrUndefined ( columns [ j ] . field ) && getValue ( columns [ j ] . field , items ) ) || '' ; let column = columns [ j ] ; let foreignKeyData ; if ( column . isForeignColumn && column . isForeignColumn ( ) ) { foreignKeyData = this . helper . getFData ( value , column ) ; value = getValue ( column . foreignKeyValue , foreignKeyData ) ; } let data = items ; let args = { data : data , value : value , column : column , style : undefined , colSpan : 1 } ; args . value = this . exportValueFormatter . formatCellValue ( args ) ; gObj . trigger ( events . pdfQueryCellInfo , args ) ; let cell = gridRow . cells . getCell ( j + startIndex ) ; cell . value = args . value ; if ( ! isNullOrUndefined ( args . style ) ) { this . processCellStyle ( cell , args ) ; } if ( args . colSpan > 1 ) { if ( ( j + startIndex + 1 + args . colSpan ) > gridRow . cells . count ) { args . colSpan = gridRow . cells . count - ( j + startIndex + 1 ) ; } cell . columnSpan = args . colSpan ; for ( let i = 1 ; i < cell . columnSpan ; i ++ ) { let spanCell = gridRow . cells . getCell ( j + startIndex + i ) ; spanCell . value = '' ; } j += ( args . colSpan - 1 ) ; } } } } private processCellStyle ( cell , args ) { if ( ! isNullOrUndefined ( args . style . backgroundColor ) ) { let backColor = this . hexToRgb ( args . style . backgroundColor ) ; cell . style . backgroundBrush = new PdfSolidBrush ( new PdfColor ( backColor . r , backColor . g , backColor . b ) ) ; } if ( ! isNullOrUndefined ( args . style . textAlignment ) ) { cell . style . stringFormat = this . getHorizontalAlignment ( args . style . textAlignment ) ; } if ( ! isNullOrUndefined ( args . style . verticalAlignment ) ) { cell . style . stringFormat = this . getVerticalAlignment ( args . style . verticalAlignment , cell . style . stringFormat ) ; } if ( ! isNullOrUndefined ( args . style . textBrushColor ) ) { let textBrushColor = this . hexToRgb ( args . style . textBrushColor ) ; cell . style . textBrush = new PdfSolidBrush ( new PdfColor ( textBrushColor . r , textBrushColor . g , textBrushColor . b ) ) ; } if ( ! isNullOrUndefined ( args . style . textPenColor ) ) { let textPenColor = this . hexToRgb ( args . style . textPenColor ) ; cell . style . textPen = new PdfPen ( new PdfColor ( textPenColor . r , textPenColor . g , textPenColor . b ) ) ; } if ( ! isNullOrUndefined ( args . style . fontFamily ) || ! isNullOrUndefined ( args . style . fontSize ) || ! isNullOrUndefined ( args . style . bold ) || ! isNullOrUndefined ( args . style . italic ) || ! isNullOrUndefined ( args . style . underline ) || ! isNullOrUndefined ( args . style . strikeout ) ) { cell . style . font = this . getFont ( args ) ; } if ( ! isNullOrUndefined ( args . style . border ) ) { let border = new PdfBorders ( ) ; let borderWidth = args . style . border . width ; let width = ( ! isNullOrUndefined ( borderWidth ) && typeof borderWidth === 'number' ) ? ( borderWidth * 0.75 ) : ( undefined ) ; let color = new PdfColor ( 196 , 196 , 196 ) ; if ( ! isNullOrUndefined ( args . style . border . color ) ) { let borderColor = this . hexToRgb ( args . style . border . color ) ; color = new PdfColor ( borderColor . r , borderColor . g , borderColor . b ) ; } let pen = new PdfPen ( color , width ) ; if ( ! isNullOrUndefined ( args . style . border . dashStyle ) ) { pen . dashStyle = this . getDashStyle ( args . style . border . dashStyle ) ; } border . all = pen ; cell . style . borders = border ; } if ( ! isNullOrUndefined ( args . style . paragraphIndent ) ) { cell . style . stringFormat = new PdfStringFormat ( ) ; cell . style . stringFormat . paragraphIndent = args . style . paragraphIndent ; } } private private getHorizontalAlignment ( textAlign , format ? ) { if ( format === undefined ) { format = new PdfStringFormat ( ) ; } switch ( textAlign ) { case 'Right' : format . alignment = PdfTextAlignment . Right ; break ; case 'Center' : format . alignment = PdfTextAlignment . Center ; break ; case 'Justify' : format . alignment = PdfTextAlignment . Justify ; break ; case 'Left' : format . alignment = PdfTextAlignment . Left ; break ; } return format ; } private private getVerticalAlignment ( verticalAlign , format ? , textAlign ? ) { if ( format === undefined ) { format = new PdfStringFormat ( ) ; format = this . getHorizontalAlignment ( textAlign , format ) ; } switch ( verticalAlign ) { case 'Bottom' : format . lineAlignment = PdfVerticalAlignment . Bottom ; break ; case 'Middle' : format . lineAlignment = PdfVerticalAlignment . Middle ; break ; case 'Top' : format . lineAlignment = PdfVerticalAlignment . Top ; break ; } return format ; } private getFontFamily ( fontFamily ) { switch ( fontFamily ) { case 'TimesRoman' : return 2 ; case 'Courier' : return 1 ; case 'Symbol' : return 3 ; case 'ZapfDingbats' : return 4 ; default : return 0 ; } } private getFont ( content ) { let fontSize = ( ! isNullOrUndefined ( content . style . fontSize ) ) ? ( content . style . fontSize * 0.75 ) : 9.75 ; let fontFamily = ( ! isNullOrUndefined ( content . style . fontFamily ) ) ? ( this . getFontFamily ( content . style . fontFamily ) ) : PdfFontFamily . Helvetica ; let fontStyle = PdfFontStyle . Regular ; if ( ! isNullOrUndefined ( content . style . bold ) && content . style . bold ) { fontStyle |= PdfFontStyle . Bold ; } if ( ! isNullOrUndefined ( content . style . italic ) && content . style . italic ) { fontStyle |= PdfFontStyle . Italic ; } if ( ! isNullOrUndefined ( content . style . underline ) && content . style . underline ) { fontStyle |= PdfFontStyle . Underline ; } if ( ! isNullOrUndefined ( content . style . strikeout ) && content . style . strikeout ) { fontStyle |= PdfFontStyle . Strikeout ; } return new PdfStandardFont ( fontFamily , fontSize , fontStyle ) ; } private getPageNumberStyle ( pageNumberType ) { switch ( pageNumberType ) { case 'LowerLatin' : return 2 ; case 'LowerRoman' : return 3 ; case 'UpperLatin' : return 4 ; case 'UpperRoman' : return 5 ; default : return 1 ; } } private setContentFormat ( content , format ) { if ( ! isNullOrUndefined ( content . size ) ) { let width = content . size . width * 0.75 ; let height = content . size . height * 0.75 ; format = new PdfStringFormat ( PdfTextAlignment . Left , PdfVerticalAlignment . Middle ) ; if ( ! isNullOrUndefined ( content . style . hAlign ) ) { switch ( content . style . hAlign ) { case 'Right' : format . alignment = PdfTextAlignment . Right ; break ; case 'Center' : format . alignment = PdfTextAlignment . Center ; break ; case 'Justify' : format . alignment = PdfTextAlignment . Justify ; break ; default : format . alignment = PdfTextAlignment . Left ; } } if ( ! isNullOrUndefined ( content . style . vAlign ) ) { format = this . getVerticalAlignment ( content . style . vAlign , format ) ; } return { format : format , size : new SizeF ( width , height ) } ; } return null ; } private getPageSize ( pageSize ) { switch ( pageSize ) { case 'Letter' : return new SizeF ( 612 , 792 ) ; case 'Note' : return new SizeF ( 540 , 720 ) ; case 'Legal' : return new SizeF ( 612 , 1008 ) ; case 'A0' : return new SizeF ( 2380 , 3368 ) ; case 'A1' : return new SizeF ( 1684 , 2380 ) ; case 'A2' : return new SizeF ( 1190 , 1684 ) ; case 'A3' : return new SizeF ( 842 , 1190 ) ; case 'A5' : return new SizeF ( 421 , 595 ) ; case 'A6' : return new SizeF ( 297 , 421 ) ; case 'A7' : return new SizeF ( 210 , 297 ) ; case 'A8' : return new SizeF ( 148 , 210 ) ; case 'A9' : return new SizeF ( 105 , 148 ) ; case 'B0' : return new SizeF ( 2836 , 4008 ) ; case 'B1' : return new SizeF ( 2004 , 2836 ) ; case 'B2' : return new SizeF ( 1418 , 2004 ) ; case 'B3' : return new SizeF ( 1002 , 1418 ) ; case 'B4' : return new SizeF ( 709 , 1002 ) ; case 'B5' : return new SizeF ( 501 , 709 ) ; case 'Archa' : return new SizeF ( 648 , 864 ) ; case 'Archb' : return new SizeF ( 864 , 1296 ) ; case 'Archc' : return new SizeF ( 1296 , 1728 ) ; case 'Archd' : return new SizeF ( 1728 , 2592 ) ; case 'Arche' : return new SizeF ( 2592 , 3456 ) ; case 'Flsa' : return new SizeF ( 612 , 936 ) ; case 'HalfLetter' : return new SizeF ( 396 , 612 ) ; case 'Letter11x17' : return new SizeF ( 792 , 1224 ) ; case 'Ledger' : return new SizeF ( 1224 , 792 ) ; default : return new SizeF ( 595 , 842 ) ; } } private getDashStyle ( dashStyle ) { switch ( dashStyle ) { case 'Dash' : return 1 ; case 'Dot' : return 2 ; case 'DashDot' : return 3 ; case 'DashDotDot' : return 4 ; default : return 0 ; } } private getPenFromContent ( content ) { let pen = new PdfPen ( new PdfColor ( 0 , 0 , 0 ) ) ; if ( ! isNullOrUndefined ( content . style ) && content . style !== null && ! isNullOrUndefined ( content . style . penColor ) ) { let penColor = this . hexToRgb ( content . style . penColor ) ; pen = new PdfPen ( new PdfColor ( penColor . r , penColor . g , penColor . b ) ) ; } return pen ; } private getBrushFromContent ( content ) { let brush = null ; if ( ! isNullOrUndefined ( content . style . textBrushColor ) ) { let brushColor = this . hexToRgb ( content . style . textBrushColor ) ; brush = new PdfSolidBrush ( new PdfColor ( brushColor . r , brushColor . g , brushColor . b ) ) ; } return brush ; } private hexToRgb ( hex ) { if ( hex === null || hex === '' || hex . length !== 7 ) { throw new Error ( 'please set valid hex value for color...' ) ; } hex = hex . substring ( 1 ) ; let bigint = parseInt ( hex , 16 ) ; let r = ( bigint >> 16 ) & 255 ; let g = ( bigint >> 8 ) & 255 ; let b = bigint & 255 ; return { r : r , g : g , b : b } ; } return hidden public destroy ( ) { } } interface SummaryData { aggregates ? : Object ; level ? : number ; }