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

Last Reply 2 months ago By simonoldma

Posted 2 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 2 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


I agree, here is the link: