Please disable your adblock and script blockers to view this page

Search this blog

Showing posts with label Apache PDFBox. Show all posts
Showing posts with label Apache PDFBox. Show all posts

Wednesday 1 February 2017

Export ViewObject data to PDF file using Apache PDFBox

Hello All
Hope you all are doing well :)

This post is about exporting view object data in a pdf file directly from af:table , export to excel is built in feature of ADF but exporting data in PDF requires little bit of extra effort
So here for this requirement I am using Apache PDFBox library , previously I have posted about using this API to create PDF file from text data
I know many of you will not visit that link ;) So a quick overview

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 http://pdfbox.apache.org/ 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();
            document.addPage(page);
    
            // 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.beginText();
            contentStream.setFont(font, 10);
            contentStream.moveTextPositionByAmount(50, 700);
            contentStream.drawString(textToConvert.getValue().toString());
            contentStream.endText();
    
            // Make sure that the content stream is closed:
            contentStream.close();
    
            // Save the results and ensure that the document is properly closed:
            try {
                document.save("D:/Test_pdf.pdf");
            } catch (COSVisitorException e) {
            }
            document.close();
    

  • 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");
                p.waitFor();
    
            } 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