iTEXT PDF generation

Posted By :Harshit Verma |30th August 2018

It is a Java library that is created by Bruno Lowagie. Under this article, I will explain how to create a PDF with your java code. iText follows the hierarchical structure. The smallest text within the iText , that is "chunk"  which is a String that has a pre-defined font, combining a group of chunks from the "phrase".

For using the itext with your project you just need to provide the maven dependency within your pom.xml

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>

First of all, you just need to create a class under which, you first need to define the path at which you want to create or generate the PDF file as a result. Then you need to create the Document object, that allows you to, configure the property of the whole document. After that using the Pdfwriter method we get the instance, that creates the file at the destination location using the file output stream. Now use the open method of the document for writing, using the document you can add the title to the PDF, you can add the subject to your PDF, add the author, one can also add the keywords to the PDF.

Now the turn comes at where we define the header, here for defining the header we use the PdfPCell. Using this you can provide the specifications to your header such as you can set the alignment of the header, you are able to set the background color of the header, setting up the border width, along with the percentage 

The last turn comes for the table data, here you can add up the data that you want to show in the pdf. You can also add up the specifications with the table data also. We can use the add cell method of the table in order to add up the entries to the table.

Here is the code for generating the pdf:-

public void  getReport() {
        Document document = new Document(PageSize.A2.rotate());
        try {
            File file = new File("home/Report.pdf");
            FileOutputStream stream = new FileOutputStream(file);
            PdfWriter.getInstance(document, stream);
            document.open();
            PdfPTable table = new PdfPTable(16);
            addTableHeader(table);
            addRows(table);
            document.addTitle("Report");
            document.add(table);
            document.close();
            stream.close();
        

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addTableHeader(PdfPTable table) {
        Stream.of("id", "Quantity", "Buy Price", "Sell Price", "Buy OrderStatus", "Sell OrderStatus", "Buy OrderId", "Sell OrderId", "Max Execution Attempt", "Buy Exchange", "Sell Exchange", "Profit", "Market" ," Commission Paid"," Description"," Profit Percentage"
        )
                .forEach(columnTitle -> {
                    logger.info("am in loop "+columnTitle);
                    PdfPCell header = new PdfPCell();
                    header.setHorizontalAlignment(Element.ALIGN_CENTER);
                    header.setBackgroundColor(BaseColor.LIGHT_GRAY);
                    header.setBorderWidth(2);
                    header.setPhrase(new Phrase(columnTitle));
                    table.setWidthPercentage(100);
                    table.addCell(header);
                });
    }

    private void addRows(PdfPTable table) {
        List<OrderBook> OrderBooks = RepositoryImpl.getReport();
        OrderBooks.forEach(data->{
            table.addCell(data.getId().toString());
            table.addCell(data.getQuantity().toString());
            table.addCell(data.getBuyPrice().toString());
            table.addCell(data.getSellPrice().toString());
            table.addCell(data.getBuyOrderStatus().toString());
            table.addCell(data.getSellOrderStatus().toString());
            table.addCell(data.getBuyOrderId());
            table.addCell(data.getSellOrderId());
            table.addCell(String.valueOf(data.getMaxExecutionAttempt()));
            table.addCell(data.getBuyExchange().toString());
            table.addCell(data.getSellExchange().toString());
            table.addCell(data.getProfit().toString());
            table.addCell(data.getMarket().getMarketName());
            table.addCell(data.getCommissionPaid().toString());
            table.addCell(data.getDescription());
            table.addCell(data.getProfitPercentage().toString());
        });
    }

About Author

Harshit Verma

Harshit is a bright Web Developer with expertise in Java and Spring framework and ORM tools Hibernate.

Request For Proposal

Sending message..

Ready to innovate ? Let's get in touch


Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Notice: Undefined index: HTTP_REFERER in /var/html/www/AI/wp-content/themes/oxides-child/functions.php on line 272

Chat With Us