Re: pdf in java Viewing... Not a good experience



On Thu, 9 Jun 2005 22:53:33 -0500, gbruno wrote
(in article <1118375613.406186.201980@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>):

> I want to display pdf files from within java
>
> 1) I can use getruntime().exec
> BUT I need to know the external pdf viewers name
>
> 2) I can use the java viewer from Adobe 1998
> This crashed wanting com.apple.mrj
> So I paid $26 to Limeware to get the MRJ stubs
> And I get an internal viewer
> BUT it shows an initial Adobe licence screen every time,
> its very slow
> it crashed one time with java heap error
> everytime it closes it tries to write a profile,
> fails and displays an error
> NOT a pleasant experience.
> (if you do want those MRJ stubs, email me, I will send them to you
> dont pay $26 to Limeware)
>
> If I choose option 1, On Windows I must delve into the registry to find
> the default pdf viewer [this seems retrograde, not truly
> WriteOnceRunAnyware]
>
> If Linux How do I find the default pdf handler from within Java?
>
> I personally believe its a waste of time. Better to let the user click
> on pdf files in an external fileviewer.
> But my manager wants my program to show pdfs, and he pays my wage.
>
> Is there an easy way?
> cheers
> gbruno
>

You are going about it the hard way. Look at:

< http://www.javaworld.com/javaworld/javatips/jw-javatip66.html>

This code is austensibly to open a url in a browser, but on Windows it will
open any file type using the application registered for that file type
(including PDF!!).

I have augmented the code to work on Mac OS X. Here is the final version:


import java.io.*;
import java.net.*;


/**
* A simple, static class to display a URL in the system browser.
*
*
*
* Under Unix, the system browser is hard-coded to be 'netscape'.
* Netscape must be in your PATH for this to work.  This has been
* tested with the following platforms: AIX, HP-UX and Solaris.
*
*
*
* Under Windows, this will bring up the default browser under windows,
* usually either Netscape or Microsoft IE.  The default browser is
* determined by the OS.  This has been tested under Windows 95/98/NT.
*
*
*
* Examples:
*
*
* BrowserControl.displayURL("http://www.javaworld.com";)
*
* BrowserControl.displayURL("file://c:\\docs\\index.html")
*
* BrowserContorl.displayURL("file:///user/joe/index.html");
*
*
* Note - you must include the url type -- either "http://"; or "file://".
*
* Taken from JavaWorld Tip #66 by Steven Spencer
*
*/


public class BrowserControl
{
// Used to identify the windows platform.
private static final String WIN_ID = "Windows";
// Used to identify the mac platform.
private static final String MAC_ID = "Mac";
// The default system browser under windows.
private static final String WIN_PATH = "rundll32";
// The flag to display a url.
private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
// The default browser under unix.
private static final String UNIX_PATH = "netscape";
// The flag to display a url.
private static final String UNIX_FLAG = "-remote openURL";


/**
* Display a file in the system browser.  If you want to display a
* file, you must include the absolute path name.
*
* @param url the file's url (the url must start with either "http://"; or
* "file://").
*/
public static void displayURL(String url)
{
boolean windows = isWindowsPlatform();
boolean mac = isMacPlatform();
String cmd = null;
// Different OS's and Java versions pass different things as the
// file: url. We need to get it in the form of file:/// for it to
// launch correctly on all platforms.
if (url.indexOf("file:///") == 0)
{
//everything is fine
}
else if (url.indexOf("file://") == 0)
url = Utility.replaceAll("file://", "file:///", url);
else if (url.indexOf("file:/") == 0)
url = Utility.replaceAll("file:/", "file:///", url);
else if (url.indexOf("file:") == 0)
url = Utility.replaceAll("file:", "file:///", url);

if (isMacPlatform())
{
url = Utility.replaceAll(" ", "%20", url);
}
if (url.startsWith("file:"))
url = Utility.replaceAll("?", "%3F", url);
//System.out.println("displaying: " + url);
try
{
if (windows)
{
// cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
Process p = Runtime.getRuntime().exec(cmd);
}
else if (mac)
{
Runtime.getRuntime().exec(new String[] {"open", url });
}
else
{
String browser = System.getProperty("BrowserCmd");
if (browser != null)
{
Runtime.getRuntime().exec(new String[] {(String) browser,
url });
}
else
{
// Under Unix, Netscape has to be running for the
"-remote"
// command to work.  So, we try sending the command and
// check for an exit value.  If the exit command is 0,
// it worked, otherwise we need to start the browser.
// cmd = 'netscape -remote
openURL(http://www.javaworld.com)'
cmd = UNIX_PATH + " " + UNIX_FLAG + "(" + url + ")";
Process p = Runtime.getRuntime().exec(cmd);
try
{
// wait for exit code -- if it's 0, command worked,
// otherwise we need to start the browser up.
int exitCode = p.waitFor();
if (exitCode != 0)
{
// Command failed, start up the browser
// cmd = 'netscape http://www.javaworld.com'
cmd = UNIX_PATH + " " + url;
p = Runtime.getRuntime().exec(cmd);
}
}
catch(InterruptedException e)
{
System.err.println("Error bringing up browser, cmd='"
+
cmd + "'");
System.err.println("Caught: " + e);
}
}
}
}
catch (IOException e)
{
// couldn't exec browser
System.err.println("Could not invoke browser, command=" + cmd);
System.err.println("Caught: " + e);
}
}


/**
* Try to determine whether this application is running under Windows
* or some other platform by examing the "os.name" property.
*
* @return true if this application is running under a Windows OS
*/
public static boolean isWindowsPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(WIN_ID))
return true;
else
return false;
}


public static boolean isMacPlatform()
{
String os = System.getProperty("os.name");
if ( os != null && os.startsWith(MAC_ID))
return true;
else
return false;
}


/**
* Simple example.
*/
public static void main(String[] args)
{
displayURL("http://www.javaworld.com";);
}

}




--
Bill Tschumy
Otherwise -- Austin, TX
http://www.otherwise.com

.



Relevant Pages

  • Re: EU nations refuse to force members to farm GM maize
    ... the Spiegel logo is on the Business Week page. ... Since there are security flaws in PDF files, allowing them to be opened to ... they don't display consistently across browsers. ... doesn't have a plug-in to show the PDFs in the browser but that's just ...
    (misc.rural)
  • Display / Download file
    ... use a showDoc.php file to display the file. ... want this to be in the browser. ... xls - when the link is clicked, I get an excel warning that the file ... pdf, dwf, dwg and dxf all do what I want. ...
    (alt.php)
  • Java Applets & Internet Explorer 6
    ... Some graphics on internet pages will not display using Windows XP and IE 6.0. ... I get an error message saying that my browser is not set up to deal with Java Applets. ...
    (microsoft.public.windowsxp.help_and_support)
  • Re: File Open and Save dialog poping up while opening PDF
    ... > I am trying to display a PDF file (which I am being passed from a web ... > want the PDF to be displayed. ... to tell the browser to show the document inline. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Attachment Icons not displaying
    ... Does anybody know why a pdf attachment would not display the pdf icon ... When it sends through my windows ...
    (microsoft.public.inetserver.iis.smtp_nntp)