0

I need to add text to the lower right corner of a pdf. I was able to successfully do this with an annotation but I couldn't flatten it so I attempted to add the text via a paragraph instead. Now none of my text is showing up. Could anyone point out where I'm going wrong with my code below? (Or maybe just tell me how to flatten an annotation... the commented out portion works fine I just need it flattened)

private MemoryStream AddPageNumbers(MemoryStream stream)
    {
        MemoryStream ms = new MemoryStream();
        PdfStamper Stamper = null;            
        PdfReader Reader = new PdfReader(stream);
        try
        {
            PdfCopyFields Copier = new PdfCopyFields(ms);
            Copier.AddDocument(Reader);
            Copier.Close();

            PdfReader docReader = new PdfReader(ms.ToArray());
            ms = new MemoryStream();
            Stamper = new PdfStamper(docReader, ms);                

            for (int i = 1; i <= Reader.NumberOfPages; i++)
            {                    
                //iTextSharp.text.Rectangle newRectangle = new iTextSharp.text.Rectangle(315, 19, 560, 33);
                //var pcb = new iTextSharp.text.pdf.PdfContentByte(Stamper.Writer);                                       

                //string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();

                //FontFactory.RegisterDirectories();
                //Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 8f, Font.NORMAL));                                      
                //Paragraph para = new Paragraph(PageNumber, fontNormalArial);

                //var annot = iTextSharp.text.pdf.PdfAnnotation.CreateFreeText(Stamper.Writer, newRectangle, para.Content, pcb);
                //annot.Flags = iTextSharp.text.pdf.PdfAnnotation.FLAGS_PRINT;                    
                //annot.BorderStyle = new iTextSharp.text.pdf.PdfBorderDictionary(0, 0);                                                         
                //Stamper.AddAnnotation(annot, i);

                //Stamper.FreeTextFlattening = true;
                //Stamper.FormFlattening = true;

                PdfContentByte cb = new PdfContentByte(Stamper.Writer);
                string PageNumber = "Confirmation of Completion Report " + i.ToString() + " of " + Reader.NumberOfPages.ToString();

                FontFactory.RegisterDirectories();
                Font fontNormalArial = new Font(FontFactory.GetFont("Arial", 10, Font.NORMAL));                    
                Paragraph para = new Paragraph(PageNumber, fontNormalArial);                    
                para.Alignment = Element.ALIGN_RIGHT;
                ColumnText ct = new ColumnText(cb);
                ct.AddText(para);
                ct.SetSimpleColumn(315, 19, 560 , 38);
                ct.SetIndent(316, false);
                ct.Go();                   

            }

        }
        catch (Exception ex)
        {
            string querystring = "?error=" + ex.Message.ToString();
            Response.Redirect("~/ErrorPage.aspx" + querystring);
        }
        finally
        {
            if (Stamper != null)
            {
                Stamper.Close();
            }
        }

        return ms;
    }
ovaltein
  • 1,185
  • 2
  • 12
  • 34
  • See my edit on this page for a very simple version: http://stackoverflow.com/a/9845722/231316 – Chris Haas Jul 19 '13 at 20:13
  • @Chris You're lifesaver, thank you! I ended up adjusting the 3rd chunk of code in your answer to fit my needs. I see where I was going wrong with my contentbyte. – ovaltein Jul 19 '13 at 20:51

0 Answers0