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

Sharing is Caring

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 the jar to view controller project’s library and class-path
Apache PDFBox
  •  To convert text to pdf format, I have used an input text and bind it to the 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

Cheers 🙂 Happy Learning

Related Posts

0 thoughts on “Creating pdf file using Apache PDFBox API in ADF Faces and opening it in new window -Oracle ADF”

Leave a Reply

Your email address will not be published. Required fields are marked *