在内存流中将绝对位置文本添加到pdf

时间:2013-07-19 17:53:52

标签: c# pdf itextsharp

我需要在pdf的右下角添加文字。我能够通过注释成功完成此操作,但我无法将其展平,因此我尝试通过段落添加文本。现在我的文字都没有显示出来。任何人都可以指出我下面的代码出错了吗? (或者也许只是告诉我如何压平注释......注释掉的部分工作正常我只需要将其弄平)

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;
    }

0 个答案:

没有答案