package Ġcom . cr az yle g end . kotlin extensions . pdf Ċ Ċ import Ġandroid . graphics . Bitmap Ċ import Ġandroid . graphics . Canvas Ċ import Ġandroid . graphics . Color Ċ import Ġandroid . graphics . Paint Ċ import Ġandroid . graphics . pdf . Pdf Document Ċ import Ġandroid . net . Uri Ċ import Ġandroid . os . Environment Ċ import Ġandroid . view . View Ċ import Ġandroid . view . ViewGroup Ċ import Ġandroid . widget . LinearLayout Ċ import Ġandroidx . core . content . File Provider Ċ import Ġjava . io . File Ċ import Ġjava . io . File OutputStream Ċ import Ġjava . util .* ĊĊ Ċ /** Ċ Ġ* ĠCreated Ġby Ġh rist ij an Ġon Ġ 3 / 2 8 / 1 9 Ġto Ġlong Ġlive Ġand Ġpro sper Ġ! Ċ Ġ*/ ĊĊ Ċ fun ĠView . create PDF ( media Dir : ĠString , Ġuri Authority : ĠString ): ĠUri ? Ġ{ ĊĊĠĠĠ Ġval Ġfile : ĠFile ? ĊĠĠĠ Ġval Ġuuid Ġ= ĠUUID . randomUUID (). toString () ĊĠĠĠ Ġval Ġdirectory Ġ= ĠFile ( Environment . getData Directory (), Ġmedia Dir ) ĊĊĠĠĠ Ġfile Ġ= Ġif Ġ( directory . exists ()) Ġ{ ĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġuuid . plus (". pdf ")) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġdirectory . mkdir s () ĊĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġuuid . plus (". pdf ")) ĊĊĠĠĠ Ġ} ĊĠĠĠ Ġfile . createNew File () ĊĊĠĠĠ Ġval Ġf os Ġ= ĠFileOutputStream ( file ) ĊĊĊĠĠĠ Ġthis . layout Params Ġ= ĠViewGroup . LayoutParams ( LinearLayout . LayoutParams . MATCH _ PARENT , ĠLinearLayout . LayoutParams . WRAP _ CONTENT ) ĊĠĠĠ Ġthis . measure ( View . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED ), ĠView . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED )) ĊĊĠĠĠ Ġthis . layout ( 0 , Ġ 0 , Ġthis . meas ured Width , Ġthis . meas ured Height ) ĊĊĠĠĠ Ġval Ġbitmap Ġ= ĠBitmap . create Bitmap ( this . meas ured Width , Ġthis . meas ured Height , ĠBitmap . Config . ARGB _ 8 8 8 8 ) ĊĠĠĠ Ġval Ġcanvas Ġ= ĠCanvas ( bitmap ) ĊĠĠĠ Ġval Ġpaint Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġcanvas . draw Paint ( paint ) ĊĊĠĠĠ Ġthis . draw ( canvas ) ĊĊĠĠĠ Ġval Ġpdf Document Ġ= ĠPdf Document () ĊĠĠĠ Ġval Ġpage Info Ġ= ĠPdf Document . Page Info . Builder ( bitmap . width , Ġbitmap . height , Ġ 0 ). create () ĊĠĠĠ Ġval Ġpage Ġ= Ġpdf Document . start Page ( page Info ) ĊĊĠĠĠ Ġval Ġpage Canvas Ġ= Ġpage . canvas ĊĠĠĠ Ġval Ġpaint Page Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint Page . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint Page . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġpage Canvas . draw Paint ( paint Page ) ĊĠĠĠ Ġpage Canvas . draw Bitmap ( bitmap , Ġ 0 f , Ġ 0 f , Ġpaint Page ) ĊĊĠĠĠ Ġpdf Document . finish Page ( page ) ĊĠĠĠ Ġpdf Document . writeTo ( fos ) ĊĠĠĠ Ġpdf Document . close () ĊĠĠĠ Ġf os . flush () ĊĠĠĠ Ġf os . close () ĊĊĠĠĠ Ġreturn ĠFile Provider . get Uri For File ( this . context , Ġuri Authority , Ġfile ) Ċ Ċ } Ċ Ċ fun ĠView . create PDF ( pdf Name Without Dot PDF : ĠString , Ġmedia Dir : ĠString , Ġuri Authority : ĠString ): ĠUri ? Ġ{ ĊĊĠĠĠ Ġval Ġfile : ĠFile ? ĊĠĠĠ Ġval Ġdirectory Ġ= ĠFile ( Environment . getData Directory (), Ġmedia Dir ) ĊĊĠĠĠ Ġfile Ġ= Ġif Ġ( directory . exists ()) Ġ{ ĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġdirectory . mkdir s () ĊĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĊĠĠĠ Ġ} ĊĠĠĠ Ġfile . createNew File () ĊĊĠĠĠ Ġval Ġf os Ġ= ĠFileOutputStream ( file ) ĊĊĊĠĠĠ Ġthis . layout Params Ġ= ĠViewGroup . LayoutParams ( LinearLayout . LayoutParams . MATCH _ PARENT , ĠLinearLayout . LayoutParams . WRAP _ CONTENT ) ĊĠĠĠ Ġthis . measure ( View . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED ), ĠView . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED )) ĊĊĠĠĠ Ġthis . layout ( 0 , Ġ 0 , Ġthis . meas ured Width , Ġthis . meas ured Height ) ĊĊĠĠĠ Ġval Ġbitmap Ġ= ĠBitmap . create Bitmap ( this . meas ured Width , Ġthis . meas ured Height , ĠBitmap . Config . ARGB _ 8 8 8 8 ) ĊĠĠĠ Ġval Ġcanvas Ġ= ĠCanvas ( bitmap ) ĊĠĠĠ Ġval Ġpaint Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġcanvas . draw Paint ( paint ) ĊĊĠĠĠ Ġthis . draw ( canvas ) ĊĊĠĠĠ Ġval Ġpdf Document Ġ= ĠPdf Document () ĊĠĠĠ Ġval Ġpage Info Ġ= ĠPdf Document . Page Info . Builder ( bitmap . width , Ġbitmap . height , Ġ 0 ). create () ĊĠĠĠ Ġval Ġpage Ġ= Ġpdf Document . start Page ( page Info ) ĊĊĠĠĠ Ġval Ġpage Canvas Ġ= Ġpage . canvas ĊĠĠĠ Ġval Ġpaint Page Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint Page . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint Page . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġpage Canvas . draw Paint ( paint Page ) ĊĠĠĠ Ġpage Canvas . draw Bitmap ( bitmap , Ġ 0 f , Ġ 0 f , Ġpaint Page ) ĊĊĠĠĠ Ġpdf Document . finish Page ( page ) ĊĠĠĠ Ġpdf Document . writeTo ( fos ) ĊĠĠĠ Ġpdf Document . close () ĊĠĠĠ Ġf os . flush () ĊĠĠĠ Ġf os . close () ĊĊĠĠĠ Ġreturn ĠFile Provider . get Uri For File ( this . context , Ġuri Authority , Ġfile ) Ċ } Ċ Ċ fun ĠView . create PDF ( pdf Name Without Dot PDF : ĠString , Ġpage Number : ĠInt , Ġmedia Dir : ĠString , Ġuri Authority : ĠString ): ĠUri ? Ġ{ ĊĊĠĠĠ Ġval Ġfile : ĠFile ? ĊĠĠĠ Ġval Ġdirectory Ġ= ĠFile ( Environment . getData Directory (), Ġmedia Dir ) ĊĊĠĠĠ Ġfile Ġ= Ġif Ġ( directory . exists ()) Ġ{ ĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġdirectory . mkdir s () ĊĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĊĠĠĠ Ġ} ĊĠĠĠ Ġfile . createNew File () ĊĊĠĠĠ Ġval Ġf os Ġ= ĠFileOutputStream ( file ) ĊĊĊĠĠĠ Ġthis . layout Params Ġ= ĠViewGroup . LayoutParams ( LinearLayout . LayoutParams . MATCH _ PARENT , ĠLinearLayout . LayoutParams . WRAP _ CONTENT ) ĊĠĠĠ Ġthis . measure ( View . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED ), ĠView . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED )) ĊĊĠĠĠ Ġthis . layout ( 0 , Ġ 0 , Ġthis . meas ured Width , Ġthis . meas ured Height ) ĊĊĠĠĠ Ġval Ġbitmap Ġ= ĠBitmap . create Bitmap ( this . meas ured Width , Ġthis . meas ured Height , ĠBitmap . Config . ARGB _ 8 8 8 8 ) ĊĠĠĠ Ġval Ġcanvas Ġ= ĠCanvas ( bitmap ) ĊĠĠĠ Ġval Ġpaint Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġcanvas . draw Paint ( paint ) ĊĊĠĠĠ Ġthis . draw ( canvas ) ĊĊĠĠĠ Ġval Ġpdf Document Ġ= ĠPdf Document () ĊĠĠĠ Ġval Ġpage Info Ġ= ĠPdf Document . Page Info . Builder ( bitmap . width , Ġbitmap . height , Ġpage Number ). create () ĊĠĠĠ Ġval Ġpage Ġ= Ġpdf Document . start Page ( page Info ) ĊĊĠĠĠ Ġval Ġpage Canvas Ġ= Ġpage . canvas ĊĠĠĠ Ġval Ġpaint Page Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint Page . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint Page . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġpage Canvas . draw Paint ( paint Page ) ĊĠĠĠ Ġpage Canvas . draw Bitmap ( bitmap , Ġ 0 f , Ġ 0 f , Ġpaint Page ) ĊĊĠĠĠ Ġpdf Document . finish Page ( page ) ĊĠĠĠ Ġpdf Document . writeTo ( fos ) ĊĠĠĠ Ġpdf Document . close () ĊĠĠĠ Ġf os . flush () ĊĠĠĠ Ġf os . close () ĊĊĠĠĠ Ġreturn ĠFile Provider . get Uri For File ( this . context , Ġuri Authority , Ġfile ) Ċ } Ċ Ċ fun ĠView . create PDF ( pdf Name Without Dot PDF : ĠString , Ġpage Number : ĠInt , Ġmedia Dir : ĠString , Ġuri Authority : ĠString , Ġbackground : ĠInt Ġ= ĠColor . WHITE ): ĠUri ? Ġ{ ĊĊĠĠĠ Ġval Ġfile : ĠFile ? ĊĠĠĠ Ġval Ġdirectory Ġ= ĠFile ( Environment . getData Directory (), Ġmedia Dir ) ĊĊĠĠĠ Ġfile Ġ= Ġif Ġ( directory . exists ()) Ġ{ ĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĠĠĠ Ġ} Ġelse Ġ{ ĊĠĠĠĠĠĠĠ Ġdirectory . mkdir s () ĊĊĠĠĠĠĠĠĠ ĠFile ( directory , Ġpdf Name Without Dot PDF . plus (". pdf ")) ĊĊĠĠĠ Ġ} ĊĠĠĠ Ġfile . createNew File () ĊĊĠĠĠ Ġval Ġf os Ġ= ĠFileOutputStream ( file ) ĊĊĊĠĠĠ Ġthis . layout Params Ġ= ĠViewGroup . LayoutParams ( LinearLayout . LayoutParams . MATCH _ PARENT , ĠLinearLayout . LayoutParams . WRAP _ CONTENT ) ĊĠĠĠ Ġthis . measure ( View . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED ), ĠView . Measure Spec . make Measure Spec ( 0 , ĠView . Measure Spec . UNSPECIFIED )) ĊĊĠĠĠ Ġthis . layout ( 0 , Ġ 0 , Ġthis . meas ured Width , Ġthis . meas ured Height ) ĊĊĠĠĠ Ġval Ġbitmap Ġ= ĠBitmap . create Bitmap ( this . meas ured Width , Ġthis . meas ured Height , ĠBitmap . Config . ARGB _ 8 8 8 8 ) ĊĠĠĠ Ġval Ġcanvas Ġ= ĠCanvas ( bitmap ) ĊĠĠĠ Ġval Ġpaint Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint . color Ġ= Ġbackground ĊĠĠĠ Ġcanvas . draw Paint ( paint ) ĊĊĠĠĠ Ġthis . draw ( canvas ) ĊĊĠĠĠ Ġval Ġpdf Document Ġ= ĠPdf Document () ĊĠĠĠ Ġval Ġpage Info Ġ= ĠPdf Document . Page Info . Builder ( bitmap . width , Ġbitmap . height , Ġpage Number ). create () ĊĠĠĠ Ġval Ġpage Ġ= Ġpdf Document . start Page ( page Info ) ĊĊĠĠĠ Ġval Ġpage Canvas Ġ= Ġpage . canvas ĊĠĠĠ Ġval Ġpaint Page Ġ= ĠPaint ( Paint . ANTI _ ALIAS _ FLAG Ġor ĠPaint . LINEAR _ TEXT _ FLAG ) ĊĠĠĠ Ġpaint Page . style Ġ= ĠPaint . Style . FILL ĊĠĠĠ Ġpaint Page . color Ġ= ĠColor . WHITE ĊĠĠĠ Ġpage Canvas . draw Paint ( paint Page ) ĊĠĠĠ Ġpage Canvas . draw Bitmap ( bitmap , Ġ 0 f , Ġ 0 f , Ġpaint Page ) ĊĊĠĠĠ Ġpdf Document . finish Page ( page ) ĊĠĠĠ Ġpdf Document . writeTo ( fos ) ĊĠĠĠ Ġpdf Document . close () ĊĠĠĠ Ġf os . flush () ĊĠĠĠ Ġf os . close () ĊĊĠĠĠ Ġreturn ĠFile Provider . get Uri For File ( this . context , Ġuri Authority , Ġfile ) Ċ } Ċ