Replace the existing image on the PDF file in C#/VB.NET
To
err is human and when it comes to documentation, we all can make mistakes.
Modern day documents are mostly shared in the PDF format and contain lots of
visuals and images. However, there will be times when you may wish to replace
an image in the PDF document due to an error pasting the image or otherwise. In
this article, I will take a
detailed look at the methods for replacing the existing image on the PDF file. To
achieve this functionality, you just need to follow a few simple steps. Read on
for more information.
Programming Environment
In this test, Free Spire.PDF
for .NET is introduced into the program. The Spire.PDF.dll file can be
referenced by:
Method 1:
Download Free
Spire.PDF for .NET locally, unzip it, and install it. After the
installation is complete, find Spire.PDF.dll in the BIN folder under the
installation path. Then open the "Solution Explorer" in Visual
Studio, right-click "References", "Add Reference", and add
a reference to the dll file in the BIN folder of the local path to the program.
Method 2:
Install via NuGet. It can
be installed by the following 2 methods:
(1) You can open the
"Solution Explorer" in Visual Studio, right-click
"References", "Manage NuGet Packages", then search for
"Free Spire.PDF", and click "Install". Wait for the program
installation to complete.
(2) Copy the following
content to the PM console installation.
Install-Package FreeSpire.PDF -Version 8.6.0
Replace the existing image
- Create a new instance of PdfDocument and load a PDF document from file using PdfDocument.LoadFromFile() method.
- Get the first page of the loaded PDF file.
- Load the new image that we want to replace the existing image.
- Replace first image on the PDF file with the new loaded image using page.ReplaceImage()method.
- Save the document to file using PdfDocument.SaveToFile() method.
using Spire.Pdf;
using Spire.Pdf.Graphics;
namespace ReplaceImage
{
class Program
{
static void Main(string[] args)
{
// Create a new instance of PdfDocument and load a PDF
document from file
PdfDocument doc = new PdfDocument();
doc.LoadFromFile("Yellowstone National Park.pdf");
//Get the first page of the loaded PDF file
PdfPageBase page = doc.Pages[0];
// Load the new image that we want to replace the
existing image
PdfImage image = PdfImage.FromFile("image.jpg");
//Replace first image on the PDF file with the new loaded
image
page.ReplaceImage(0,
image);
//Save the document to file
doc.SaveToFile("result.pdf");
}
}
【VB.NET】
Imports Spire.Pdf
Imports Spire.Pdf.Graphics
Namespace ReplaceImage
Friend Class Program
Private Shared Sub Main(ByVal args As String())
' Create a new instance of PdfDocument and
load a PDF document from file
Dim doc As PdfDocument = New
PdfDocument()
doc.LoadFromFile("Yellowstone National Park.pdf")
'Get the first page of the loaded PDF file
Dim page As PdfPageBase = doc.Pages(0)
' Load the new image that we want to
replace the existing image
Dim image As PdfImage = PdfImage.FromFile("image.jpg")
'Replace first image on the PDF file with
the new loaded image
page.ReplaceImage(0, image)
'Save the document to file
doc.SaveToFile("result.pdf")
End Sub
End Class
End Namespace
Effective shot:
Attention:
The code path is for
reference only, and you can customize the path as you want.
Comments
Post a Comment