How can i print pdf file in Java Swing - java

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).

Related

Add print file button to pdf created using iText

I am trying to add "print file" button to a dynamically created pdf file. I am not even sure whether its possible or where to start.
EDIT:
I have this one solution using Javascript from this link. But some PDF Readers dont allow Javascript by default.

Show HTML mark-up in JFrame or open Browser

I am writing a Java program that creates HTML code.
What's the best/easiest possibility to have a preview of the code?
Show the code in the JFrame: How can I do this?
Open the windows browser with the created File? Is this possible?
A totally different approach?
Both are quite easy to do. Here is a page from the Java tutorial that shows how to display HTML in Swing (you'd use a file: URL to display the contents of a file.) You can display a URL in an external browser using Java 6's Desktop class:
Desktop.getDesktop().browse(new URI("file://myfile.html"));
Alternately, save the data in a file with the *.html extension and use
Desktop.getDesktop().open(new File("myfile.html"));

How can I pretty-print Java source code as a PDF?

I'm planning to put some Java code in an appendix to my report. The report is a PDF document, and I use Eclipse for Java.
How can I present it best and do this easily? Any recommendations?
For this purpose, I created a LaTeX doclet. This is a Javadoc doclet, which converts the javadoc comments to LaTeX code, and (if wanted) also includes a pretty-printed version of the source code of the documented methods.
You can then convert the generated LaTeX document to PDF, and append it to your report.
If you use Windows, install CutePDF. This adds a "Printer" that when you print to it it asks you a file name and then prints the output to a .pdf document on your hard drive - hence it is a psuedo printer - it acts like a printer, but is really a pdf file writer.
Don't know solutions for other o/s...
I usually prefer to install a PDF "psuedo" printer in whatever OS I am using. That way I can use the print facilities of whatever app I am using (like Eclipse for example) and get the result in PDF file.
EDIT:
Here is one example of a pseudo printer, this for the Windows platform. Mac OS X has a built in "print to PDF file" capability.
You can use doxygen to generate documentation for your project which can include a formatted source file listing in addition to Javadoc. doxygen can generate both HTML and PDF output. You'll need latex to generate the PDF output.
Another way to pretty print is with IntelliJIDEA. It works also with the community edition.
It's advisable to install a PDF printer, in order to try printouts without wasting a lot of paper. Once you're satisfied with the result, you can print on the real printer. On Windows you can use CutePDF, on Linux Ubuntu install the package cups-pdf with sudo apt-get install cups-pdf.
Note that IntelliJ prints the theme's background, so it's advisable to be on a white background to avoid wasting ink.
To print click on menu File -> Print. The printer selection is in the next menu, after you press on the Print button.
Interestingly you can also print only the selected text, which is useful if you don't want to print import statements.
Other options include the possibility to add line numbers, syntax highlighting and colour printing. On Linux IntelliJ 14.0.3, the default font was a huge size 14, so you might want to change that too.
You could just copy & paste into Word (2007+) and save as PDF. It's a little more straightforward than the file printer, and you can format your code for best results in Word.
You could just copy & paste into OpenOffice/LibreOffice and export to PDF.

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

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).

Generate PCL from PDF in Java

What is the best way to generate a PCL output file from an existing PDF file in java?
It depends on how much you want to invest, and how robust the solution needs to be. For quick and dirty, you can print from Adobe Acrobat to a file, using a PCL driver (look, mom, no Java ...).
The Java Print Service API can process PDF. Use StreamPrintService and write the stream to a file, using PCL for the output format.
If you need to have more control over the content, maybe modify it or add to it, you can use a PDF parser (this one, for instance) and print the resulting HTML from a browser that your application starts, by adding some Javascript, for example.
The StreamPrintService from JDK 6 does only support PS. I am still searching for a StreamPrintService which supports PCL.
We capture PCL generated from Acrobat printing a PDF to a PCL driver and redirect as input to our Windows console PCLXForm program. With a custom script, we can "stream edit" the PCL. We can extract the address block text for address correction, insert the corrected text, add the Intelligent Mail Barcode, 2-D barcodes, sort the documents, batch them by page count, change tray assignments, merge with other documents, etc. The product required is PCLTool SDK - Option V at www.pagetech.com

Categories