Results 1 to 6 of 6
  1. #1
    aparna is offline Member
    Join Date
    Feb 2008
    Posts
    12
    Rep Power
    0

    Red face open a pdf file in linux

    Hi... I want to open a pdf file in linux with java code
    In windows i am using
    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + path);

    In Mac
    Runtime.getRuntime().exec("open /Users/source/abc.pdf");

    but I didnt find any solution for Linux
    Somewhere I found solution
    Runtime.getRuntime().exec("open file:/home/source/abc.pdf");
    But its not working and this solution is specific to KDE desktop. I want a common solution for any desktop in linux. Please help me
    Thanks

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    PDF is proprietary, probably study Post Script, which is popular in the college cs crowd and leads to pdf eventually
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    Did you try to find the common execution location of pdf? Because that's not related to Java anymore. May be our Linux users can help you. At the time try to search on a Linux forum as well. Sorry pal, I'm not a linux user. :)

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    PDF is proprietary
    This was true, but Adobe has released the specs. So its sort of open license, and there are open source tools to read it.

    But, and this is a big but, the format is complex. Most programmers prefer to not reinvent the wheel. So find some code that already does what you need.

  5. #5
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Download library from here :
    New: 100% Java PDF Renderer and Viewer
    then create two file:
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    
    import java.io.*;
    
    import java.nio.*;
    import java.nio.channels.*;
    
    import javax.swing.*;
    
    import com.sun.pdfview.*;
    public class Renderer {
    	
    	
    	private Image image;
    	private String fileName;
    	private PDFFile pdfFile;
    	private double height;
    	private double width;
    	private int numpages;
    	private Image[] images;
    	
    	public Renderer(String file,int pagenum){
    		fileName=file;
    		
    		try{
    			RandomAccessFile raf = new RandomAccessFile (new File (fileName), "r");
    			FileChannel fc = raf.getChannel ();
    			ByteBuffer buf = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size ());
    			pdfFile = new PDFFile (buf);
    		}
    		catch(IOException e){
    			System.out.println("the file wasn't found");
    		}
    		numpages = pdfFile.getNumPages ();
    		System.out.println ("Number of pages = "+numpages);
    		/*if (pagenum > numpages)
    			pagenum = numpages;
    */
    		images=new Image[numpages];
    		int counter=1;
    		for(int i=0;i<images.length;i++){
    			PDFPage page = pdfFile.getPage (counter);
                 
    			Rectangle2D r2d = page.getBBox ();
    
    			width = r2d.getWidth ();
    			height = r2d.getHeight ();
    			width /= 72.0;
    			height /= 72.0;
    			int res = Toolkit.getDefaultToolkit ().getScreenResolution ();
    			width *= res;
    			height *= res;
    
    			image = page.getImage ((int) width, (int) height, r2d, null, true, true);
    			images[i]=image;
    			counter++;
    			
    		}
    		
    	}
    	
    	
    	public Image[] getImagePDFArray(){
    		return this.images;
    	}
    	
    	public Image getPDFImage(){
    		return this.image;
    	}
    	
    	public double getWidth(){
    		return this.width;
    	}
    	
    	
    	
    	public int getNumOfPages(){
    		return this.numpages;
    	}
    	
    	public int getHeight(){
    		return (int)this.height;
    	}
    }
    the seocnd one:
    Java Code:
    import java.awt.*;
    import java.awt.geom.*;
    
    import java.io.*;
    
    import java.nio.*;
    import java.nio.channels.*;
    
    import javax.swing.*;
    
    import com.sun.pdfview.*;
    import java.awt.event.*;
    public class PDFViewer extends JFrame
    {
      // static Image image;
       
       public PDFViewer (String title)
       {
          super (title);
          setDefaultCloseOperation (EXIT_ON_CLOSE);
          pack ();
          
       }
       
       
       
    
       public static void main (final String [] args) throws IOException
       {
    
    	  int pagenum=1;
          
         
          
         final Renderer rend=new Renderer(fileName,pagenum);
        
          Runnable r = new Runnable ()
                       {
        	  			int y=0;
        	  			JViewport view;
                           public void run ()
                           {
                            PDFViewer pdf=  new PDFViewer ("PDF Viewer: "+"<Your pdf file>");
                           
                        	JToolBar toolBar=new JToolBar();
                  	      	
                        	JButton buttonScroll=new JButton("Next");
                    	    toolBar.add(buttonScroll);
                    	    toolBar.setVisible(true); 
                            JLabel[] labels=new JLabel[rend.getNumOfPages()];
                            JPanel main=new JPanel();
                            main.setLayout(new BoxLayout(main,BoxLayout.Y_AXIS));
                            for(int i=0;i<labels.length;i++){
                            	labels[i]=new JLabel(new ImageIcon (rend.getImagePDFArray()[i]));
                            	labels[i].setVerticalAlignment (JLabel.BOTTOM);
                            	main.add(labels[i]);
                            }
                            JScrollPane pane=new JScrollPane (main);
                            JSplitPane splitPane=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,toolBar,pane);
                    		splitPane.setDividerSize(4);
                    		view = pane.getViewport();
    
                    		Point p = new Point(0,y);
                    		view.setViewPosition(p);
                    		y=rend.getHeight();
                    		
                    		
                    		buttonScroll.addActionListener(new ActionListener(){
                    			public void actionPerformed(ActionEvent e){
                    				int r=0;
                    				double rp=0.0;
                    				rp=view.getViewPosition().getY();
                    				r=(int)rp/rend.getHeight();
                    				y=rend.getHeight()*r+rend.getHeight();
                    				Point p = new Point(0,y);
                            		view.setViewPosition(p);
                    			}
                    		});
                    		
                            pdf.setContentPane (splitPane);
                            pdf.setSize(800,600);
                  	      
                  	      
                            pdf.setVisible (true);
                           }
                       };
          EventQueue.invokeLater (r);
       }
    }
    Search where you have PDFViewer pdf= new PDFViewer ("PDF Viewer: "+"<Your pdf file>"); and insert your own file pdf to open

  6. #6
    Join Date
    Jun 2010
    Posts
    2
    Rep Power
    0

    Default open a file in linux using java

    Hi,

    using linux for Gnome Desktop, u can open a file in java as follows:

    String[] cmd = {"gnome-open", filename};
    process = Runtime.getRuntime().exec(cmd);

Similar Threads

  1. Replies: 6
    Last Post: 04-08-2009, 05:05 AM
  2. how to open a file through URL
    By katie in forum New To Java
    Replies: 3
    Last Post: 07-13-2008, 03:53 AM
  3. new member need help with linux
    By praveena in forum New To Java
    Replies: 0
    Last Post: 01-22-2008, 11:22 AM
  4. Is it possible to open a txt file?
    By jason27131 in forum New To Java
    Replies: 20
    Last Post: 08-07-2007, 02:24 AM
  5. Linux and java
    By Alan in forum Advanced Java
    Replies: 2
    Last Post: 05-31-2007, 03:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •