nueog

[Android Studio] Pdf Viewer 본문

Android Studio

[Android Studio] Pdf Viewer

nueog 2021. 4. 13. 17:32
반응형

온라인 사용설명서 기능을 만들어야 해서 Github에 있는 오픈소스 라이브러리를 가져다가 간단하게 적용시켰다.

사용한 라이브러리는 다음과 같다.

 

github.com/barteksc/AndroidPdfViewer

 

barteksc/AndroidPdfViewer

Android view for displaying PDFs rendered with PdfiumAndroid - barteksc/AndroidPdfViewer

github.com

 

먼저 Module 단의 build.gradle 파일에 implement 시켜준다.

implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'

내 경우에는 모든 메뉴를 fragment로 만들고 있어서 pdf viewer도 fragment로 만들었다. 

public class ManualFragment extends Fragment {

    PDFView pdfView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View v = inflater.inflate(R.layout.activity_manual, container, false);
        pdfView = (PDFView) v.findViewById(R.id.pdfView);

        pdfView.fromAsset("Manual.pdf").load();

        return v;
    }

}

레이아웃은 이렇게 간단하게 구현하였다.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>

 

반응형