PPTX DOCX XLSX PDF ODP
Aspose.PDF  for .NET
PDF

Working with Images in PDF document via .NET

Build your own .NET apps to manipulate images in document files using server-side APIs.

How to Insert image to PDF Document Using C#

In order to annotate PDF file, we’ll use Aspose.PDF for .NET API which is a feature-rich, powerful and easy to use document manipulation API for C# platform. Open NuGet package manager, search for Aspose.PDF and install. You may also use the following command from the Package Manager Console.

Package Manager Console Command


    PM> Install-Package Aspose.PDF

Add Image to PDF document via C#

You need aspose.pdf.dll to try the code in your environment.

  • Create a Document object and open the input PDF document.
  • Get the page you want to add an image to.
  • Add the image into the page’s Resources collection.
  • Use operators to place the image on the page.
  • Use the GSave operator to save the current graphical state.
  • Use ConcatenateMatrix operator to specify where the image is to be placed.
  • Use the Do operator to draw the image on the page.
  • Finally, use GRestore operator to save the updated graphical state.
  • Save the file.

System Requirements

Aspose.PDF for .NET is supported on all major operating systems. Just make sure that you have the following prerequisites.

  • Microsoft Windows or a compatible OS with .NET Framework, .NET Core, and PHP, VBScript, Delphi, C++ via COM Interop.
  • Development environment like Microsoft Visual Studio.
  • Aspose.PDF for .NET DLL referenced in your project.
 

Add Image in an Existing PDF File

    // Open document
    Document pdfDocument = new Document(dataDir+ "AddImage.pdf");

    // Set coordinates
    int lowerLeftX = 100;
    int lowerLeftY = 100;
    int upperRightX = 200;
    int upperRightY = 200;

    // Get the page where image needs to be added
    Page page = pdfDocument.Pages[1];

    // Load image into stream
    FileStream imageStream = new FileStream(dataDir + "aspose-logo.jpg", FileMode.Open);

    // Add image to Images collection of Page Resources
    page.Resources.Images.Add(imageStream);

    // Using GSave operator: this operator saves current graphics state
    page.Contents.Add(new Aspose.Pdf.Operators.GSave());

    // Create Rectangle and Matrix objects
    Aspose.Pdf.Rectangle rectangle = new Aspose.Pdf.Rectangle(lowerLeftX, lowerLeftY, upperRightX, upperRightY);
    Matrix matrix = new Matrix(new double[] { rectangle.URX - rectangle.LLX, 0, 0, rectangle.URY - rectangle.LLY, rectangle.LLX, rectangle.LLY });

    // Using ConcatenateMatrix (concatenate matrix) operator: defines how image must be placed
    page.Contents.Add(new Aspose.Pdf.Operators.ConcatenateMatrix(matrix));
    XImage ximage = page.Resources.Images[page.Resources.Images.Count];

    // Using Do operator: this operator draws image
    page.Contents.Add(new Aspose.Pdf.Operators.Do(ximage.Name));

    // Using GRestore operator: this operator restores graphics state
    page.Contents.Add(new Aspose.Pdf.Operators.GRestore());
    dataDir = dataDir + "AddImage_out.pdf";

    // Save updated document
    pdfDocument.Save(dataDir);
 
  • About Aspose.PDF for .NET API

    Aspose.PDF API can be used for PDF document manipulation and parsing within applications. One can create, modify, compress, secure, print or save PDF to TXT, HTML, PCL, XFA, XML, XPS, EPUB, TEX, Images and more formats. Aspose.PDF is a standalone API and it does not depend on any software including Adobe Acrobat.

    Image to PDF Online App

    Image to PDF online converter now by visiting our Live Demos website. The live demo has the following benefits

      No need to download or setup anything
      No need to write any code
      Just upload your PDF file and hit the "Remove" button
      Instantly get the download link for the resultant file

    PDF What is PDF File Format

    Portable Document Format (PDF) is a type of document created by Adobe back in 1990s. The purpose of this file format was to introduce a standard for representation of documents and other reference material in a format that is independent of application software, hardware as well as Operating System. PDF files can be opened in Adobe Acrobat Reader/Writer as well in most modern browsers like Chrome, Safari, Firefox via extensions/plug-ins. Most of the commercially available software suites also offer conversion of their documents to PDF file format without the requirement of any additional software component. Thus, PDF file format has full capability to contain information like text, images, hyperlinks, form-fields, rich media, digital signatures, attachments, metadata, Geospatial features and 3D objects in it that can become as part of source document.

    Read More