How to Open and Red a PDF File in Java


In this example, we will learn about how to open a PDF file in Java.

Source Code

package com.beginner.examples;

import java.io.File;

//Windows solution to view a PDF file
public class OpenPDF {

	public static void main(String[] args) {
		File file = new File("E:tmpTest.pdf");
	  try {

		if (file.exists()) {

			Process process = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler E:tmpTest.pdf");
			process.waitFor();
				
		} else {

			System.out.println("File is not exists");

		}

		System.out.println("Opening...");

  	  } catch (Exception e) {
		e.printStackTrace();
	  }

	}
}

Output:

Opening...

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments