Add Image to Existing PDF File
Every PDF page contains
Contents properties. Resources can be images and forms for example, while content is represented by a set of PDF operators. Each operator has its name and argument. This example uses operators to add anh image to a PDF file.
To add an image to an existing PDF file:
- Create a
Documentobject and open the input PDF document.
- Get the page you want to add an image to.
- Add the image into the page’s
- Use operators to place the image on the page:
- Use the
GSaveoperator to save the current graphical state.
ConcatenateMatrixoperator to specify where the image is to be placed.
- Use the
Dooperator to draw the image on the page.
- Finally, use
GRestoreoperator to save the updated graphical state.
- Use the
- Save the file.
The following code snippet shows how to add the image in a PDF document.
Add Reference of a single Image multiple times in a PDF Document
Sometimes we have a requirement to use same image multiple times in a PDF document. Adding a new instance increases the resultant PDF document. We have added a new method
XImageCollection.Add(XImage) in Aspose.PDF for .NET 17.1.0. This method allows to add reference to the same PDF object as original image that optimize the PDF Document size.
Delete Images from a PDF File
To delete an image from a PDF file:
- Create a
Documentobject and open the input PDF file.
- Get the
Pagethat holds the image from the
- Images are held in the
Imagescollection, found in the page's
- Delete an image with the
- Saved the output like using the
The following code snippet shows how to delete an image from a PDF file.
Extract Images from the PDF File
The image's index returns an
XImage object. This object provides a
Save method which can be used to save the extracted image. The following code snippet shows how to extract images from a PDF file.
Get the Resolution and Dimensions of Embedded Images
This topic explains how to use the operator classes in the Aspose.PDF namespace which provide the capability to get resolution and dimension information about images without having to extract them.
Getting Image Information without Extracting the Images
Aspose.PDF for .NET lets you extract images from a PDF file. Once the images are extracted, we can get information about the image's dimensions and resolution. However, there are scenarios where we do not want to extract the images to get this information. When this is the case, we can still using Aspose.PDF for .NET to get the information.
- First, load the source PDF file (with images).
- Then create an ArrayList object to hold the names of any images in the document.
- Get the images using the
- Create a stack object to hold the image's graphics state and use it to keep track of different image states.
- Create a
ConcatenateMatrixobject which defines current transformation. It also supports scaling, rotating, and skewing any content. It concatenates the new matrix with previous one. Please note that we cannot define the transformation from scratch but only modify the existing transformation.
Because we can modify the matrix with
ConcatenateMatrix, we may also need to revert back to the original image state. Use
These operators are paired so they should be called together. For example, if you do some graphics work with complex transformations and finally return transformations back to initial state, the approach will be something like this:
As a result, text is drawn in regular form but some transformations are performed between the text operators. In order to display the image or to draw form objects and images, we need to use the Do operator.
We also have a class named
XImagewhich provides two properties,l
Height, which can be used to get image dimensions.
- Perform some calculations to compute the image resolution.
- Display the information in a Command Prompt along with the image name.
The following code snippet shows you how to get an image's dimensions and resolution without extracting the image from the PDF document.
Working with Image Placement
With the release of Aspose.PDF for .NET 7.0.0, we introduced classes called
ImagePlacementCollection which provide similar capability as the classes described above to get an image's resolution and position in a PDF document.
ImagePlacementAbsorberperforms image usage search as the
- ImagePlacement provides the members
Rectanglethat return actual image placement values.
Search and Get Images from PDF Document
ImagePlacementAbsorber allows you to search among images on all pages in a PDF document.
To search a whole document for images:
- Call the
Acceptmethod takes an
ImagePlacementAbsorberobject as a parameter. This returns a collection of
- Loop through the
ImagePlacementsobjects and get their properties (
resolutionand so on).
The following code snippet shows how to search a document for all its images.
To get an image from an individual page, use the following code:
Identify if image inside PDF is Colored or Black & White
Different type of compression can be applied over images to reduce their size. The type of compression being applied over image depends upon the ColorSpace of source image i.e. if image is Color (RGB), then apply JPEG2000 compression, and if it is Black & White, then JBIG2/JBIG2000 compression should be applied. Therefore identifying each image type and using an appropriate type of compression will create best/optimized output.
A PDF file may contain Text, Image, Graph, Attachment, Annotation etc elements and if the source PDF file contains images, we can determine image Color space and apply appropriate compression for image to reduce PDF file size. The following code snippet shows the steps to Identify if image inside PDF is Colored or Black & White.
Replace Image in Existing PDF File
Replace method allows you to replace an image in an existing PDF file.
Images collection can be found in a page's Resources collection. To replace an image:
- Open the PDF file using the Document object.
- Replaced the , save the updated PDF file using Save method of the Document object.
The following code snippet shows you how to replace an image in a PDF file.
Control Image Quality
It is possible to control the quality of an image that's being added to a PDF file. Use the overloaded
Replace method in the
public void Replace(int index, Stream stream, int quality)
The following code snippet demonstrates how to convert all the document images into JPEGs that use 80% quality for compression.
Set Image Size
It is possible to set the size of an image that's being added to a PDF file. In order to set size, you can use FixWidth and FixHeight properties of
Aspose.Pdf.Image Class. The following code snippet demonstrates how to set the size of an image:
Set Default Font Name
Support for DICOM Images
Aspose.PDF for .NET supports functionality to add DICOM images to PDF documents. The following code snippet shows how to use this functionality.