Site iconJava PDF Blog

Accessing files from inside NetBeans

At IDR Solutions we are constantly working on adding new functionality and improving our applications. In my previous article “How to write JPEG images in Java” I explained how to use ImageIO, JAI and JDeli to write JPEG images in Java.

Our PDF viewer allows configuration data to be stored in an XML file inside NetBeans jar. So in this article I am going to show you how to access a file (which can be any binary or text file) from inside your code. So here is how we did it:

Create XML file

Creating XML file in NetBeans is very easy. Just right click on your project -> select New -> and XML Document option. Notice the other options as well.

If XML Document isn’t displayed select Other -> than select XML option from the left window -> and XML Document option from the right window.

Next chose the name for your file -> select the Folder you want to place your XML Document in -> and click Next

Chose default and click Next.

Now you are in position of adding your code to the XML file.

Adding the Action to read a File

In our action to show the Oracle guide we are reading the file path of the XML file as an InputStream that we are passing as a parameter to the TopComponent. Note the input stream is a location in our jar.

@Override
public void actionPerformed(ActionEvent e) {

TopComponent tc;
//this gives me a new window each time

InputStream inputFile = this.getClass().getResourceAsStream("/org/jpedal/netbeans/oracle.xml");

tc = new PDFDisplayTopComponent("http://docs.oracle.com/cd/E50453_01/doc.80/e50452.pdf", inputFile, PDFViewerTypes.INTERNAL_OPENVIWERFX);

tc.open();
tc.requestActive();

}

Using in The TopComponent

In the TopComponent we are assigning the value to the properitiesFile

private InputStream propertiesFile=null;

Than we creating PDFDisplayTopComponent method where we assigning 3 parameters.

public PDFDisplayTopComponent(String file, InputStream properties, PDFViewerTypes viewerType){

this(viewerType);

PDFfile = file;

this.propertiesFile=properties;

this.setDisplayName(file);

}

Next we are adding the functionality to load the propertiesFile. You could add your own code here:

//Root pane which holds JavaFX PDF Viewer
Pane viewerPane = new Pane();

fullViewer = new OpenViewerFX(viewerPane, null);
if(propertiesFile!=null){
fullViewer.loadProperties(propertiesFile);
}
fullViewer.setupViewer();
Scene scene = new Scene(viewerPane);

External access of a XML file

The XML file could be also accessed externally. All what you have to do is to sent the path to the XML file as a String parameter in the action functionality:

tc = new PDFDisplayTopComponent("http://docs.oracle.com/cd/E50453_01/doc.80/e50452.pdf", "C:\\Users\\Sylwia\\Desktop\\oracle.xml",PDFViewerTypes.INTERNAL_OPENVIWERFX);

Than in TopComponent assign String value to the properitiesFile

private String propertiesFile=null;

And pass String parameter to the PDFDisplayTopComponent metchod

public PDFDisplayTopComponent(String file, String properties, PDFViewerTypes viewerType){

this(viewerType);

PDFfile = file;

this.propertiesFile=properties;

this.setDisplayName(file);

}

Hopefully you have found this quick guide useful.