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

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.

Related

Preprocess OpenDoPE Word file (Macro or docx4j)

I have recently discovered the OpenDoPE project. From what I understand from the walkthrough, .docx files must be preprocessed to replace repeatable contents for example.
If I understand well there are 2 ways to do it :
Using docx4j
Using a Macro
I am developing a rails web platform, and I'd prefer the preprocessing to be done client-side, so with the Macro. But then If I can only do it with java, I'll go with it
Problem : when I click the "inject macro" button in the OpenDop Add-in in Word2010, nothing happens :O
So two possible answers :
Explain how I can install this macro in the document
Explain how I can have docx4j to preprocess the document. ie : from a linux terminal, what command with what parameters should I type to preprocess some document.docx file containing repeatable-contents ?
I tried clicking the "inject macro" button in my Word 2010, and it worked, that is:
it prompted me to save a .docm file
when i opened the .docm file in Word, the macro ran
Trying to open the macro in Word's VBA editor though, I couldn't. Seems I obfuscated it :-(
I do have the source files floating around, which I'd be happy to put on GitHub.
Please note however, that it is 4yo unmaintained 'proof of concept' level code (whereas the docx4j code is actively maintained and used by a variety of companies).
For non-interactive processing using Java, see samples/ContentControlBindingExtensions.java
To invoke from a Linux command line, that would need modifying slightly; also you need of course to pass a suitable class path.
The other way you could do it is by installing this simple web app in say Tomcat.

Java: How to pretty print .java file

I have a JTextArea, and I have this being populated with a chosen .java file, the problem is that the file is appearing as just normal text. I would like to add some pretty print so that the user will be able to see the file as if the file was open in eclipse (keywords different color, comments will be in grey... etc)
I have not been able to find any good examples online for how to do this. what would be the best way to do this?
EDIT:
I got this to finally work, i added the JSyntaxPane jar file to my classpath (jsyntaxpane-0.9.5-b29.jar to be exact) then i added the following two lines of code to get it working for my JEditorPane.
jsyntaxpane.DefaultSyntaxKit.initKit();
JEditorPane.setContentType("text/java");
This can also be used on the following languages: JavaScript, Properties, Groovy, C, C++, XML, SQL, Ruby and Python... just by simply changing text/java to text/LANGUAGE_YOU_WANT in the above code
JTextArea can only display plain text (like notepad in Windows). If you want to show pretty printed source code with colors, you need JEditorPane, which allows you to display HTML.
Now you can either generate HTML manually with syntax-highlighted Java source code or use some library.
See also
Where can I find a syntax highlighting library for Java?
JEdit-Syntax has a JEditTextArea class that will do syntax highlighting. This project was spun off to package some subcomponents of the JEdit project.

Any tool to help writing programing article with MS Office Word

All! When I use MS office 2010 Word to write an article about Java, I always need to embed some Java code into the document. I wonder whether there is such a plugin for Word, or another software tool, that can help me do such a job.
Especially features on code formatting and keywords highlighting are welcome!
There are several ways to get syntax highlighted text in Word. Here are some I know:
Eclipse + OpenOffice Writer (Word should do this as well)
Mark and copy a code section in Eclipse
Paste into Writer (Ctrl+Shift+V) or Word (Ctrl+Alt+V) as HTML or RTF text
Using Notepad++
Paste the code section to Notepad++
Select the appropriate syntax highlighting
Use the NppExport Plugin to export to a RTF file
Open the file with Word
http://www.ifcx.org/wiki/Wings.html will let you embed live code and execute it in a document, but it requires open office and some plugins.
There is also Highlight, which can export to multiple formats (including HTML, RTF and even SVG!), and has many themes.

Extract text from PDF (google app engine)

Is there any free Java library for extracting text from PDF, that is compatible with Google Application Engine?
I've read about PDFJet, but it can't read PDF, can it?
Is there perhaps other way how to extract text from PDF? I tried http://www.pdfdownload.org/, unfortunately they don't handle non-English characters correctly.
iText now has a text parsing module (I'm one of the parser authors). See the com.itextpdf.text.pdf.parser.PdfContentReaderTool class for an example of how to use it.
PdfBox does not run on GAE. It uses not-allowed java classes.
(GAE only permits these http://code.google.com/appengine/docs/java/jrewhitelist.html)
I have partially modified a very old version of PdfBox (0.7.3) to be GAE complaiant. Now I'm able to extract text from PDF (whole page or rectangular area). I only modified a minumum part of the pdf text extraction and not the whole PdfBox. :)
The idea was to remove refences to java.awt.retangle & C. using my own "rectangle" class.
More info: http://fhtino.blogspot.com/2010/04/pdfbox-text-extration-gae.html
I modified the latest (1.8.0-Snapshot) version to run on Google AppEngine. Had to disable one Unit-Test, but it runs fine for simple text extraction.
Following the simple try-fail-fix approach i had to modify 5 files in total. Pretty doable.
You'll also have to explicitly use a RandomAccessBuffer, like Fabrizio explained.
For the extra lazy, heres the compiled jar, dependencies for text extraction, and the patch. Note that it might not work for every usecase (i.e. rectangle based extraction). Used it to extract text of a whole page.
https://docs.google.com/folder/d/0B53n_gP2oU6iVjhOOVBNZHk0a0E/edit
I know there is http://pdfbox.apache.org/index.html
Apache PDFBox is an open source Java
PDF library for working with PDF
documents. This project allows
creation of new PDF documents,
manipulation of existing documents and
the ability to extract content from
documents.
but I've never tested it.
Last month, I'd just finished extracting text from pdf file in my project. I used XPDF tool for getting text, and text coordinates, but I used it in Xcode (Objective-C). This tool was open source, written by C++, and able to be encoded in many language. However, I didn't know whether XPdf would be work on your java, or not. Anyway, You can try this tool.

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