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

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

Create First PDF : Android Studio 안드로이드에서 PDF 생성하기

 

1. AndroidManifest.xml 에 다음과 같이 추가한다. 

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

 

2. activity_main.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" />

 

 

3. 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("안드로이드 PDF 첫번째 문서 만들기 ",30,30,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();

            }
        });
    }
}

4. pdf 생성확인. (Check pdf)

 

 

5. 비디오 보고 따라한다. (Follow the video )

 

 

안드로이드에서 스위치 버튼을 추가하는 방법

 

이 게시물에서는 Android에서 스위치 활성화 및 비활성화 버튼을 구현하는 방법을 보여줍니다.

 

1. Android Studio에서 프로젝트를 열고 build.gradle 파일에 아래의 종속성을 추가 한 다음 프로젝트를 동기화하십시오

implementation 'com.github.zcweng:switch-button:0.0.3@aar'

2. 그런 다음 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">



    <com.suke.widget.SwitchButton
        android:id="@+id/sb"
        android:layout_width="100dp"
        android:layout_height="60dp"
        android:layout_marginLeft="30dp"
        android:layout_margin="10dp"
        android:tint="@color/colorAccent"
        android:layout_centerHorizontal="true"
        android:paddingRight="16dp"
        android:layout_gravity="center"
        app:sb_button_color="@color/white"
        app:sb_shadow_color="#A36F95"
        app:sb_background="#FFF"
        app:sb_checkline_color="@color/green"
        app:sb_checked_color="@color/green"
        app:sb_uncheck_color="@color/red"
        app:sb_uncheckcircle_color="@color/red"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

3. Java 클래스 파일에 아래 코드를 추가하십시오.

package com.jwlee.switch_test;

import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Toast;

import com.suke.widget.SwitchButton;

public class MainActivity extends AppCompatActivity {

    SwitchButton swithButton;

    SharedPreferences.Editor prefEditor;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        swithButton = findViewById(R.id.sb);
        prefEditor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
        prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        swithButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SwitchButton view, boolean isChecked) {

                if (isChecked){

                    // DO what ever you want here when button is enabled
                    Toast.makeText(MainActivity.this, "Enabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","yes");
                    prefEditor.apply();

                }else {

                    // DO what ever you want here when button is disabled
                    Toast.makeText(MainActivity.this, "Disabled", Toast.LENGTH_SHORT).show();
                    prefEditor.putString("checked","false");
                    prefEditor.apply();
                }


            }
        });


        if (prefs.getString("checked","no").equals("yes")){

            swithButton.setChecked(true);

        }else {

            swithButton.setChecked(false);
        }
    }
}

4. color.xml 

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#6200EE</color>
    <color name="colorPrimaryDark">#3700B3</color>
    <color name="colorAccent">#03DAC5</color>
    <color name ="white">#ffffff</color>
    <color name ="red">#ff0000</color>
    <color name ="green">#00ff00</color>
</resources>

5. 더 나은 이해를 위해 아래 비디오 시청

 

+ Recent posts