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:

private static void AddImage01()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create Document and PdfFileMend objects using 'using' block to ensure proper disposal
    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 = System.IO.File.OpenRead(dataDir + "AddImage.png"))
        {
            mender.BindPdf(document);
            mender.AddImage(imageStream, 1, 10, 650, 110, 750); // Add image to first page

            // Save the updated PDF file with '_out' suffix
            mender.Save(dataDir + "AddImage_out.pdf");
        }
    }
}

Add Image

With the help of CompositingParameters, we can superimpose one image on top of another:

private static void AddImage02()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create Document and PdfFileMend objects using 'using' block to ensure proper disposal
    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 = System.IO.File.OpenRead(dataDir + "AddImage.png"))
        {
            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 the output file with '_out' suffix
            mender.Save(dataDir + "AddImage_out.pdf");
        }
    }
}

Add Image

There are several ways to store an image in PDF file. We will demonstrate one of them in the following example:

private static void AddImage03()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create Document and PdfFileMend objects using 'using' block to ensure proper disposal
    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 = System.IO.File.OpenRead(dataDir + "AddImage.png"))
        {
            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 the output file with '_out' suffix
            mender.Save(dataDir + "AddImage_out.pdf");
        }
    }
}
private static void AddImage04()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create Document and PdfFileMend objects using 'using' block to ensure proper disposal
    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 = System.IO.File.OpenRead(dataDir + "AddImage.png"))
        {
            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 the output file with '_out' suffix
            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.

private static void AddText01()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileMend object using 'using' block to ensure proper disposal
    using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
    {
        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 the output file with '_out' suffix
        mender.Save(dataDir + "AddText_out.pdf");
    }
}

Check how it’s looks:

Add Text

The second way to add FormattedText. Additionally, we indicate a rectangle in which our text should fit.

private static void AddText02()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create PdfFileMend object using 'using' block to ensure proper disposal
    using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
    {
        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 the output file with '_out' suffix
        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.

private static void AddText03()
{
    var dataDir = RunExamples.GetDataDir_AsposePdf_Images();  // Using dynamic path

    // Create Document object and add pages
    using (var document = new Aspose.Pdf.Document(dataDir + "AddImage.pdf"))
    {
        document.Pages.Add();
        document.Pages.Add();
        document.Pages.Add();

        // Create PdfFileMend object using 'using' block to ensure proper disposal
        using (var mender = new Aspose.Pdf.Facades.PdfFileMend())
        {
            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 the output file with '_out' suffix
            mender.Save(dataDir + "AddText_out.pdf");
        }
    }
}