Adding Images and Text
PdfFileMend class can help you add images and text in an existing PDF document, at a specified location. It provides two methods with the names AddImage and AddText. AddImage method allows you to add images of type JPG, GIF, PNG, and BMP. AddText method takes an argument of type FormattedText class and adds it in the existing PDF file. The images and text can be added in a rectangle region specified by the coordinates of lower left and upper right points. While adding images you can specify either image file path or a stream of an image file. In order to specify the page number at which the image or text needs to be added, both of these methods provide an argument of page number. So, you can not only add the images and text at the specified location but also on a specified page as well.
Images are an important part of the contents of a PDF document. Manipulating images in an existing PDF file is a common requirement of the people working with PDF files. In this article, we’ll explore how the images can be manipulated, in an existing PDF file, with the help of Aspose.Pdf.Facades namespace of Aspose.PDF for .NET. All the image related operations of Aspose.Pdf.Facades namespace have been consolidated in this article.
Implementation details
Aspose.Pdf.Facades namespace allows you to add new images in an existing PDF file. You can also replace or remove an existing image. A PDF file can also be converted to images. You can either convert each individual page into a single image or a complete PDF file into one image. It allows you to formats i.e. JPEG, BMP, PNG and TIFF etc. You can extract the images from a PDF file as well. You can use four classes of Aspose.Pdf.Facades namespace to implement these operations i.e. PdfFileMend, PdfContentEditor, PdfExtractor and PdfConverter.
Image Operations
In this section, we’ll have a detailed look into these image operations. We’ll also see the code snippets to show the use of the related classes and methods. First of all, let’s have a look at adding an image in an existing PDF file. We can use AddImage method of PdfFileMend class to add a new image. Using this method, you can not only specify the page number on which you want to add the image, but also the location of the image can be specified.
Add Image in an Existing PDF File (Facades)
You can use AddImage method of the PdfFileMend class. The AddImage method requires the image to be added, the page number at which the image needs to be added and the coordinate information. After that, save the updated PDF file using Close method.
In the following example, we add image to the page using imageStream:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImage01()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PDF document and PdfFileMend objects
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Load image into stream
using (var imageStream = File.OpenRead(dataDir + "AddImage.png"))
{
// Bind PDF document
mender.BindPdf(document);
// Add image to first page
mender.AddImage(imageStream, 1, 10, 650, 110, 750);
// Save PDF document
mender.Save(dataDir + "AddImage_out.pdf");
}
}
}
}
With the help of CompositingParameters, we can superimpose one image on top of another:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImage02()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PDF document and PdfFileMend objects
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Load image into stream
using (var imageStream = File.OpenRead(dataDir + "AddImage.png"))
{
// Bind PDF document
mender.BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
// Use compositing parameters for the image
var compositingParameters = new Aspose.Pdf.CompositingParameters(Aspose.Pdf.BlendMode.Multiply);
mender.AddImage(imageStream, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
// Save PDF document
mender.Save(dataDir + "AddImage_out.pdf");
}
}
}
}
There are several ways to store an image in PDF file. We will demonstrate one of them in the following example:
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImage03()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Load image into stream
using (var imageStream = File.OpenRead(dataDir + "AddImage.png"))
{
// Bind PDF document
mender.BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
// Use compositing parameters with BlendMode.Exclusion and ImageFilterType.Flate
var compositingParameters = new Aspose.Pdf.CompositingParameters(
Aspose.Pdf.BlendMode.Exclusion,
Aspose.Pdf.ImageFilterType.Flate);
mender.AddImage(imageStream, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
// Save PDF document
mender.Save(dataDir + "AddImage_out.pdf");
}
}
}
}
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddImage04()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Load image into stream
using (var imageStream = File.OpenRead(dataDir + "AddImage.png"))
{
// Bind PDF document
mender.BindPdf(document);
int pageNum = 1;
int lowerLeftX = 10;
int lowerLeftY = 650;
int upperRightX = 110;
int upperRightY = 750;
// Use compositing parameters with BlendMode.Multiply, ImageFilterType.Flate and false
var compositingParameters = new Aspose.Pdf.CompositingParameters(
Aspose.Pdf.BlendMode.Multiply,
Aspose.Pdf.ImageFilterType.Flate,
false);
mender.AddImage(imageStream, pageNum, lowerLeftX, lowerLeftY, upperRightX, upperRightY, compositingParameters);
// Save PDF document
mender.Save(dataDir + "AddImage_outp.pdf");
}
}
}
}
Add Text in an Existing PDF File (facades)
We can add text in several ways. Consider the first. We take the FormattedText and add it to the Page. After, we indicate the coordinates of the lower left corner, and then we add our text to the Page.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddText01()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PdfFileMend object
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Bind PDF document
mender.BindPdf(dataDir + "AddImage.pdf");
// Create formatted text
var message = new Aspose.Pdf.Facades.FormattedText("Welcome to Aspose!");
// Add text to the first page at position (10, 750)
mender.AddText(message, 1, 10, 750);
// Save PDF document
mender.Save(dataDir + "AddText_out.pdf");
}
}
Check how it’s looks:
The second way to add FormattedText. Additionally, we indicate a rectangle in which our text should fit.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddText02()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Create PdfFileMend object
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Bind PDF document
mender.BindPdf(dataDir + "AddImage.pdf");
// Create formatted text
var message = new Aspose.Pdf.Facades.FormattedText("Welcome to Aspose! Welcome to Aspose!");
// Add text to the first page at the specified position with wrapping
mender.AddText(message, 1, 10, 700, 55, 810);
// Set word wrapping mode to wrap by words
mender.WrapMode = Aspose.Pdf.Facades.WordWrapMode.ByWords;
// Save PDF document
mender.Save(dataDir + "AddText_out.pdf");
}
}
The third example provides the ability to Add Text to specified pages. In our example, let’s add a caption on pages 1 and 3 of the document.
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void AddText03()
{
// The path to the documents directory
var dataDir = RunExamples.GetDataDir_AsposePdf_Images();
// Open PDF document
using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
{
document.Pages.Add();
document.Pages.Add();
document.Pages.Add();
// Create PdfFileMend object
using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
{
// Bind PDF document
mender.BindPdf(document);
// Create formatted text
var message = new Aspose.Pdf.Facades.FormattedText("Welcome to Aspose!");
// Specify the pages where text should be added
int[] pageNums = new int[] { 1, 3 };
// Add text to the specified pages at the specified coordinates
mender.AddText(message, pageNums, 10, 750, 310, 760);
// Save PDF document
mender.Save(dataDir + "AddText_out.pdf");
}
}
}