Fri. Apr 23rd, 2021

Requirement: I have a demo.jspx page.

On that page I have an af:selectOneChoice to select the Font Type.

Also there is an af:inputText field to enter the text to be displayed in the generated PDF report.

There is also an af:commandButton named  Generate PDF Report to generate the PDF Report.

Detailed screen is shown below:

Solution: For solution to the above requirement follow the steps as shown below:

Step 1: Create an Oracle ADF Fusion Web Application and name the application as PDFReport1.

Step 2: Let us download the pdfbox-app-2.0.0.jar from the below link:

http://pdfbox.apache.org/download.cgi

The Apache PDFBox library is an open source Java tool 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. Apache PDFBox also includes several command line utilities. Apache PDFBox is published under the Apache License v2.0.

Step 3: Open ViewController project of our application and add the pdfbox-app-2.0.0.jar. To add the pdfbox-app-2.0.0.jar follow the steps shown below:

  1. Double click ViewController project.
  2. Click Libraries and Claspath
  3. Click Add JAR/Directory to browse and add the pdfbox-app-2.0.0.jar file
  4. Click OK

Step 4: Create a demo.jspx page. Drag and drop af:panelBox inside the demo.jspx page and set text=”PDF report Creator in Oracle ADF” id=”pb1″ showDisclosure=”false” for the af:panelBox.

Drag and drop af:selectOneChoice and set label=”Select Font” binding=”#{PDFGeneratorBean.fontTypeBind}”

Drag and drop af:inputText and set label=”Enter your Text” rows=”5″ contentStyle=”width:500px; required=”true” showRequired=”true” binding=”#{PDFGeneratorBean.textToConvert}”.

Create PDFGeneratorBean.java as shown below:

Open PDFGeneratorBean.java and declare private PDType1Font fontDef = PDType1Font.COURIER;

Also generate the accessors for the fontDef

Also declare, private List<SelectItem> fontItems;

Generate the accessors for fontItems as shown below:

Inside public List<SelectItem> getFontItems( ) method write the below codes:

Now set value=”#{PDFGeneratorBean.fontDef}” for the af:selectOneChoice and value=”#{PDFGeneratorBean.fontItems}” for the f:selectItems as shown below:

Create actionListener=”#{PDFGeneratorBean.generatePdf}” for the af:commandButton.

Open PDFGeneratorBean.java and write the generatePdf method codes as shown below:

Thus, the complete demo.jspx page code is shown below:

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1" xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
    <jsp:directive.page contentType="text/html;charset=UTF-8"/>
    <f:view>
        <af:document title="demo.jspx" id="d1">
            <af:form id="f1">
                <af:panelBox text="PDF report Creator in Oracle ADF" id="pb1" showDisclosure="false">
                    <f:facet name="toolbar"/>
                    <af:selectOneChoice label="Select Font" id="soc1" binding="#{PDFGeneratorBean.fontTypeBind}"
                                        value="#{PDFGeneratorBean.fontDef}">
                        <f:selectItems value="#{PDFGeneratorBean.fontItems}" id="si1"/>
                    </af:selectOneChoice>
                    <af:inputText id="it1" label="Enter your Text" rows="5" contentStyle="width:500px;" required="true"
                                  showRequired="true" binding="#{PDFGeneratorBean.textToConvert}"/>
                    <af:commandButton text="Generate PDF Report" id="cb1"
                                      actionListener="#{PDFGeneratorBean.generatePdf}"/>
                </af:panelBox>
            </af:form>
        </af:document>
    </f:view>
</jsp:root>

Thus, the complete PDFGeneratorBean.java code is shown below:

package com.susanto.benas;

import java.io.File;
import java.io.IOException;

import java.util.ArrayList;
import java.util.List;

import javax.faces.event.ActionEvent;

import javax.faces.model.SelectItem;

import oracle.adf.view.rich.component.rich.input.RichInputText;

import oracle.adf.view.rich.component.rich.input.RichSelectOneChoice;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

public class PDFGeneratorBean {
    private RichInputText textToConvert;
    private RichSelectOneChoice fontTypeBind;
    private PDType1Font fontDef = PDType1Font.COURIER;
    private List<SelectItem> fontItems;

    public PDFGeneratorBean() {
    }

    public void setTextToConvert(RichInputText textToConvert) {
        this.textToConvert = textToConvert;
    }

    public RichInputText getTextToConvert() {
        return textToConvert;
    }

    public void setFontTypeBind(RichSelectOneChoice fontTypeBind) {
        this.fontTypeBind = fontTypeBind;
    }

    public RichSelectOneChoice getFontTypeBind() {
        return fontTypeBind;
    }

    public void setFontDef(PDType1Font fontDef) {
        this.fontDef = fontDef;
    }

    public PDType1Font getFontDef() {
        return fontDef;
    }

    public void setFontItems(List<SelectItem> fontItems) {
        this.fontItems = fontItems;
    }

    public List<SelectItem> getFontItems() {
        if (fontItems == null) {
            fontItems = new ArrayList<SelectItem>();
            fontItems.add(new SelectItem(PDType1Font.HELVETICA_BOLD, "Helvetica bold"));
            fontItems.add(new SelectItem(PDType1Font.TIMES_BOLD, "Times bold"));
            fontItems.add(new SelectItem(PDType1Font.TIMES_ITALIC, "Times italic"));
            fontItems.add(new SelectItem(PDType1Font.TIMES_BOLD_ITALIC, "Times bold italic"));
            fontItems.add(new SelectItem(PDType1Font.HELVETICA, "Helvetica regular"));
            fontItems.add(new SelectItem(PDType1Font.HELVETICA_OBLIQUE, "Helvetica italic"));
            fontItems.add(new SelectItem(PDType1Font.HELVETICA_BOLD_OBLIQUE, "Helvetica bold italic"));
            fontItems.add(new SelectItem(PDType1Font.COURIER, "Courier"));
            fontItems.add(new SelectItem(PDType1Font.COURIER_BOLD, "Courier bold"));
            fontItems.add(new SelectItem(PDType1Font.COURIER_OBLIQUE, "Courier italic"));
            fontItems.add(new SelectItem(PDType1Font.COURIER_BOLD_OBLIQUE, "Courier bold italic"));
            fontItems.add(new SelectItem(PDType1Font.SYMBOL, "Symbol Set"));
            fontItems.add(new SelectItem(PDType1Font.ZAPF_DINGBATS, "Dingbat Typeface"));
        }
        return fontItems;
    }

    public void generatePdf(ActionEvent actionEvent) throws IOException, InterruptedException {
        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:/Susanto_pdf.pdf");
        } catch (Exception e) {
            e.printStackTrace();
        }
        document.close();
        if ((new File("D:\\Susanto_pdf.pdf")).exists()) {
            Process p = Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler D:\\Susanto_pdf.pdf");
            p.waitFor();
        } else {
            System.out.println("File is not exists");
        }
    }
}

Step 5: Save all and run the application. Thus the ran application is shown below:

Select the Font Type from the af:selectOneChoice component .

Write some text which we want to be displayed in the generated PDF report as shown below:

Click on Generate PDF Report button. Thus, the generated report is shown below:

Hence, the solution to our requirement.

If you like the post please comment, share, and do join me on Facebook. Please subscribe to my YouTube Channel for video tutorials.

Thanks & Regards,
Susanto Paul

 325 total views,  1 views today

By Susanto Paul

Susanto is an Oracle ACE, a qualified MCA, MBA, and a highly-skilled Senior Oracle Specialist. He is an enthusiastic Blogger and YouTuber who helps learners to solve their complex problems more efficiently. He has 9+ years of experience in multiple technologies like AWS, Oracle ADF, Oracle APEX, Oracle JET, Oracle VBCS, Oracle IDCS, Oracle PL/SQL, Oracle Integration Cloud, Java, JDBC, Servlets, JSP, Spring, Hibernate, HTML5, CSS3, JavaScript, TypeScript, NodesJS, Angular, MySQL, Oracle WebLogic Server, JUnit, JMeter, Selenium Web Driver, etc. He is a certified: Oracle Certified Professional Java SE 6 Programmer, Oracle ADF 11g Certified Implementation Specialist, Oracle Cloud Platform Application Integration 2020 Certified Specialist, Oracle Cloud Infrastructure Foundations 2020 Certified Associate, and Oracle Cloud Infrastructure Developer 2020 Certified Associate

Leave a Reply

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