1

I am using following code for adding watermark on a page but output MemoryStream is always empty, I am wondering where I have done wrong.

public static MemoryStream testwatermark(MemoryStream pdf)
{
    PdfReader reader = new PdfReader(pdf);

    using (MemoryStream output = new MemoryStream())
    {
        PdfStamper pdfStamper = new PdfStamper(reader, output);

        for (int pageIndex = 1; pageIndex <= reader.NumberOfPages; pageIndex++)
        {
            iTextSharp.text.Rectangle pageRectangle = reader.GetPageSizeWithRotation(pageIndex);
            PdfContentByte pdfData = pdfStamper.GetUnderContent(pageIndex);
            pdfData.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED), 40);
            PdfGState graphicsState = new PdfGState(); graphicsState.FillOpacity = 0.4F;
            pdfData.SetGState(graphicsState);     
            //set color of watermark     
            pdfData.SetColorFill(BaseColor.BLUE);
            pdfData.BeginText();     
            //show text as per position and rotation     
            pdfData.ShowTextAligned(Element.ALIGN_CENTER, "BlueLemonCode", pageRectangle.Width / 2, pageRectangle.Height / 2, 45);     
            //call endText to invalid font set     
            pdfData.EndText();
        }
        pdfStamper.Close();
        return output;
    }
}

Following error is shown when output stream is further viewed.

CanRead = false
CanSeek = false
CanWrite = false
Capacity = 'output.Capacity' threw an exception of type 'System.ObjectDisposedException'
Length = 'output.Length' threw an exception of type 'System.ObjectDisposedException'
Position = 'output.Position' threw an exception of type 'System.ObjectDisposedException'
Sylca
  • 2,523
  • 4
  • 31
  • 51
mck
  • 978
  • 3
  • 14
  • 38
  • Try having a look at the code featured: http://stackoverflow.com/questions/2372041/c-sharp-itextsharp-pdf-creation-with-watermark-on-each-page – Ryan McDonough Nov 13 '12 at 11:08
  • return byte[] instead of the disposed stream (because of that using statement). return output.ToArray(); – VahidN Nov 13 '12 at 19:11

1 Answers1

1

Problem is solved by returning bytes instead of memorystream in the above code.

.
.
.
.
pdfStamper.Close();
return output;
mck
  • 978
  • 3
  • 14
  • 38