How to Create Images In Pdf generated Table in java itext


In this core java tutorial we will learn How to Create Images In Pdf generated Table using iText library - core java tutorial in Java with program and examples.


We will generate PDF files in java using iText library. PDF files can be generated dynamically in java using iText library.

We will create PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then create image instance using Image.getInstance("E:/tiger.jpg")
and then create cells ( com.itextpdf.text.pdf.PdfPCell) and add image in cell using new PdfPCell(image).





Download jars required to execute program >

Or, you may download jars from here.

Program/Example 1 to - How to Create Images In Pdf generated Table - Using iText library - core java tutorial in Java

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How to Create Images In Pdf generated Table Example Using iText library - core java tutorial
*
*/
public class CreateImagesInPdfTableExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "e:/Create Images In Pdf TableExample.pdf";
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 Image image = Image.getInstance("E:/tiger.jpg");
                 PdfPTable table = new PdfPTable(2); // Create 2 columns in table.
                 // Create cells
                 PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1")); // Add text
                 PdfPCell cell2 = new PdfPCell(image); // Add image
                 // Add cells in table
                 table.addCell(cell1);
                 table.addCell(cell2);
                 // Add table in document
                 document.add(table);
                 document.close();
                 fos.close();
                 System.out.println("PDF created in >> " + pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* Output of above program
PDF created in >> e:/Create Images In Pdf TableExample.pdf
*/


PDF formed after executing above java program (How to Create Images In Pdf generated Table in java itext) will look like this >

Summary -
In this core java tutorial we will learned How to Create Images In Pdf generated Table using iText library - core java tutorial in Java with program and examples. We created PdfPTable (com.itextpdf.text.pdf.PdfPTable) and then created image instance using Image.getInstance("E:/tiger.jpg") and then created cells ( com.itextpdf.text.pdf.PdfPCell) and added image in cell using new PdfPCell(image).



Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.

References >



RELATED LINKS>

iText library tutorial in java - How to Create and work with PDF files in java



BAR AND PIE CHARTS in pdf in java - iText tutorial >

How to create Bar Chart In Pdf in java - iText java tutorial

Create Pie Chart In Pdf in java using itext - iText java tutorial



MODIFY/ EDIT pdf in java - iText tutorial >

How to Modify - Add Text To Existing PDF in java - iText java tutorial example

How To Read And Add Image To Existing Pdf in java Example - iText java tutorial



Solve common Exceptions in itext in java - iText tutorial >

How to solve the Document Has No Pages IOException in iText in java


Alternates to itext in java - iText tutorial >

What are alternatives to iText library for creating Pdf in java?


No comments:

Post a Comment