Work with images in PDF in .NET SDK

Manipulate images in PDF Document using server-side .NET API.

Get Started

How to work with images via Cloud .NET SDK

In order to work with images in PDF via Cloud .NET SDK , we’ll use Aspose.PDF Cloud .NET SDK This Cloud SDK allows you to easily build cloud-based PDF creator, editor & converter apps in C#, ASP.NET, or other .NET languages for various cloud platforms. Open NuGet package manager, search for Aspose.PDF Cloud and install. You may also use the following command from the Package Manager Console: PM> Install-Package Aspose.Pdf-Cloud

Steps to create a image via .NET SDK

Aspose.PDF Cloud developers can easily load & create image in PDF in just a few lines of code.

  1. Initialize the Aspose PDF API
  2. Upload the PDF Document to Cloud Storage
  3. Reads a local image file and uploads it to the Aspose.PDF Cloud storage.
  4. Inserts the uploaded image into the uploaded PDF file at a specific location.
  5. Download the result if needed it
 

This sample code shows creating a image in PDF documents


    public async Task AddImage()
    {
        const string localPdfDocument = @"C:\Samples\sample.pdf";
        const string localFolder = @"C:\Samples";
        const string storageTempFolder = "TempPdfCloud";
        const string storageFileName = "sample.pdf";
        const string localImageFile = @"C:\Samples\sample.jpg";
        const string storageImageFile = "sample.jpg";
        const string resultFileName = "output_sample_images_add.pdf";
        const int pageNumber = 1;

        // Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
        pdfApi = new PdfApi(AppSecret, AppSid);

        using var file = File.OpenRead(localPdfDocument);
        await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, storageFileName), file);

        using var imageFile = File.OpenRead(localImageFile);
        await pdfApi.UploadFileAsync(Path.Combine(storageTempFolder, storageImageFile), imageFile);

        double llx = 10;
        double lly = 10;
        double urx = 100;
        double ury = 100;

        AsposeResponse response = await pdfApi.PostInsertImageAsync(storageFileName,
                pageNumber, 
                llx, lly, urx, ury,
                Path.Combine(storageTempFolder, storageImageFile),
                folder: storageTempFolder);

        if (response == null)
            Console.WriteLine("AddImage(): Unexpected error!");
        else if (response.Code < 200 || response.Code > 299)
            Console.WriteLine("AddImage(): Failed to insert image in the document.");
        else
        {
            await( await pdfApi.DownloadFileAsync(Path.Combine(storageTempFolder, storageFileName)))
                .CopyToAsync(File.Create(Path.Combine(localFolder, resultFileName)));

            Console.WriteLine("AddImage(): image '{0}' successfully inserted in the document '{1}.", storageImageFile, resultFileName);
        }
    }
 

With our .NET SDK you can

  • Add PDF document’s header & footer in text or image format.
  • Add tables & text or image stamps to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free App to replace the images in PDF files online and test the functionality.