Using ICE PDF viewer pdf is not changed inside a jinternal frame - java

Am using icepdf to show pdf on my swing application. I have external buttons for pdf navigation. I need to navigate so many pdfs, but inside the same panel without closing.
My code for Icepdf:
public static void pdfviewerICE(currentpdfurl) {
filePath = currentpdfurl;
// build a controller
SwingController controller = new SwingController();
// Build a SwingViewFactory configured with the controller
SwingViewBuilder factory = new SwingViewBuilder(controller);
PropertiesManager properties = new PropertiesManager(
System.getProperties(),
ResourceBundle.getBundle(PropertiesManager.DEFAULT_MESSAGE_BUNDLE));
properties.set(PropertiesManager.PROPERTY_DEFAULT_ZOOM_LEVEL, "1.75");
JSplitPane jSplitPane_PDF = factory.buildUtilityAndDocumentSplitPane(true);
controller.openDocument(filePath);
if ((internalFrame.getComponents()!= null) || (internalFrame.isClosed())) {
internalFrame.add(jSplitPane_PDF);
internalFrame.show();
}
}
This is loading same pdf everytime.

Prasath Bala,
you have to make "controller" as public. Then call your controller when you pdf page is changed
Code:
controller.openDocument(currentpdfurl);
I too had the same issue, this will solve your query for sure.

Related

Java GUI save a pdf (PdfAcroForm) with JFileChooser

I did a lot of researches on this site but i didn't find anything connected to my situation, so I'm kindly looking for an advice in order to reach the following result:
I'm working on a Java GUI and I developed an application to create an invoice.
On my project's folder i uploaded a blank pdf (that includes forms named: "nr_invoice", "client", "service", "price", "quantity", "tax" and "tot"), this pdf is named "invoice.pdf". Through the following code, by clicking a button on the GUI, the datas in the GUI are reported into the selected pdf's fields and the pdf is saved into the folder "src/pdf" with the name "new_invoice.pdf".
The code works, but what I'd like to do is to show a save file dialog (using JFileChooser) when the user click on the button "btnSave". So, by clicking on this button, the user is able to choose the save path and also the name of the file (but will be possible to save only a .pdf format).
Thank you very much for your help!
public class PdfGenerator extends JFrame implements ActionListener {
public static final String SRC = "src/pdf/invoice.pdf";
public static final String DEST = "src/pdf/new_invoice.pdf";
PdfDocument pdf = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
btnSave.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PdfAcroForm form = PdfAcroForm.getAcroForm(pdf, true);
Map<String, PdfFormField> fields = form.getFormFields();
fields.get("nr_invoice").setValue(invoice.getText());
fields.get("client").setValue(cl_name.getText());
fields.get("service").setValue(description.getText());
fields.get("price").setValue(amount.getText());
fields.get("quantity").setValue(quantity.getText());
fields.get("tax").setValue(tax_amount.getText());
fields.get("tot").setValue(total_price.getText());
form.flattenFields();
pdf.close();
}
});
}

Why should I use Platform.runlater on javafx main thread in this code?

I'm making java application with javaFx ui in kind of tiny machine. I wanted to show loading page, and load data files during the progress indicator going on.
I know it could work well if I added Platform.runlater on the code loading fxml file and controller, but it's weird for me to use Platform.runlater on javaFx main app thread. I checked threads' name but they were same. Also it works if it run separately using annotation.
Why do i need to use Platform.runlater ?
If I don't add that, loading image turns white screen and skip image, and just show menu view.
//Process
//1. Set loading page image
//2. Load data files
//3. Load next page(menu)
public void loadHomeMenuPage() {
setLoadingImage();
execLoadingData();
execLoadingView(this);
}
private void setLoadingImage() {
System.out.println("Load -> " + Thread.currentThread());
File file = new File("Resources/images/load.png");
InputStream is;
try
{
is = new FileInputStream(file);
this.logoImageView.setImage(new Image(is));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
private void execLoadingData() {
// load openCv files
new LoadOpenCV();
// load protocol files
new ProtocolLoader().load();
// load language pack here
}
private void execLoadingView(IController loadController) {
//Load homeMenu after loading all data
//Platform.runLater(() -> {
System.out.println(Thread.currentThread());
IController controller = (IController) FxmlUtils.LOAD
.fxmlPath(PathFxml.ABS_PATH_HOME_MENU_VIEW)
.pane("BorderPane")
.set2BaseBorderPane(this.baseBorderPane, "center")
.exec();
controller.setBaseBorderPane(this.baseBorderPane);
//});
}

Opening a PDF in a javafx aplication

I'm developing a javafx application that opens a PDF when I pres a button. I'm using the xdg-open command in linux like this:
String[] command = {"xdg-open",path}
Process p = Runtime.getRuntime().exec(command);
p.waitFor();
but when i pres the button nothing happens.
I tested it in a different project and it opened the PDF without problem.
Any idea how can i fix this?
Here's the method that I use. A simple call to the Desktop.getDesktop().open() method will open any given File using the system's default application.
This will also open the file in a background Thread so your application doesn't hang while waiting for the file to load.
public static void openFile(File file) throws Exception {
if (Desktop.isDesktopSupported()) {
new Thread(() -> {
try {
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
}).start();
}
}
This code show the document in the default browser :
File file = new File("C:/filePath/Test.pdf");
HostServices hostServices = getHostServices();
hostServices.showDocument(file.getAbsolutePath());
I hope this help!!
I have used ICEpdf's org.icepdf.core.pobjects.Document to render the pages of my PDF's; as described here. This gives a ava.awt.image.BufferedImageper page. I convert this to a JavaFX node:
Image fxImage = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView imageView = new ImageView(fxImage);
From there you can write your own simple paging viewer in JavaFX. The rendering is fast and the result looks as hoped for.

Can anyone help me out with the errors in this code for displaying PDF in Java swing?

This code is to view a PDF in swing, but as I open any PDF the content is not visible and blank pages appear. I am using icepdf core and viewer jar files.
public class PDFView{
public static void main(String[]args) {
SwingController controller = new SwingController();
SwingViewBuilder factory = new SwingViewBuilder(controller);
controller.setIsEmbeddedComponent(true);
DocumentViewController viewController = controller.getDocumentViewController();
JPanel viewerComponentPanel = factory.buildViewerPanel();
ComponentKeyBinding.install(controller, viewerComponentPanel);
controller.getDocumentViewController().setAnnotationCallback(
new org.icepdf.ri.common.MyAnnotationCallback(
controller.getDocumentViewController()));
JFrame applicationFrame = new JFrame();
applicationFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
applicationFrame.getContentPane().setLayout(new BorderLayout());
applicationFrame.getContentPane().add(viewerComponentPanel,BorderLayout.CENTER);
applicationFrame.getContentPane().add(factory.buildCompleteMenuBar(),BorderLayout.NORTH);
controller.setPageViewMode(DocumentViewControllerImpl.ONE_PAGE_VIEW, false);
applicationFrame.pack();
applicationFrame.setVisible(true);
}}
Find the screenshots here..
Errors and warnings in the code
[]
Output-view of PDF
java.lang.noClassDefFoundError : com.sun.image.codec.jpeg.JPEGImageDecoder
Its seems you need JPEGImageDecoder for it
http://www.java2s.com/Code/Jar/s/Downloadsunjaicodecjar.htm
Download the JAR and add it to your build path libraries

How to add Google map for GWT into FormPanel

I am new to GWT and face some problem here. I need to show up Google map on a form in my GWT web.
First, there has a windowForm.class which extends FormPanel and I have wrote a mapWindowForm.class which extends this windowForm.class as below.
http://paste.ideaslabs.com/show/Q0ThysUrSF
And the problem is how shall I add this map "final MapWidget map = new MapWidget(Location, 2);" which is a MapWidget into this FormPanel: "mapWindowForm.class"
This system was developed by GWT-2.5.0. The library I have imported is gwt-maps-1.1.1.jar(Maps v2 API 1.1.1), so I give "2" as the second parameter in Maps.loadMapsApi(). Unfortunately, the first time to call mapWindowForm.class, the window.alert showed up, indicated that the Map.loadMapsApi() has not successed. But the second time to call this class, the window.alert didn't show up until I have refresh the web page. When I tried to give "3", it shows the sensor shall be given as true of false. When I tried to use gwt-maps-3.8.1(Maps v3 API), the import were not work.
I have tried some way to add this Map into frompanel as below
add(map);
LayoutContainer lc= new Layoutcontainer();
lc.add(map);
add(lc);
But both doesn't work, it just show a FormPanel with nothing on it.
I am not sure that is the MapWidget doesn't create or the MapWidget doesn't add into the FormPanel succeed
Thanks.
Hi Braj,
Thank you very much for your reply.
I have tried your suggestion in my code, but still face some problem.
mapWindowForm form = new mapWindowForm();
GoogleMap gmap = googleMap.create(form.getElement(), options);
But, I still don't how to add the gMap into my mapWindowForm.
Others form in this project would be code as below:(BaseWindow.java extends Window)
BaseWindow window = new BaseWindow(new mapWindowForm());
window.show();
And I have found this issue Example "Google Maps API v3 for GWT" Project for Eclipse.
As this issue claimed, I need to add <inherits name="com.google.maps.gwt.GoogleMaps" /> in my project.gwt.xml and a script to load map api. But some one also said cannot add script in gwt.xml.
So, I add inherits into gwt.xml and script into my project.html.
But I am bit of confuse, according to https://code.google.com/p/gwt-google-apis/wiki/MapsGettingStarted , it was import com.google.gwt.maps, but in this issue, it import com.google.maps.gwt, what's the different between them?
Thanks again.
I have tried below code(BaseWindow.java extends Window, windowForm.java extends FormPanel)
onClick(){
// MapOptions options = MapOptions.create() ;
// options.setCenter(LatLng.create( latCenter, lngCenter ));
// options.setZoom( 6 ) ;
// options.setMapTypeId( MapTypeId.ROADMAP );
// options.setDraggable(true);
// options.setMapTypeControl(true);
// options.setScaleControl(true) ;
// options.setScrollwheel(true) ;
windowForm panel = new mapWindowForm();
// GoogleMap gMap = GoogleMap.create( panel.getElement(), options ) ;
//BaseWindow(FormPanel formpanel, String ID, String title, int Height, int Weight)
BaseWindow window = new BaseWindow(panel,"Maps","This is Maps", 500, 650);
window.show();
}
This will show a form with title "This is Maps" and nothing in it.
But, when I unmark the toggle, the form will not be showed. Click the button will nothing happen. I am wonder is MapOptions doesn't work(API not load?) correct or some error because GWT container?
After created a new Google web application project and tried as below code:
public class Map implements EntryPoint {
public void onModuleLoad() {
FormPanel panel = new FormPanel();
panel.setWidth("100%");
panel.setHeight("100%");
MapOptions options = MapOptions.create();
options.setCenter(LatLng.create(23,-151));
options.setZoom(2);
options.setMapTypeId(MapTypeId.ROADMAP);
options.setDraggable(true);
options.setMapTypeControl(true);
options.setScaleControl(true);
options.setScrollwheel(true);
Button btn = new Button();
GoogleMap gMap = GoogleMap.create(panel.getElement(), options);
RootPanel.get().add(panel);
RootPanel.get().add(btn);
gMap.addIdleListener(new GoogleMap.IdleHandler() {
public void handle() {
Window.alert("Idle");
}
});
}
}
Also add gwt-map.3.8.1.jar into project and add configuration as below into Map.gwt.xml
<inherits name="com.google.maps.gwt.GoogleMaps" />
<script src="http://maps.google.com/maps/api/js?sensor=false" />
Compile success while with network connection.
But when run as web application, the url http://127.0.0.1:8888/Map.html?gwt.codesvr=127.0.0.1:9997 doesn't show up Google Map.
So, I have added a button, it has been showed. And add a idleListener, the Window.alert showed up, indicated that the map doesn't load.
On the other hand, in my project, add into gwt.xml still compile failed, because it doesn't support script tag, even with network connection. So, I have added script into home_page.html, but still failed to load the map. At the meantime, Window.alert also show up.
Connect to http://maps.google.com/maps/api/js?sensor=false in browser will get information as below
window.google = window.google || {};
google.maps = google.maps || {};
(function() {
function getScript(src) {
document.write('<' + 'script src="' + src + '"' +
' type="text/javascript"><' + '/script>');
}
var modules = google.maps.modules = {};
google.maps.__gjsload__ = function(name, text) {
modules[name] = text;
};
google.maps.Load = function(apiLoad) {
delete google.maps.Load;
apiLoad([0.009999999776482582,[[["http://mt0.googleapis.com/vt?lyrs=m#262000000\u0026src=api\u0026hl=zh-TW\u0026","http://mt1.googleapis.com/vt?lyrs=m#262000000\u0026src=api\u0026hl=zh-TW\u0026"],null,null,null,null,"m#262000000",["https://mts0.google.com/vt?lyrs=m#262000000\u0026src=api\u0026hl=zh-TW\u0026","https://mts1.google.com/vt?lyrs=m#262000000\u0026src=api\u0026hl=zh-TW\u0026"]],[["http://khm0.googleapis.com/kh?v=149\u0026hl=zh-TW\u0026","http://khm1.googleapis.com/kh?v=149\u0026hl=zh-TW\u0026"],null,null,null,1,"149",["https://khms0.google.com/kh?v=149\u0026hl=zh-TW\u0026","https://khms1.google.com/kh?v=149\u0026hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/vt?lyrs=h#262000000\u0026src=api\u0026hl=zh-TW\u0026","http://mt1.googleapis.com/vt?lyrs=h#262000000\u0026src=api\u0026hl=zh-TW\u0026"],null,null,null,null,"h#262000000",["https://mts0.google.com/vt?lyrs=h#262000000\u0026src=api\u0026hl=zh-TW\u0026","https://mts1.google.com/vt?lyrs=h#262000000\u0026src=api\u0026hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/vt?lyrs=t#132,r#262000000\u0026src=api\u0026hl=zh-TW\u0026","http://mt1.googleapis.com/vt?lyrs=t#132,r#262000000\u0026src=api\u0026hl=zh-TW\u0026"],null,null,null,null,"t#132,r#262000000",["https://mts0.google.com/vt?lyrs=t#132,r#262000000\u0026src=api\u0026hl=zh-TW\u0026","https://mts1.google.com/vt?lyrs=t#132,r#262000000\u0026src=api\u0026hl=zh-TW\u0026"]],null,null,[["http://cbk0.googleapis.com/cbk?","http://cbk1.googleapis.com/cbk?"]],[["http://khm0.googleapis.com/kh?v=84\u0026hl=zh-TW\u0026","http://khm1.googleapis.com/kh?v=84\u0026hl=zh-TW\u0026"],null,null,null,null,"84",["https://khms0.google.com/kh?v=84\u0026hl=zh-TW\u0026","https://khms1.google.com/kh?v=84\u0026hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/mapslt?hl=zh-TW\u0026","http://mt1.googleapis.com/mapslt?hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/mapslt/ft?hl=zh-TW\u0026","http://mt1.googleapis.com/mapslt/ft?hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/vt?hl=zh-TW\u0026","http://mt1.googleapis.com/vt?hl=zh-TW\u0026"]],[["http://mt0.googleapis.com/mapslt/loom?hl=zh-TW\u0026","http://mt1.googleapis.com/mapslt/loom?hl=zh-TW\u0026"]],[["https://mts0.googleapis.com/mapslt?hl=zh-TW\u0026","https://mts1.googleapis.com/mapslt?hl=zh-TW\u0026"]],[["https://mts0.googleapis.com/mapslt/ft?hl=zh-TW\u0026","https://mts1.googleapis.com/mapslt/ft?hl=zh-TW\u0026"]],[["https://mts0.googleapis.com/mapslt/loom?hl=zh-TW\u0026","https://mts1.googleapis.com/mapslt/loom?hl=zh-TW\u0026"]]],["zh-TW","US",null,0,null,null,"http://maps.gstatic.com/mapfiles/","http://csi.gstatic.com","https://maps.googleapis.com","http://maps.googleapis.com"],["http://maps.gstatic.com/intl/zh_tw/mapfiles/api-3/16/11","3.16.11"],[662838505],1,null,null,null,null,0,"",null,null,0,"http://khm.googleapis.com/mz?v=149\u0026",null,"https://earthbuilder.googleapis.com","https://earthbuilder.googleapis.com",null,"http://mt.googleapis.com/vt/icon",[["http://mt0.googleapis.com/vt","http://mt1.googleapis.com/vt"],["https://mts0.googleapis.com/vt","https://mts1.googleapis.com/vt"],[null,[[0,"m",262000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[47],[37,[["smartmaps"]]]]],0],[null,[[0,"m",262000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[47],[37,[["smartmaps"]]]]],3],[null,[[0,"m",262000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[50],[37,[["smartmaps"]]]]],0],[null,[[0,"m",262000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[50],[37,[["smartmaps"]]]]],3],[null,[[4,"t",132],[0,"r",132000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[5],[37,[["smartmaps"]]]]],0],[null,[[4,"t",132],[0,"r",132000000]],[null,"zh-TW","US",null,18,null,null,null,null,null,null,[[5],[37,[["smartmaps"]]]]],3],[null,null,[null,"zh-TW","US",null,18],0],[null,null,[null,"zh-TW","US",null,18],3],[null,null,[null,"zh-TW","US",null,18],6],[null,null,[null,"zh-TW","US",null,18],0],["https://mts0.google.com/vt","https://mts1.google.com/vt"],"/maps/vt"],2,500,["http://geo0.ggpht.com/cbk?cb_client=maps_sv.uv_api_demo","http://www.gstatic.com/landmark/tour","http://www.gstatic.com/landmark/config","/maps/preview/reveal?authuser=0","/maps/preview/log204","/gen204?tbm=map","http://static.panoramio.com.storage.googleapis.com/photos/"]], loadScriptTime);
};
var loadScriptTime = (new Date).getTime();
getScript("http://maps.gstatic.com/intl/zh_tw/mapfiles/api-3/16/11/main.js");
})();
How to add Google map for GWT into FormPanel?
Here is the code.
Simply call below line and the map is added in formPanel.
GoogleMap gMap = GoogleMap.create(formPanel.getElement(), options);
Some more configuration as defined below:
gwt.xml:
<inherits name="com.google.maps.gwt.GoogleMaps" />
<script src="http://maps.google.com/maps/api/js?sensor=false" />
gwt-maps.jar in build path of the project.
Sample code:
public void onModuleLoad() {
FormPanel formPanel = new FormPanel();
formPanel.setWidth("500px");
formPanel.setHeight("650px");
RootPanel.get().add(formPanel);
MapOptions options = MapOptions.create();
options.setZoom(6);
options.setMapTypeId(MapTypeId.ROADMAP);
options.setDraggable(true);
options.setMapTypeControl(true);
options.setScaleControl(true);
options.setScrollwheel(true);
GoogleMap gMap = GoogleMap.create(formPanel.getElement(), options);
gMap.setCenter(LatLng.create(58.378679, -2.197266));
}
screenshot:
I've been using the branflake API for quite some time, and currently it's working fine one GWT 2.8.
https://mvnrepository.com/artifact/com.github.branflake2267/gwt-maps-api
However, it's no longer maintained, some I'm shopping around for an alternative. In the meantime, I recommend it. The only downside being that it's using the 3.1 API version, which is obsolete. I'll post back if I find a way forward with it, or if I find an alternative.

Categories