Show PDF file in GWT app - java

I have a PDF file stored in server and I want to display it on the client side. How can I make the pdf displayed in the required place?

You can create a frame widget with a reference to your PDF file. The Frame widget in GWT will render an HTML iFrame in your browser.
public class FrameExample implements EntryPoint {
public void onModuleLoad() {
// Make a new frame, and point it at Google.
Frame frame = new Frame("http://www.mydomain.com/path to my pdf");
// Add it to the root panel. or anywhere you want
RootPanel.get().add(frame);
}
}

I have used following. Its working fine. May be its usefull for you.
GWT Client side:
HTML pdf = new HTML("<embed src='http://www.mydomain.com/path to my pdf' width='100%' height='500px'></embed>");
PDFPannel.add(pdf);

Related

How to create iframe JAVASCRIPT to JAVA in GWT?

Can anyone help me to javascript into java in gwt, and I need one simple example for this and I am gonna one JavaScript(URL) user form in to Java.
Thanking you
Try with Frame to create iframe in JAVA in GWT.
public class FrameExample implements EntryPoint {
public void onModuleLoad() {
// Make a new frame, and point it at Google.
Frame frame = new Frame("http://www.google.com/");
// Add it to the root panel.
RootPanel.get().add(frame);
}
}
Find more than 30 Code Examples here Java Code Examples for Frame

codenameone setURL with HTML file doesn't work on device

i'm re writing an app with codename one for other devices other android.
On the simulator everything is working fine, iv'e got some buttons that, when pressed, give access to an HTML page, the actual code for this is:
wifi.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ev){
Form form = new Form("WiFi");
Container container = new Container();
WebBrowser wb = new WebBrowser();
container.addComponent(wb);
wb.setURL("jar:///assets/html/wifi.html");
System.out.println("url:"+wb.getURL());
Command backCommand = new Command("Back") {
public void actionPerformed(ActionEvent ev) {
wifi.getComponentForm().showBack();
}};
form.addCommand(backCommand);
form.setBackCommand(backCommand);
form.addComponent(container);
form.show();
}
});
When i first want to getURL() back, the string is empty; then when i want to build for an android device (Galaxy Nexus with Android 4.3) when i tap on the button it returns me nothing but a blank page.
I also tried with an http link like https://www.google.com, on simulator no problem, on device the usual blank page.
Then i tried modifying the position of the html file, putting it in src like for the image file (that works on device), but still nothing.
I've checked the developer guide and every example i could find, and everyone got no problem with this (and on the simulator me neither). Could anyone solve this?
Thank you :)
Place the file in the root of the src directory and point directly at it without the assets hierarchy for it to work properly across platforms.

Get page's html source using a Java applet

I know in scripting languages like Python that this is possible but I know that Java applets can't access other servers other than their own.
I don't know/think I can get this applet signed. Is there a way to use PHP to do what I want to accomplish?
I also know that this code will go to google.com
import java.applet.*;
import java.awt.*;
import java.net.*;
import java.awt.event.*;
public class tesURL extends Applet implements ActionListener{
public void init(){
String link_Text = "google";
Button b = new Button(link_Text);
b.addActionListener(this);
add(b);
}
public void actionPerformed(ActionEvent ae){
//get the button label
Button source = (Button)ae.getSource();
String link = "http://www."+source.getLabel()+".com";
try
{
AppletContext a = getAppletContext();
URL u = new URL(link);
// a.showDocument(u,"_blank");
// _blank to open page in new window
a.showDocument(u,"_self");
}
catch (MalformedURLException e){
System.out.println(e.getMessage());
}
}
}
That is assuming that source.getLabel() is "google"
But how would I get the source html of that page?
The source html is dynamic and is updated every few seconds or miliseconds. But, the html is also updated, so I can still read the dynamic content directly from the html. I already did this in vb.net, but now I need to port it to Java, but I can't figure out how to access a page's html source; that's why I'm asking.
AppletContext.showDocument opens a page in the browser, much like a hyperlink in HTML or a similar call in JavaScript would do. Under the Same Origin Policy you will not have access to this page if it is from a different site, even if the page is in an iframe.
Some sites may have a crossdomain.xml policy file that allows access if you were to read the contents of the java.net.URL directly. However, www.google.com appears to be using a restricted form that I don't believe is currently supported by the Java PlugIn.
Someone will probably suggest signing your applet, which turns off the "sandbox" security feature of Java. You would then need to persuade your users to trust your ability to publish safe signed code.

JavaFX 2 WebView URL Listener

When using a JavaFX (2.2) WebView, is there a way to listen to and handle url's within java code?
For example: I'm loading a local HTML file to my WebView with webEngine.loadContent(html). The HTML contains resources like
<script src="local:my-script.js"></script>
<img src="cms:1234.png"/>
which is also a local file and should be provided from the java application. So I want to register a listener that could handle requests from the page.
Edit: The resource data that is loaded within the HTML page comes from a content-managemant-system so using relative paths is not working.
Create your own URL protocol handler and install it using:
URL.setURLStreamHandlerFactory(new HandlerFactory());
A New Era for Java URL Protocol Handlers article provides detailed instructions for creating the handler.
WebView can make use of your custom protocol to load your content.
The easiest way for you would be substituting local: resource with runtime value in html right before loading it. E.g.:
public class WebViewLocal extends Application {
#Override
public void start(Stage primaryStage) {
String st = "<html><head><title>X</title>"
+ "</head><body><img src='local:1.jpg'/>"
+ "</body></html>";
System.out.println(st);
primaryStage.setScene(new Scene(loadHtml(st), 400, 400));
primaryStage.show();
}
public WebView loadHtml(String html) {
html = html.replace("local:", getClass().getResource(".").toString());
WebView view = new WebView();
view.getEngine().loadContent(html);
return view;
}
public static void main(String[] args) { launch(); }
}
N.B.: This is working sample, just put 1.jpg image at the same place as this file.
If you really want to work with java class from javascript you can use "JavaFX to JavaScript bridge" feature. Take a look at tutorial here: https://blogs.oracle.com/javafx/entry/communicating_between_javascript_and_javafx
If you load your html-file locally, you can simply use relative paths:
This is an example, which load html, with relative scripts.
http://code.google.com/p/jfx-gap/source/browse/src/main/java/com/googlecode/jfxgap/JFXGap.java
The trick schould be the relative path inside the html.

JApplet fails to run in HTML page

I have created a JApplet using the JUNG library in Netbeans that compiles and runs normally. However, when I try to create an html file that runs the applet, only a grey pane appears but the components are missing.
My class is :
public class View extends JApplet {
//Here I declare the buttons etc..
public View()
{
initializeComponent();
fetchGraphs();
}
public static void main(String[] args) throws IOException{
f = new JFrame();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
x = screenSize.width;
y = screenSize.height;
f.getContentPane().add(new View());
f.setTitle("Social Network Privacy Settings and Access Control");
f.setLocation(new Point(15, 20));
f.setSize(new Dimension(x-20,y-50));
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.setResizable(false);
f.setVisible(true);
}
}
The method initializeComponent() adds all the components to the main window. I used JFrameBuilder to build some basic components. JFrameBuilder uses a method addComponent(container, component, x, y, width, height) to add components
I use the code below for that:
contentPane = (JPanel)this.getContentPane();
//to create the japplet contentpane
addComponent(contentPane, genGraphButton, (int)(0.35*x),(int)(0.63*y),
(int)(0.2*x),28);
// to add components
Then I create an html file:
<applet code = 'MyPackage.View'
archive = 'MyProject.jar',
width = 1600,
height = 800/>
in the /dist folder but then only a grey pane appears when I try to open it with Mozilla Firefox. The strange thing is that I have created another simple applet, this time with netbeans JBuilder and it runs normally in a web page.
I really need some help!
You mention the JUNG library, it relies on the two third party libraries, Collections-Generic & Cern Colt Scientific Library 1.2.0. As mentioned by #othman they need to be added to the run-time class-path of the applet (added to the archive attribute of the applet element).
But just so we are clear, make sure the HTML contains more than just the applet element. Something like this:
<html>
<body>
<applet
code='MyPackage.View'
archive='MyProject.jar,jung.jar,collections.jar,colt-scientific.jar'
alt='Java is DISABLED in this browser!'
width='1600'
height='800'>
This browser does not recognize the applet element!
</applet>
</body>
</html>
Of course, you'll need to change the names of the last 3 Jars to their real names.
I'm no Applet expert, since I don't use them, but IIRC you need the init() method to initialize your view. main(...) is not called for an applet.
First, I am not sure that new lines you added into the html are legal. I mean write <applet and /> without any new lines and spaces.
Second, test that your jar is really available. To do this go to the same URL that you go to retrieve your HTML without HTML but with jar, i.e.
if your HTML URL is: http://somehost/my.html type in browser http://somehost/MyProject.jar and see that you can download the jar.
if this works check the code attribute. Is your package name really MyPackage? Capitalized? Do you know it is not according the naming convention?
Also check java console. Find it somewhere in menus of your browser: it depends in browser. I believe that you will see the reason there in form of exception stack trace.
you need to reference also the JUG jars in your applet tag :
<
applet code = 'MyPackage.View'
archive = 'MyProject.jar , jung_xx.jar',
width = 1600,
height = 800 /
>
in the archive attribute add all jung jars that you have currently in your netbeans project classpath.

Categories