Watermark to PDF using iSharptext

Last Reply 15 days ago By dharmendr

Posted 16 days ago

My code (as provided earlier)

 
Private Sub PdfStampWithNewFile(watermarkImagePath As String, sourceFilePath As String, destinationFilePath As String)
    Dim pdfReader = New iTextSharp.text.pdf.PdfReader(sourceFilePath)
    Dim pdfStamper = New iTextSharp.text.pdf.PdfStamper(pdfReader, New FileStream(destinationFilePath, FileMode.Create))
    Dim image = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    image.SetAbsolutePosition(200, 400)
    For i As Integer = 0 To pdfReader.NumberOfPages - 1
        Dim content = pdfStamper.GetUnderContent(i + 1)
        content.AddImage(image)
    Next
    pdfStamper.Close()
End Sub
 
Private Sub PdfStampInExistingFile(watermarkImagePath As String, sourceFilePath As String)
    Dim bytes As Byte() = File.ReadAllBytes(sourceFilePath)
    Dim img = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    img.SetAbsolutePosition(200, 400)
    Dim waterMark As PdfContentByte
 
    Using stream As New MemoryStream()
        Dim reader As New PdfReader(bytes)
        Using stamper As New PdfStamper(reader, stream)
            Dim pages As Integer = reader.NumberOfPages
            For i As Integer = 1 To pages
                waterMark = stamper.GetUnderContent(i)
                waterMark.AddImage(img)
            Next
        End Using
        bytes = stream.ToArray()
    End Using
    File.WriteAllBytes(sourceFilePath, bytes)
End Sub

This workks and put the image on the document but when I open the pdf the watermark FLASHES for a split second then disapears. What else do I need so the watermark stays visible??

 

Thanks

gollnick

Posted 16 days ago

Hi pgollnic,

Please take reference to the below code and correct your code.

HTML

<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />

Namespaces

C#

using iTextSharp.text.pdf;
using System.IO;

VB.Net

Imports System.IO
Imports iTextSharp.text.pdf

Code

C#

protected void btnSubmit_Click(object sender, EventArgs e)
{
    PdfStampWithNewFile("D:\\Watermark.png", "C:/Users/anand/Desktop/Test.pdf", Server.MapPath("~/files/Test1.pdf"));
    PdfStampInExistingFile("D:\\Watermark.png", "C:/Users/anand/Desktop/Test.pdf");
}

private void PdfStampWithNewFile(string watermarkImagePath, string sourceFilePath, string destinationFilePath)
{
    var pdfReader = new PdfReader(sourceFilePath);
    var pdfStamper = new PdfStamper(pdfReader, new FileStream(destinationFilePath, FileMode.Create));
    var image = iTextSharp.text.Image.GetInstance(watermarkImagePath);
    image.SetAbsolutePosition(200, 400);
    for (var i = 0; i < pdfReader.NumberOfPages; i++)
    {
        var content = pdfStamper.GetUnderContent(i + 1);
        content.AddImage(image);
    }
    pdfStamper.Close();
}

private void PdfStampInExistingFile(string watermarkImagePath, string sourceFilePath)
{
    byte[] bytes = File.ReadAllBytes(sourceFilePath);
    var img = iTextSharp.text.Image.GetInstance(watermarkImagePath);
    img.SetAbsolutePosition(200, 400);
    PdfContentByte waterMark;

    using (MemoryStream stream = new MemoryStream())
    {
        PdfReader reader = new PdfReader(bytes);
        using (PdfStamper stamper = new PdfStamper(reader, stream))
        {
            int pages = reader.NumberOfPages;
            for (int i = 1; i <= pages; i++)
            {
                waterMark = stamper.GetUnderContent(i);
                waterMark.AddImage(img);
            }
        }
        bytes = stream.ToArray();
    }
    File.WriteAllBytes(sourceFilePath, bytes);
}

 VB.Net

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs)
    PdfStampWithNewFile("D:\Watermark.png", "C:/Users/anand/Desktop/Test.pdf", Server.MapPath("~/files/Test1.pdf"))
    PdfStampInExistingFile("D:\Watermark.png", "C:/Users/anand/Desktop/Test.pdf")
End Sub

Private Sub PdfStampWithNewFile(ByVal watermarkImagePath As String, ByVal sourceFilePath As String, ByVal destinationFilePath As String)
    Dim pdfReader = New PdfReader(sourceFilePath)
    Dim pdfStamper = New PdfStamper(pdfReader, New FileStream(destinationFilePath, FileMode.Create))
    Dim image = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    image.SetAbsolutePosition(200, 400)
    For i = 0 To pdfReader.NumberOfPages - 1
        Dim content = pdfStamper.GetUnderContent(i + 1)
        content.AddImage(image)
    Next
    pdfStamper.Close()
End Sub

Private Sub PdfStampInExistingFile(ByVal watermarkImagePath As String, ByVal sourceFilePath As String)
    Dim bytes As Byte() = File.ReadAllBytes(sourceFilePath)
    Dim img = iTextSharp.text.Image.GetInstance(watermarkImagePath)
    img.SetAbsolutePosition(200, 400)
    Dim waterMark As PdfContentByte
    Using stream As MemoryStream = New MemoryStream()
        Dim reader As PdfReader = New PdfReader(bytes)
        Using stamper As PdfStamper = New PdfStamper(reader, stream)
            Dim pages As Integer = reader.NumberOfPages

            For i As Integer = 1 To pages
                waterMark = stamper.GetUnderContent(i)
                waterMark.AddImage(img)
            Next
        End Using
        bytes = stream.ToArray()
    End Using
    File.WriteAllBytes(sourceFilePath, bytes)
End Sub

Screenshot


Posted 16 days ago

I have two documents (pdf) and a confidentail.png.

On one of there this works great. On the other.. nothing. The SIZE of the pdf increases a little, but when viewed .. does not show.

How do I upload these 2 documents and the png so you can see what I'm talking about.

One is a scanned image saved to a pdf(the one NOT working). The other is a document created on the fly. Word document with a saveas to a pdf format. THIS one works like a champ.

help

gollnick

 


Posted 15 days ago

Hi,

Please upload the screenshots to http://imgur.com/ and
copy the link from Direct Link option and add to the screenshot in the editor.

How to add image or screenshot to the Editor


Posted 15 days ago

Hi and thanks.. I would like to upload the 3 FILES.. not images.. Is that possible?

Or if I use GetOverContent it'll work BUT than I need a transparent or opaic (see through) image.

 

thanks

gollnick

 


Posted 15 days ago

you can create gif video file and upload there and share the link.