Error: Using operand of type iTextSharp.txt.pdf.PdfStamper must implement System.IDisposable in ASP.Net

Last Reply 3 months ago By simonoldma

Posted 3 months ago

Dear friends from ASPFORUMS,

Tried to use the solution from your article published in ASPSNIPPETS "iTextSharp: Add Page numbers to existing PDF using C# and VB.Net

18 Mar 2015  5 Comments 23185 Views
Getting the error: 'Using' operand of type 'iTextSharp.text.pdf.PdfStamper' must implement 'System.IDisposable'.
Please help me,
Simon Mardakh.
Posted 3 months ago

Hi simonoldma,

Will you please let me know which version of dll you are using.

Posted 3 months ago

Hi simonoldma,

Change with the below code and check.


protected void AddPageNumber(object sender, EventArgs e)
    byte[] bytes = File.ReadAllBytes(Server.MapPath("~/PDF/Test.pdf"));
    Font blackFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK);
    using (MemoryStream stream = new MemoryStream())
        PdfReader reader = new PdfReader(bytes);
        PdfStamper stamper = new PdfStamper(reader, stream);
        int pages = reader.NumberOfPages;
        for (int i = 1; i <= pages; i++)
            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, new Phrase(i.ToString(), blackFont), 568f, 15f, 0);
        bytes = stream.ToArray();
    File.WriteAllBytes(Server.MapPath("~/PDF/Test1.pdf"), bytes);


Protected Sub AddPageNumber(sender As Object, e As EventArgs)
    Dim bytes As Byte() = File.ReadAllBytes(Server.MapPath("~/PDF/Test.pdf"))
    Dim blackFont As Font = FontFactory.GetFont("Arial", 12, Font.NORMAL, BaseColor.BLACK)
    Using stream As New MemoryStream()
        Dim reader As New PdfReader(bytes)
        Dim stamper As New PdfStamper(reader, stream)
        Dim pages As Integer = reader.NumberOfPages
        For i As Integer = 1 To pages
            ColumnText.ShowTextAligned(stamper.GetUnderContent(i), Element.ALIGN_RIGHT, New Phrase(i.ToString(), blackFont), 568.0F, 15.0F, 0)
        bytes = stream.ToArray()
    End Using
    File.WriteAllBytes(Server.MapPath("~/PDF/Test1.pdf"), bytes)
End Sub


Posted 3 months ago

The code worked.  Thank you. Must admit that ASPFORUMS is the best in the world!  Simon M.


Posted 3 months ago

Not certain which version is.  But as I know it is not too old.  was downloaded in 2016.  I got the help from dharmendr  and do not have problems with code.

I agree, here is the link: