Android Studio PDF Document(Add Image in PDF)
안드로이드 PDF만들고 그 안에 그림파일 삽입하기
1. AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2. drawable에 earth.png 그림파일 아무거나 붙여넣기 한다. Image File Ctrl+C,V
3. activity_mail.xml 버튼을 한개 만든다.
<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" />
4. 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.Bitmap;
import android.graphics.BitmapFactory;
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;
Bitmap bitmap, scaleBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.earth);
scaleBitmap = Bitmap.createScaledBitmap(bitmap, 150, 150,false);
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.drawBitmap(scaleBitmap,30,50,paint);
pdfDocument.finishPage(page);
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 First PDF : Android Studio 안드로이드에서 PDF 생성하기
2020/03/18 - [Android Studio] - Create PDF Multiple Pages : Android Studio
'기타 ETC > Android Studio' 카테고리의 다른 글
안드로이드 영수증 PDF 만들기 (0) | 2020.03.19 |
---|---|
EditText에 숫자만 입력받기 (0) | 2020.03.19 |
Create PDF Multiple Pages : Android Studio (0) | 2020.03.18 |
Create First PDF : Android Studio 안드로이드에서 PDF 생성하기 (0) | 2020.03.18 |
안드로이드에서 스위치 버튼을 추가하는 방법 (1) | 2020.03.17 |