Adding Image watermark to Pdf while Creating it using iTextSharp with ASP.Net backed with VB.Net
Loading
Adding Image watermark to Pdf while Creating it using iTextSharp with ASP.Net backed with VB.Net
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amit MohantyRank #112 · 20K reputationPosted Jul 12, 2023, 8:50 AM EDT
Check this
Dim document As New Document() Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("path/to/output.pdf", FileMode.Create)) document.Open() ' Add content to the document ' Add tables, additional text, etc. document.Add(New Paragraph("Hello, World!")) Dim table As New PdfPTable(1) Dim cell As New PdfPCell(New Phrase("Hello, World!")) table.AddCell(cell) document.Add(table) ' Create a watermark object Dim watermark As PdfContentByte = writer.DirectContentUnder ' Load the watermark image Dim watermarkImage As Image = Image.GetInstance("path/to/watermark.png") watermarkImage.SetAbsolutePosition(100, 100) ' Set the transparency of the watermark watermark.SetGState(New PdfGState() With {.FillOpacity = 0.5F}) ' Add the watermark to each page For i As Integer = 1 To writer.PageNumber watermark.AddImage(watermarkImage) watermark.BeginText() watermark.EndText() Next document.Close()Praveen Mishra183 reputationPosted Jul 13, 2023, 4:44 AM EDT
Hi Sagar ,
To add an image watermark to a PDF while creating it using iTextSharp with ASP.Net backed with VB.Net, you can follow these steps:
Step 1: Install iTextSharp library Make sure you have the iTextSharp library installed in your project. You can install it via NuGet by right-clicking on your project in Visual Studio and selecting "Manage NuGet Packages." Search for "iTextSharp" and install the appropriate package.
Step 2: Import necessary namespaces In your VB.Net code file, import the necessary namespaces for iTextSharp:
Here is code snipped :
Import necessary namespaces
Create a PDF document
Dim document As New Document(PageSize.A4, 50, 50, 50, 50)Create a PdfWriter
Dim writer As PdfWriter = PdfWriter.GetInstance(document, New FileStream("path/to/output.pdf", FileMode.Create))Add content to the document
document.Open() Dim imagePath As String = "path/to/image.png" Dim image As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(imagePath) document.Add(image) Dim watermarkImagePath As String = "path/to/watermark.png" Dim watermarkImage As iTextSharp.text.Image = iTextSharp.text.Image.GetInstance(watermarkImagePath) watermarkImage.SetAbsolutePosition(200, 400) ' Set the position of the watermark image on the page Dim pageCount As Integer = document.PageNumber For i As Integer = 1 To pageCount document.SetPageSize(PageSize.A4) document.NewPage() document.Add(watermarkImage) Next document.Close()