Follow by Email

Friday, 9 August 2013

Creating pdf file using Apache PDFBox API in ADF Faces and opening it in new window -Oracle ADF

Apache PDFBox library is an open source java tool for working with PDF documents, go to for API docs and download jar (pdfbox-app-1.8.2) from there.

  •  Now create a fusion web application and add jar to view controller project's library and class-path
  •  To convert text to pdf format, i have used an input text and bind it to bean (to get value)
  •  Now see the button code that converts text to pdf file format using Apache PDFBox

  •         PDDocument document = new PDDocument();
            PDPage page = new PDPage();
            // Create a new font object selecting one of the PDF base fonts
            PDFont font = getFontDef();
            // Start a new content stream which will "hold" the to be created content
            PDPageContentStream contentStream = new PDPageContentStream(document, page);
            // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
            contentStream.setFont(font, 10);
            contentStream.moveTextPositionByAmount(50, 700);
            // Make sure that the content stream is closed:
            // Save the results and ensure that the document is properly closed:
            try {
            } catch (COSVisitorException e) {

  • now your pdf is generated in fixed path, and if user want to open it immediately , try to do this

  •         if ((new File("D:\\Hello World.pdf")).exists()) {
                Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler D:\\Hello World.pdf");
            } else {
                System.out.println("File is not exists");

  • this code invokes File Protocol using Runtime class
  • Run this application and see-
 Click on generate button-

For more details and functionality visit Apache PDFBox site
Sample ADF Application-Download

No comments :

Post a Comment