How to Replace Single Reference Image in an Existing PDF in VB.NET?
The Syncfusion® .NET PDF library is used to create, read, and edit PDF documents. Using this library, you can replace a single reference image in an existing PDF document.
Steps to replace a single reference image in an existing PDF document
- Create a new windows Forms Application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Form1.cs file.
C#
using Syncfusion.Pdf;
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf.Parsing;
Imports Syncfusion.Pdf
Imports Syncfusion.Pdf.Graphics
Imports Syncfusion.Pdf.Parsing
- Use the following code sample to resize an image before inserting it into the PDF document.
C#
// Load an existing PDF
PdfLoadedDocument loadedDocument = new PdfLoadedDocument("Input.pdf");
// Disable the incremental update
loadedDocument.FileStructure.IncrementalUpdate = false;
// Load the PDF page
PdfLoadedPage loadedPage = loadedDocument.Pages[0] as PdfLoadedPage;
// Create an image instance
PdfBitmap image = new PdfBitmap("Image.jpg");
// Replace the image
loadedPage.ReplaceImage(0, image);
// Save the document
loadedDocument.Save("Output.pdf");
// Close the document
loadedDocument.Close(true);
// This will open the PDF file, and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf");
'Load an existing PDF
Dim loadedDocument As PdfLoadedDocument = New PdfLoadedDocument("Input.pdf")
'Disable the incremental update
loadedDocument.FileStructure.IncrementalUpdate = False
'Load the PDF page
Dim loadedPage As PdfLoadedPage = TryCast(loadedDocument.Pages(0), PdfLoadedPage)
'Create an image instance
Dim image As PdfBitmap = New PdfBitmap("Image.jpg")
'Replace the image
loadedPage.ReplaceImage(0, image)
'Save the document
loadedDocument.Save("Output.pdf")
'close the document
loadedDocument.Close(True)
'This will open the PDF file, and the result will be seen in the default PDF Viewer
Process.Start("Output.pdf")
A complete working sample can be downloaded from ReplaceImageReference.zip.
By executing the program, you will get the output document as follows,
Take a moment to peruse the documentation. You can find the other options like inserting an image in a new and existing PDF document, inserting vector image, replacing images in an existing PDF document, Image pagination, applying transparency, and rotation to the image, convert multi-page TIFF to PDF.
Conclusion
I hope you enjoyed learning about how to replace single reference image in an existing PDF document.
You can refer to our Winforms PDF feature tour page to learn about its other groundbreaking feature representations. You can also explore our documentation to understand how to create and manipulate data.
For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion®, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!