android:pdfdocument:pdfdocument_start
pdfdocument start
차이
문서의 선택한 두 판 사이의 차이를 보여줍니다.
| 다음 판 | 이전 판 | ||
| android:pdfdocument:pdfdocument_start [2024/11/29 01:01] – 만듦 이거니맨 | android:pdfdocument:pdfdocument_start [2024/12/05 21:45] (현재) – [1. PDF Util Class] 이거니맨 | ||
|---|---|---|---|
| 줄 27: | 줄 27: | ||
| val PDF_PAGE_HEIGHT = 842 //11.69 Inch | val PDF_PAGE_HEIGHT = 842 //11.69 Inch | ||
| </ | </ | ||
| + | |||
| + | ===== Starting Page ===== | ||
| + | |||
| + | 용지 중앙에 글자를 그리는 예시는 다음과 같다. | ||
| + | |||
| + | <code kotlin> | ||
| + | fun generatePDFStart() | ||
| + | { | ||
| + | / | ||
| + | val PDF_PAGE_WIDTH = 595 //8.26 Inch | ||
| + | val PDF_PAGE_HEIGHT = 842 //11.69 Inch | ||
| + | |||
| + | // creating a PDF Document instance | ||
| + | val pdfDocument: | ||
| + | |||
| + | // Page Information : Page's Width, Height, and PageNumber | ||
| + | val myPageInfo: PdfDocument.PageInfo? | ||
| + | PdfDocument.PageInfo.Builder(PDF_PAGE_WIDTH, | ||
| + | |||
| + | // Page | ||
| + | val myPage: PdfDocument.Page = pdfDocument.startPage(myPageInfo) | ||
| + | |||
| + | // creating a variable for canvas from our page of PDF. | ||
| + | val canvas: Canvas = myPage.canvas | ||
| + | |||
| + | // Variable to make a drawing(Text) | ||
| + | val title: Paint = Paint() | ||
| + | // Draw Text | ||
| + | canvas.drawText(" | ||
| + | |||
| + | pdfDocument.finishPage(myPage) | ||
| + | |||
| + | savePdfFileExternalStorage(" | ||
| + | |||
| + | pdfDocument.close() | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | ===== PDF Font Change ===== | ||
| + | |||
| + | ==== 1. Paint API ==== | ||
| + | |||
| + | PDFDocument API에서는 글자나 그림이나 모두 android.graphics.Paint API(이하에서는 Paint API로 약칭하겠다)를 사용한다. | ||
| + | |||
| + | 제트팩컴퍼즈에서 사용하는 text API는 사용할 수 없으니, Paint API를 사용하여 텍스트를 그려야 한다. | ||
| + | |||
| + | 나는 [[https:// | ||
| + | |||
| + | <code kotlin> | ||
| + | // Paint object to make a Text | ||
| + | val title: Paint = Paint() | ||
| + | val ttoppokki : Paint = Paint() | ||
| + | </ | ||
| + | |||
| + | |||
| + | ==== 2. 폰트 불러오기 ==== | ||
| + | |||
| + | 위의 페인트 오브젝트에서 사용할 폰트는 다음과 같이 불러온다. | ||
| + | |||
| + | <code kotlin> | ||
| + | // Loading Strawberry font | ||
| + | val fontStrawberry = ResourcesCompat.getFont(context, | ||
| + | 떡볶이체 폰트 불러오기 | ||
| + | val fontTtoppoki = ResourcesCompat.getFont(context, | ||
| + | </ | ||
| + | |||
| + | 여기서 R.font다음에 있는 폰트리소스는 ui.theme폴더에 있는 fontfamily.kt파일에 설정된 것을 그대로 불러올 수 있다. | ||
| + | |||
| + | 이를태면 나는 ui.theme폴더에 다음과 같이 fontfamily.kt를 만들어뒀다. | ||
| + | |||
| + | <file kotlin fontfamily.kt> | ||
| + | val fontGoryeong = FontFamily( | ||
| + | Font(R.font.goryeongstrawberry, | ||
| + | ) | ||
| + | |||
| + | |||
| + | val ttoppokki = FontFamily( | ||
| + | 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.1732809700.txt.gz · 마지막으로 수정됨: 저자 이거니맨
