How to show and deploy a PDF document from Java using WebStart - java

I want to show a PDF document from a Java (Swing) application in a system independent manner (provided that a PDF viewer is properly installed on the target system).
Also I'd like to deploy this PDF document using Java WebStart.
Could you please tell me the "standard" way to achieve this? (I confess, I'm to lazy/busy to look up the details ...) Thanks!

I assume you mean you want to deploy the PDF along with the Java Web Start application? If so, you simply need to package the PDF(s) with your webstart application. When your application downloads and runs, the PDFs will have come too, and your code can use the getClass().getResource("/where/is/my.pdf") type of lookup to locate the PDF and then operate on it for display. You might also need to get your code to read the PDF out of the resources and save it in a temp file (File.createTempFile()) so that the PDF viewer can see it.
rough idea:
// Find the PDF in the Webstart App download
InputStream in = getClass().getResourceAsStream("/where/is/my.pdf");
// create a temp file to copy the pdf to
File tmpFile = File.createTempFile("my", "pdf");
OutputStream out = new FileOutputStream(tmpFile);
// stream the file from in to out ... heaps of examples on the net for doing this ("copy files")
// display the file
Desktop.getDesktop().open(tmpFile);
// ideally clean the tmp file up at some point.

You can use the Java 6 Desktop system and Desktop.open() to open the associated desktop application for your document (in this case, a PDF file).

Related

Possible to automate an excel process using java?

My current program is trying to download files and then combine them into one large excel file. The issue that I'm struggling with is that the website I'm downloading them from is for some reason making them .html files, but appending the .xls extension to them. This allows them to be opened by Excel manually but does not allow me to use Apache POI in order to read them as it sees a file format/extension difference. My process is as follows:
1 - Run part of my program which downloads a file through my web browser using Selenium - This works fine
2 - Manually open each downloaded file and Save-As xlsx files (Note: When I open them in Excel manually is when I'm told there is a file format/extension difference just to be clear)
3 - Run the rest of my program which combs through each new file (the ones created in step 2) and appends all the data to the ultimate output file - This works fine
Is there any way to automate the process or am I going to have to continue to do it manually?
you said in the comment that you opened the file in text editor and saw that it is HTML5.
I would use HTML parser like jsoup to get the data that you need and create a new file using Apache POI.
You can use EasyXLS library. It allows to read HTML files and save as XLSX.
ExcelDocument workbookForXLSX = new ExcelDocument();
for (int i=0; i<fileCount; i++){
ExcelDocument workbookForHTML = new ExcelDocument();
workbookForHTML.easy_LoadHTMLFile(filePath[i]);//or stream to the file
workbookForXLSX.easy_addWorksheet((ExcelWorksheet)workbookForHTML.easy_getSheetAt(0));
workbookForHTML.Dispose();
}
workbookForXLSX.easy_WriteXLSXFile(filePathXLSX);
workbookForXLSX.Dispose();
You can download the Excel library for Java from:
https://www.easyxls.com/java-excel-library
More details about reading HTML files and what HTML tags are supported at:
https://www.easyxls.com/manual/basics/import-from-html-file-format.html

How to import files to iPhone file system for use in app?

I'm trying to develop an iPhone version of an Android app I made. In the Android version you can import spreadsheets to use as templates into the app by copying the file into your sd card from your computer and loading it through an open file dialogue through the app. What would be the a similar way I can do this for non-jailbroken iPhones and iPads to achieve this same functionality?
My goal is to do this without the use of a second app to import them; etc.
You can use file sharing. To activate it you have to check UIFileSharingEnabled in your plist file. And you cad drag files to your document directory and out of it via iTunes. You can even specify what data types your app wants to support and you can even register the app to open specific file so if someone, for example send you an email with this file you can press on the file attached and if your app is installed on the device you will be able to use your app to open this file.
What are the type of your file ?.
You can use the UIWebView to display files with this extensions :
Excel (.xls)
Keynote (.key.zip)
Numbers (.numbers.zip)
Pages (.pages.zip)
PDF (.pdf)
Powerpoint (.ppt)
Word (.doc)
Where to store your file ?
It depends of your business and your application.
You can store it in a server and the application can load it.
You can store it in the Application sandbox.
For example, you can store it in the application's bundle and use this code to laod your file and display it in a UIWebView :
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
I am not sure I understand your question but have you consider converting your spreadsheet do a .csv file?
After that you can read your csv and transform it into array.
Take a look at this tutorial. It has code that converts a CSV into arrays of the strings in each column / row.

Get the filepath of the file being accessed by a window using JNA with Java

I am using JNA with Java to find some properties about open windows on a Windows machine desktop. I am trying to find a way to get the file being accessed by an arbitrary windowed application. For instance, say I get information regarding the window of an open pdf document in adobe. I want to be able to get the filepath of the pdf document displayed in the window.
I know about the GetWindowModuleFileName() method, however this gets you the filepath of the executable of the application, i.e. 'javaw.exe'. If you have 'my.pdf' open in adobe, I'd like to get the filepath of this document, i.e. 'C:\...\my.pdf'.
I've done some searching around (on this site and others) and haven't found anything yet on this in particular.
thank you for your time, -Kevin
If you know the process ID, you can get the list of all files currently opened by the process using Handle utility. However, it depends if adobe reader continues to keep the file open or closes it after reading it completely.

How can i print pdf file in Java Swing

I want to like after click a JButton will directly pop out a printer window to print the pdf file no need to show the file, is it possible?
Multiple ways to do it ,
You can get access to printers installed, this requires how the printers are configured etc and then you require some print plugin to write it to pdf file.
Else you can use plenty of java pdf libraries available to do the pdf creation part too
See Desktop.print(File).

How to show the printable document directly in acrobat with out saving?

I am using PD4ML to print a PDF file and It is working fine. Now the thing is I want show that file directly in acrobat with out save that file. In Local version I am using
Program.launch(getFilePath());
It is working fine but in web version I am unable to get that.
Can you please suggest me, Its very helpful.
Thanks,
Vara Kumar PJD
The web isn't like your desktop, so forget about doing things on the web the way you do them on the desktop without at least some effort.
Know that you don't read PDF files on the web using Acrobat without a browser plugin. Or some other reader like Foxit Reader.
My recommendation: forget about doing it this way. Either server your pdf as a file that can be downloaded, or read this SO post about embedding PDF in HTML.
I don't think this will be possible: "showing file outside browser in an application without user consent" because that is how browser are made for security reason. The best you can do is, as pointed in earlier post is by darioo, to show file in browser or prompt user to download/open.

Categories