android:pdfdocument:pdfdocument_start
pdfdocument start
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 양쪽 이전 판이전 판다음 판 | 이전 판 | ||
| android:pdfdocument:pdfdocument_start [2024/12/05 20:56] – 이거니맨 | android:pdfdocument:pdfdocument_start [2024/12/05 21:45] (현재) – [1. PDF Util Class] 이거니맨 | ||
|---|---|---|---|
| 줄 98: | 줄 98: | ||
| 이를태면 나는 ui.theme폴더에 다음과 같이 fontfamily.kt를 만들어뒀다. | 이를태면 나는 ui.theme폴더에 다음과 같이 fontfamily.kt를 만들어뒀다. | ||
| - | <file kotlin | + | <file kotlin fontfamily.kt> |
| val fontGoryeong = FontFamily( | val fontGoryeong = FontFamily( | ||
| Font(R.font.goryeongstrawberry, | Font(R.font.goryeongstrawberry, | ||
| 줄 107: | 줄 107: | ||
| Font(R.font.ttoppokki, | Font(R.font.ttoppokki, | ||
| ) | ) | ||
| + | </ | ||
| + | |||
| + | ==== 3. 페인트 오브젝트에 폰트리소스 연결하기 ==== | ||
| + | |||
| + | 페인트 오브젝트에 폰트를 연결하는 함수는 | ||
| + | |||
| + | <code kotlin> | ||
| + | title.setTypeface(fontStrawberry) | ||
| + | ttoppokki.setTypeface(fontTtoppoki) | ||
| + | </ | ||
| + | |||
| + | ==== 4. 컬러, 텍스트 크기, 텍스트 정렬 ==== | ||
| + | |||
| + | 다음과 같은 방식으로 Paint오브젝트의 컬러, 크기, 정렬을 할 수 있다. | ||
| + | |||
| + | <code kotlin> | ||
| + | // title paint setting | ||
| + | title.setTypeface(fontStrawberry) | ||
| + | title.color = ContextCompat.getColor(context, | ||
| + | title.textSize = 16f | ||
| + | | ||
| + | // ttoppokki paint setting | ||
| + | ttoppokki.setTypeface(fontTtoppoki) | ||
| + | ttoppokki.color = ContextCompat.getColor(context, | ||
| + | ttoppokki.textSize = 12f | ||
| + | ttoppokki.textAlign = Paint.Align.CENTER | ||
| + | </ | ||
| + | |||
| + | 여기서 color는 res 폴더 내에 colors.xml로 정의되어 있는 컬러코드를 말한다. ui.theme에 정의되어 있는 Color.kt가 아님에 유의하자. | ||
| + | |||
| + | |||
| + | 참고로 나는 colors.xml을 다음과 같이 정의하였다. | ||
| + | |||
| + | <code xml> | ||
| + | <?xml version=" | ||
| + | < | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | <color name=" | ||
| + | </ | ||
| </ | </ | ||
| + | ===== 클래스화 하기 ===== | ||
| + | |||
| + | ==== 1. PDF Util Class ==== | ||
| + | |||
| + | 지금까를 정리하면 다음과 같이 텍스트를 만드는 기능들을 클래스화 할 수 있을 것이다. | ||
| + | |||
| + | <file kotlin PDFUtil.kt> | ||
| + | class PDFUtil constructor(val context: Context) { | ||
| + | |||
| + | / | ||
| + | val PDF_PAGE_WIDTH = 595 //8.26 Inch | ||
| + | val PDF_PAGE_HEIGHT = 842 //11.69 Inch | ||
| + | |||
| + | /* Font Resource */ | ||
| + | val fontStrawberry = ResourcesCompat.getFont(context, | ||
| + | |||
| + | |||
| + | // A4 Size Page Info | ||
| + | val A4 = a4Paper() | ||
| + | |||
| + | // Paint object to make a Text | ||
| + | val title: Paint = Paint() | ||
| + | |||
| + | |||
| + | // A4 Size Paper Initializer | ||
| + | private fun a4Paper() : PdfDocument.PageInfo { | ||
| + | |||
| + | // Page Information : Page's Width, Height, and PageNumber | ||
| + | val myPageInfo: PdfDocument.PageInfo = | ||
| + | PdfDocument.PageInfo.Builder(PDF_PAGE_WIDTH, | ||
| + | |||
| + | return myPageInfo | ||
| + | } | ||
| + | |||
| + | // Draw Title | ||
| + | fun titleText(text : String, canvas: Canvas) { | ||
| + | title.setTypeface(fontStrawberry) | ||
| + | title.color = ContextCompat.getColor(context, | ||
| + | title.textSize = 16f | ||
| + | title.textAlign = Paint.Align.CENTER | ||
| + | |||
| + | // Draw Text | ||
| + | canvas.drawText(text, | ||
| + | |||
| + | } | ||
| + | |||
| + | |||
| + | @Throws(IOException:: | ||
| + | fun savePdfFileExternalStorage(filename: | ||
| + | |||
| + | try { | ||
| + | // First, creating External Storage Directory, and a file name, | ||
| + | // Second, write our PDF file to that location. | ||
| + | // Third, close file | ||
| + | val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) | ||
| + | val file = File(dir, filename) | ||
| + | val fos = FileOutputStream(file) | ||
| + | document.writeTo(fos) | ||
| + | fos.close() | ||
| + | |||
| + | // on below line we are displaying a toast message as PDF file generated.. | ||
| + | Toast.makeText(context, | ||
| + | } catch (e: Exception) { | ||
| + | // below line is used | ||
| + | // to handle error | ||
| + | e.printStackTrace() | ||
| + | |||
| + | // on below line we are displaying a toast message as fail to generate PDF | ||
| + | Toast.makeText(context, | ||
| + | } | ||
| + | } | ||
| + | |||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 2. 유틸리티 클래스 사용하기 ==== | ||
| + | |||
| + | 위에서 만들 클래스는 다음과 같이 사용할 수 있다. | ||
| + | |||
| + | <file kotlin pdfgenerator.kt> | ||
| + | fun generatePDFStart(context: | ||
| + | { | ||
| + | // creating a PDF Document instance | ||
| + | val pdfDocument: | ||
| + | |||
| + | // PDFUtil instance | ||
| + | val pdfUtil = PDFUtil(context) | ||
| + | // Page1 Start | ||
| + | val page1: PdfDocument.Page = pdfDocument.startPage(pdfUtil.A4) | ||
| + | |||
| + | // creating a variable for canvas from our page of PDF. | ||
| + | val canvas: Canvas = page1.canvas | ||
| + | |||
| + | |||
| + | pdfUtil.titleText(" | ||
| + | |||
| + | |||
| + | // End of Page | ||
| + | pdfDocument.finishPage(page1) | ||
| + | |||
| + | |||
| + | // Save PDF file | ||
| + | pdfUtil.savePdfFileExternalStorage(" | ||
| + | |||
| + | // Closing PDF Document | ||
| + | pdfDocument.close() | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | 이렇게 하면 상당히 코드가 깔끔해진다. | ||
| + | |||
| + | 다음번 부터는 이런 방식으로 소제목 만들기와 표 만들기를 해보자. | ||
android/pdfdocument/pdfdocument_start.1733399764.txt.gz · 마지막으로 수정됨: 저자 이거니맨
