Replace an Image in PDF via .NET SDK
Replace Images in a PDF Document using Cloud .NET SDK
Get StartedHow to replace Images in PDF via .NET SDK
To replace 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.
Package Manager Console Command
PM> Install-Package Aspose.Pdf-Cloud
Steps to replace images in PDF using .NET
Aspose.PDF Cloud developers can easily load & replace PDF images in just a few lines of code.
- Initialize the Aspose PDF API
- Upload the PDF Document to Cloud Storage
- Reads a local image file and uploads it to the Aspose.PDF Cloud storage.
- Inserts the uploaded image into the uploaded PDF document at a specific location using image Id.
- Download the result if needed it
Replace images in PDF using .NET
public async Task ImagesReplace()
{
const string LOCAL_FOLDER = @"C:\Samples";
const string REMOTE_TEMP_FOLDER = "TempPdfCloud";
const string PDF_DOCUMENT = "sample.pdf";
const string PDF_OUTPUT = "output_sample_images_replace.pdf";
const string IMAGE_FILE_NAME = "sample.jpg";
const string IMAGE_ID = "GI5TCNJTGU3TWMRRHEWDIOBMGI3DELBWGM";
// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required).
pdfApi = new PdfApi(AppSecret, AppSid);
using var file = File.OpenRead(Path.Combine(LOCAL_FOLDER, PDF_DOCUMENT));
await pdfApi.UploadFileAsync(Path.Combine(REMOTE_TEMP_FOLDER, PDF_DOCUMENT), file);
using var imageFile = File.OpenRead(Path.Combine(LOCAL_FOLDER, IMAGE_FILE_NAME));
await pdfApi.UploadFileAsync(Path.Combine(REMOTE_TEMP_FOLDER, IMAGE_FILE_NAME), imageFile);
ImageResponse response = await pdfApi.PutReplaceImageAsync(PDF_DOCUMENT, IMAGE_ID, Path.Combine(REMOTE_TEMP_FOLDER, IMAGE_FILE_NAME), folder: REMOTE_TEMP_FOLDER);
if (response == null)
Console.WriteLine("ImagesReplace(): Unexpected error!");
else if (response.Code < 200 || response.Code > 299)
Console.WriteLine("ImagesReplace(): Failed to replace image in the document.");
else {
Console.WriteLine("ImagesReplace(): image '{0}' successfully replaced in the document '{1}.", response.Image.Id, PDF_DOCUMENT);
await ( await pdfApi.DownloadFileAsync(Path.Combine(REMOTE_TEMP_FOLDER, PDF_DOCUMENT)))
.CopyToAsync(File.Create(Path.Combine(LOCAL_FOLDER, PDF_OUTPUT))));
Console.WriteLine("DownloadFile(): File '{0}' successfully downloaded.", PDF_OUTPUT);
}
}
Work with Images in PDF
Images make documents visually engaging and help break up large blocks of text, making them more attractive to readers. Photos, screenshots, or icons can add context to the content, making it more relatable and meaningful for the audience. Including images in PDFs is essential for creating engaging presentations, user manuals, marketing brochures, and educational materials. Add the Images into PDF documents with Aspose.PDF Cloud .NET SDK.
With our .NET library you can:
- Add PDF document’s header & footer in text or image format.
- Add tables & stamps (text or image) 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 change image dimensions in PDF files online and test the functionality.
- Learning Resources
- Documentation
- Source Code
- API References
- Product Support
- Free Support
- Paid Support
- Blog
- Why Aspose.PDF Cloud for .NET?
- Customers List
- Security