ImageGear Java PDF
Add an Image to a PDF

To add an image to a PDF document:

  1. Use the createDocument method of the PDF class to create a new instance of the Document class.
  2. If you want to add an image to an existing document, use the openDocument method of the Document class to load the PDF document.
  3. Use the insertBlankPage method of the Document class to add a new blank page to the PDF document.
  4. Use the getPage method of the Document class to retrieve the newly added or another specific page of the PDF document.
  5. Create a new instance of the AddImageOptions class and provide the necessary data to its members. In particular, set the location and the resulting size of the image to be added to the page. See the AddImageOptions class for details.
  6. Use the addImage methods of the Page class to add an image from file, or from a memory byte array to the PDF page.
  7. Use the close method of the Page class to close the page if you do not need it anymore.
  8. Use the saveDocument method of the Document class to save the resulting PDF document to a file.
  9. Use the close method of the Document class to close the document if you do not need it anymore.

The following is an illustration of how to add an image from file or from a BufferedImage to the PDF page:

 
Copy Code
import com.accusoft.imagegearpdf.*;

import java.io.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
 
class PdfDemo
{
       private PDF pdf;
       private Document document;
 
       // Add an image file to the specified page of the PDF document.
       public boolean addImage(String imageFilename, long pageNumber)
       {
              Page page = null;
    
              try
              {
                     // Retrieve specific page to add image into.
                     page = document.getPage(pageNumber);
 
                     // Get options for adding image.
                     AddImageOptions addOptions = getAddImageOptions();
 
	             // Add image to PDF page.
                     page.addImage(imageFilename, addOptions);
 
                     return true;
              }
              catch (Throwable ex)
              {
        	     // Failed to add image to the page.
                     System.err.println("Exception: " + ex.toString());
 
                     return false;
              }
              finally
              {
                     if (page != null)
                     {
        	            // Close PDF page as it is not needed anymore.
                            page.close();
                     }
              }
       }

       // Add a Bufferedimage to the specified page of the PDF document.
       public boolean addImage(BufferedImage bufferedImage, long pageNumber)
       {
              Page page = null;
 
              try
              {
                     // Retrieve specific page to add image into.
                     page = document.getPage(pageNumber);
 
                     // Get options for adding image.
                     AddImageOptions addOptions = getAddImageOptions();

                     // Convert BufferedImage to byte array.
                     ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                     if (!ImageIO.write(bufferedImage, "png", outputStream))
                     {
	                    // The source image could not be written as a PNG image stream.
	                    return false;
                     }

                     byte[] imageData = outputStream.toByteArray();

	             // Add image to PDF page.
                     page.addImage(imageData, addOptions);
 
                     return true;
              }
              catch (Throwable ex)
              {
        	     // Failed to add image to the page.
                     System.err.println("Exception: " + ex.toString());
 
                     return false;
    	      }
              finally
              {
                     if (page != null)
                     {
        	            // Close PDF page as it is not needed anymore.
                            page.close();
                     }
              }
       }

       // Prepare and return AddImageOptions.
       private AddImageOptions getAddImageOptions()
       {
              AddImageOptions options = new AddImageOptions();
              options.setX(20);
              options.setY(400);
              options.setWidth(200);
              options.setHeight(200);
 
              return options;
       }
}

See Also

 

 


©2016. Accusoft Corporation. All Rights Reserved.

Send Feedback