Free Support Forum - aspose.com

Add image in existing PDF document using Aspose.PDF for .NET

Hi there,
I need to add an image to a pdf file and I am trying to follow the example in the demos. 

The following line of code throws an error that the g() method does not exist.

pdfDocument.Pages[1].Contents.Add(new Operator.q());
Also the following doesn’t find a reference to Aspose.Pdf.Image class:
Aspose.Pdf.Image image1 = new Aspose.Pdf.Image(sec1);
The whole code is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Aspose.Pdf;
using Aspose.Pdf.Text;
using Aspose.Pdf.DOM;

namespace PDFGenerator
{
public class PDFGeneratorFactory
{

    <span style="color:blue;">public</span> <span style="color:blue;">void</span> ReplaceImage()
    {
        <span style="color:blue;">int</span> lowerLeftX = 100;
        <span style="color:blue;">int</span> lowerLeftY = 100;
        <span style="color:blue;">int</span> upperRightX = 200;
        <span style="color:blue;">int</span> upperRightY = 200;

        <span style="color:green;">//open document</span>
        <span style="color:#2b91af;">Document</span> pdfDocument = <span style="color:blue;">new</span> <span style="color:#2b91af;">Document</span>(<span style="color:#a31515;">"e:\\invoice_2.pdf"</span>);

        Aspose.Pdf.Image image1 = <span style="color:blue;">new</span> Aspose.Pdf.Image(sec1);

        <span style="color:green;">//replace a particular image</span>
        pdfDocument.Pages[1].Resources.Images.Delete(1);

        pdfDocument.Pages[1].Resources.Images.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">FileStream</span>(<span style="color:#a31515;">"e:\\vivo_logo_BW.jpg"</span>, <span style="color:#2b91af;">FileMode</span>.Open));

        <span style="color:green;">//using q operator: this operator saves current graphics state</span>
        pdfDocument.Pages[1].Contents.Add(<span style="color:blue;">new</span> <span style="color:#2b91af;">Operator</span>.q());


        <span style="color:green;">//save updated PDF file</span>
        pdfDocument.Save(<span style="color:#a31515;">"e:\\invoice_3.pdf"</span>);

    }

}

}

Hi,


Thanks for using our products.

Recently we have made some changes in operator class names and I am afraid the example contains older product binaries and older code snippet. In order to accomplish your requirement, please try using the code snippet shared over Add Image in an Existing PDF File

You may also check Changes in Operator class names

In case of any further query, please feel free to contact. We are sorry for this inconvenience.