Create PDF Multiple Pages : Android Studio
안드로이드 스튜디오 PDF 여러페이지 만드는 방법
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jwlee.pdf_example2">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Create PDF"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.jwlee.pdf_example2;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
ActivityCompat.requestPermissions(this,new String[] {
Manifest.permission.WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
createPDF();
}
private void createPDF() {
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PdfDocument pdfDocument = new PdfDocument();
Paint paint = new Paint();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(250,400,1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
Canvas canvas = page.getCanvas();
canvas.drawText("Page 1 ",30,30,paint);
pdfDocument.finishPage(page);
PdfDocument.PageInfo pageInfo2 = new PdfDocument.PageInfo.Builder(250,400,1).create();
PdfDocument.Page page2 = pdfDocument.startPage(pageInfo2);
Canvas canvas2 = page2.getCanvas();
canvas2.drawText("Page 2 ",30,30,paint);
pdfDocument.finishPage(page2);
File file = new File(Environment.getExternalStorageDirectory(),"/myPDF.pdf");
try {
pdfDocument.writeTo(new FileOutputStream(file));
}catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
}
});
}
}
2020/03/18 - [Android Studio] - Create PDF Multiple Pages : Android Studio
'기타 ETC > Android Studio' 카테고리의 다른 글
EditText에 숫자만 입력받기 (0) | 2020.03.19 |
---|---|
Android Studio PDF Document(Add Image in PDF) (0) | 2020.03.18 |
Create First PDF : Android Studio 안드로이드에서 PDF 생성하기 (0) | 2020.03.18 |
안드로이드에서 스위치 버튼을 추가하는 방법 (1) | 2020.03.17 |
Android Studio를 사용하여 애니메이션 된 스플래시 화면을 Android 앱에 추가하는 방법 (2) | 2020.03.17 |