이 예제는 안드로이드 WebView에서 PDF 문서를 표시하는 방법을 단계별로 보여줍니다. WebView 자체에는 PDF 렌더링 기능이 없기 때문에, 구글 문서 뷰어(Google Docs Viewer)를 임베드하는 방식으로 PDF를 화면에 출력하게 됩니다.
1단계 – 새 프로젝트 만들기
Android Studio에서 File ⇒ New Project로 이동해 새 프로젝트를 생성하고, 필요한 모든 정보를 입력합니다.
2단계 – 레이아웃 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 버튼 하나만 배치하고, 클릭 시 loadPage() 메서드가 호출되도록 연결합니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="16dp" tools:context=".MainActivity"> <Button android:onClick="loadPage" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Load web Page" /> </RelativeLayout>
3단계 – MainActivity 코드 작성
src/MainActivity에 아래 코드를 추가합니다.
package app.tutorialspoint.com.sample;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.webkit.WebView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadPage(View view) {
WebView webview = new WebView(this);
webview.getSettings().setJavaScriptEnabled(true);
setContentView(webview);
String pdf =
"https://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf";
webview.loadUrl("https://drive.google.com/viewerng/viewer?embedded=true&url=" + pdf);
}
}여기서 핵심은 loadPage() 메서드입니다. 동적으로 WebView 객체를 생성한 뒤 setJavaScriptEnabled(true)로 자바스크립트를 활성화하고, PDF 주소를 url 파라미터로 넘겨 구글 뷰어 페이지를 로드합니다. 이렇게 하면 별도의 PDF 리더 앱 없이 앱 화면 안에서 문서를 바로 볼 수 있습니다.
4단계 – 매니페스트 설정
androidManifest.xml에 웹 콘텐츠를 불러오기 위한 인터넷 권한을 반드시 추가합니다. 누락되면 WebView가 아무것도 표시하지 못하므로 주의하세요.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.tutorialspoint.com.sample"> <uses-permission android:name="android.permission.INTERNET"/> <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>
앱 실행하기
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 모바일 기기를 컴퓨터에 연결했다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택하면 됩니다. 그러면 연결된 기기에 앱이 설치·실행됩니다.

추가 팁 및 참고 사항
- 구글 문서 뷰어 방식은 온라인 상태에서만 동작하며, 로컬 저장소의 PDF 파일은 직접 표시할 수 없습니다.
- 최근에는
https://docs.google.com/viewer?url=PDF주소&embedded=true형태의 URL이 더 안정적으로 사용됩니다. - 오프라인 지원이나 페이지 이동, 확대·축소 같은 고급 기능이 필요하다면 API 21 이상에서 제공하는
PdfRenderer클래스나 AndroidPdfViewer 같은 외부 라이브러리를 사용하는 것이 좋습니다.