How can you Add the Images from URL In Pdf in java


In this core java tutorial we will learn Adding the Images from specific provided URL In Pdf using iText library - core java tutorial in Java with program and examples.


Follow these steps to Add Images from URL in java using iText>
  1. Create FileOutputStream - the file in which created PDF will be stored
OutputStream fos = new FileOutputStream(new File(pdfFilePath));

  1. create Document (This document will represent the PDF document)
Document class is present in com.itextpdf.text.Document.
Document document = new Document();

  1. Attach PdfWriter to document,whenever any PDF element is added to document, it will get written to FileOutputStream

PdfWriter.getInstance(document, fos);

  1. open the document.
document.open();
                

  1. Now we will get the image from your local system or if you are working on web applications you may give path of image on server.
And create object/instance of com.itextpdf.text.Image like (Image image = Image.getInstance(new URL(urlOfImage));)

Then optionally you may set absolute position, Then optionally you may set the image width and height.
                
  1. add image to document
document.add(image);
  1. close document
document.close();
                

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

How to Create the Table In Pdf in java - iText java tutorial

Create Greek List In pdf in java using iText - iText java tutorial

How to Create Password Protected Pdf in java - iText Example

How to add TEXT as Watermark In Pdf in java - iText java tutorial



Download jars required to execute program >

Or, you may download jars from here.


Program/Example to Adding the Images from specific provided URL In Pdf - Using iText library - core java tutorial

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.URL;
import com.itextpdf.text.Document;
import com.itextpdf.text.Image;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
/**
*
*  How can you Add Images In Pdf File From some URL Example
*  Using iText library - core java tutorial
*
*/
public class AddingImagesInPdfFileFromURLExample {
   public static void main(String[] args) {
          try {
                 String pdfFilePath = "E:/Adding Images In Pdf File from URL Example.pdf";
                
                 OutputStream fos = new FileOutputStream(new File(pdfFilePath));
                 Document document = new Document();
                 PdfWriter.getInstance(document, fos);
                 document.open();
                 document.add(new Paragraph("This is AddingImagesInPdfFileExample.pdf"));
                 String urlOfImage = "https://lh5.googleusercontent.com/E3eX_"
                              + "hgl-eK9cX6j6XMyM6eOkCPvYs9Us5ySKIu60_fYFGlKywKP9pGfNcTj"
                              + "7WDSnDb4zrHubFRLHGK4DqBiLBa4HzRAWx728iHpDrL21HxzsEXSHAa"
                              + "lK49-rBzvU3DlmGURrwg";
                
                 //Add Image from some URL
                 Image image = Image.getInstance(new URL(urlOfImage));
                
                
                 //Set absolute position for image in PDF (or fixed)
                 image.setAbsolutePosition(100f, 500f);
                
                 //Scale image's width and height
                 image.scaleAbsolute(200f, 200f);
                
                 //Scale image's height
                 image.scaleAbsoluteWidth(200f);
                 //Scale image's width
                 image.scaleAbsoluteHeight(200f);
                
                 document.add(image); //Add image to document
                 document.close();             
                 fos.close();
                
                 System.out.println("PDF created in >> "+ pdfFilePath);
          } catch (Exception e) {
                 e.printStackTrace();
          }
   }
}
/* OUTPUT of above program/Example -
PDF created in >> E:/Adding Images In Pdf File from URL Example.pdf
*/


PDF formed after executing above java program(How can you Add the Images from URL In Pdf in java) will look like this >


Summary -
So in this core java tutorial we learned how to Add the Image from specific provided URL In Pdf using iText library - core java tutorial in Java with program and examples.




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>

Creating the UnOrdered List In pdf in java

Create Greek List In pdf in java using iText

Creating the Table and set the Column Width And Spacing In Pdf in java


How to do the Cell Alignment In Pdf generated table in Java

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


How to Create Password Protected Pdf in java - iText Example

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


Creating first pdf and setting attributes using itext in java - iText tutorial >

Create the first Pdf Example Using iText library in java - iText java tutorial example

Setting Pdf Attributes like Title, Author, Creator, subject, Keywords, Header and CreationDate in java



Creating PASSWORD PROTECTED PDF in java - iText tutorial >

How to Create Password Protected Pdf in java - iText Example



Creating IMAGES in pdf in java - iText tutorial >

Adding the Images In Pdf in java example using iText - iText java tutorial

How to Add And Rotate Images In Pdf in java - iText java tutorial example

How can you Add the Images from URL In Pdf in java program





Adding WATERMARK in pdf in java - iText tutorial >

How to add TEXT as Watermark In Pdf in java - iText java tutorial

How to add IMAGE as Watermark In Pdf in iText in java example




Create new page, set page height and width in java -
iText tutorial >

How To Create New Pages In Pdf Using Itext - iText java tutorial example

How to Find Pdf Page Height And Width in java - iText java tutorial program



HEADER and FOOTER in pdf in java - iText tutorial >

How To Set Header and Footer in pdf in java using Itext Example - iText java tutorial



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?




eEdit
Must read for you :