-1

how can I (read) different types of files in my program (pdf, txt, word or excel) and display them on JEditorPane?

My programme is taking any type of files and open them in himself.

I already tried

public void displaydata(String path){
    File file = new File(path);
    display.setPage(file.toURL());
}

I get correct data only with txt files.

I get strange output with pdf.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Java doesn't display PDF files by default, you'll need to have a look around for some view/component which can – MadProgrammer Dec 27 '15 at 00:18
  • can i open the file normaly in any pdf reader and display results in Jeditorpane?? –  Dec 27 '15 at 00:20
  • You can use `Desktop#open` to open any file in it's associated program (if available), see [How to Integrate with the Desktop Class](https://docs.oracle.com/javase/tutorial/uiswing/misc/desktop.html) for more details. But no, you won't be able to use a `JEditorPane`, it's not designed for show PDF files – MadProgrammer Dec 27 '15 at 00:22
  • if i did that is there any thing i can show data on (like i am making the program a window to see the data i opened with its file?) –  Dec 27 '15 at 00:25
  • 1
    You could also have a look at [this](http://stackoverflow.com/questions/9761727/basic-code-to-display-a-pdf-in-an-existing-jpanel) or [this](http://stackoverflow.com/questions/10620222/how-can-i-open-pdf-file-in-java) or [this](https://www.google.com.au/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#safe=off&q=java+swing+show+pdf) – MadProgrammer Dec 27 '15 at 00:26
  • Open any file formats... You mean you want to build a hex editor/viewer? – MikeCAT Dec 27 '15 at 00:28
  • not exactly i was thinking in opening these files normaly by their viewers but my program view their results(i enter any file path-it opens with its supported program-the results copied in my program) –  Dec 27 '15 at 00:32
  • @KareemElsayed i added a comment to my below answer, added some link to library that allow render a spreadsheet as an image. – Anthony Raymond Dec 28 '15 at 21:13

1 Answers1

0

You are missing a important point.

Every files have a different structure, this is the point of File extention, this is how your OS (more often) recognize the type of file and know how to open it.

You can't just open a file as a text file, and display the content as a PDF Reader or Excel would present it to you, because each software have a behvior to build the view. (This is why you can't open an mp3 file with Microsoft Word).

So, in my opinion, you can't do what you are trying to achieve. You may be able to do this, but you'll need import a reader library for each file type you want to open.

Anthony Raymond
  • 7,434
  • 6
  • 42
  • 59