No problem. The "shortened for brevity" was just for the filler text anyway.  I 
am also including the code for the footer as well (just in case that makes any 
difference).  I appreciate the help!

    // Pulled from a sample found on the web
    public class PdfPageEvent : IPdfPageEvent
    {
        private PdfTemplate objTemplate;
        private PdfContentByte objCB;

        #region Contructors

        public PdfPageEvent()
        {

        }

        #endregion

        #region Interface Methods

        /**
        * Called when the document is opened.
        *
        * @param writer the <CODE>PdfWriter</CODE> for this document
        * @param document the document
        */
        void IPdfPageEvent.OnOpenDocument(PdfWriter writer, Document document)
        {
            objCB = writer.DirectContent;
            objTemplate = objCB.CreateTemplate(50f, 50f);
        }

        /**
        * Called when a page is finished, just before being written to the 
document.
        *
        * @param writer the <CODE>PdfWriter</CODE> for this document
        * @param document the document
        */
        void IPdfPageEvent.OnEndPage(PdfWriter writer, Document document)
        {
            SetFooter(writer, document);
        }

        /**
         * Called when the document is closed.
         * <P>
         * Note that this method is called with the page number equal
         * to the last page plus one.
         *
         * @param writer the <CODE>PdfWriter</CODE> for this document
         * @param document the document
         */
        void IPdfPageEvent.OnCloseDocument(PdfWriter writer, Document document)
        {
            objTemplate.BeginText();
            BaseFont objBaseFont = 
BaseFont.CreateFont("C:/windows/fonts/Arial.ttf", BaseFont.WINANSI, 
BaseFont.EMBEDDED);
            Font objFont = new Font(objBaseFont, 10f);
            objTemplate.SetFontAndSize(objFont.BaseFont, objFont.Size);
            objTemplate.ShowText((writer.PageNumber - 1).ToString());
            objTemplate.EndText();
        }

        #region Unimplemented Interface Memebers

        /**
         * Called when a page is initialized.
         * <P>
         * Note that if even if a page is not written this method is still
         * called. It is preferable to use <CODE>onEndPage</CODE> to avoid
         * infinite loops.
         *
         * @param writer the <CODE>PdfWriter</CODE> for this document
         * @param document the document
         */
        void IPdfPageEvent.OnStartPage(PdfWriter writer, Document document)
        {
        }

        /**
        * Called when a Paragraph is written.
        * <P>
        * <CODE>paragraphPosition</CODE> will hold the height at which the
        * paragraph will be written to. This is useful to insert bookmarks with
        * more control.
        *
        * @param writer the <CODE>PdfWriter</CODE> for this document
        * @param document the document
        * @param paragraphPosition the position the paragraph will be written to
        */
        void IPdfPageEvent.OnParagraph(PdfWriter writer, Document document, 
float paragraphPosition)
        {

        }

        /**
         * Called when a Paragraph is written.
         * <P>
         * <CODE>paragraphPosition</CODE> will hold the height of the end of 
the paragraph.
         *
         * @param writer the <CODE>PdfWriter</CODE> for this document
         * @param document the document
         * @param paragraphPosition the position of the end of the paragraph
         */
        void IPdfPageEvent.OnParagraphEnd(PdfWriter writer, Document document, 
float paragraphPosition)
        {

        }

        /**
         * Called when a Chapter is written.
         * <P>
         * <CODE>position</CODE> will hold the height at which the
         * chapter will be written to.
         *
         * @param writer            the <CODE>PdfWriter</CODE> for this document
         * @param document          the document
         * @param paragraphPosition the position the chapter will be written to
         * @param title             the title of the Chapter
         */
        void IPdfPageEvent.OnChapter(PdfWriter writer, Document document, float 
paragraphPosition, Paragraph title)
        {

        }

        /**
        * Called when the end of a Chapter is reached.
        * <P>
        * <CODE>position</CODE> will hold the height of the end of the chapter.
        *
        * @param writer            the <CODE>PdfWriter</CODE> for this document
        * @param document          the document
        * @param paragraphPosition the position the chapter will be written to
        */
        void IPdfPageEvent.OnChapterEnd(PdfWriter writer, Document document, 
float paragraphPosition)
        {

        }

        /**
         * Called when a Section is written.
         * <P>
         * <CODE>position</CODE> will hold the height at which the
         * section will be written to.
         *
         * @param writer            the <CODE>PdfWriter</CODE> for this document
         * @param document          the document
         * @param paragraphPosition the position the chapter will be written to
         * @param title             the title of the Chapter
         */
        void IPdfPageEvent.OnSection(PdfWriter writer, Document document, float 
paragraphPosition, int depth, Paragraph title)
        {
        }

        /**
         * Called when the end of a Section is reached.
         * <P>
         * <CODE>position</CODE> will hold the height of the section end.
         *
         * @param writer            the <CODE>PdfWriter</CODE> for this document
         * @param document          the document
         * @param paragraphPosition the position the chapter will be written to
         */
        void IPdfPageEvent.OnSectionEnd(PdfWriter writer, Document document, 
float paragraphPosition)
        {

        }

        /**
         * Called when a <CODE>Chunk</CODE> with a generic tag is written.
         * <P>
         * It is usefull to pinpoint the <CODE>Chunk</CODE> location to generate
         * bookmarks, for example.
         *
         * @param writer the <CODE>PdfWriter</CODE> for this document
         * @param document the document
         * @param rect the <CODE>Rectangle</CODE> containing the 
<CODE>Chunk</CODE>
         * @param text the text of the tag
         */
        void IPdfPageEvent.OnGenericTag(PdfWriter writer, Document document, 
Rectangle rect, string text)
        {
        }

        #endregion

        #endregion


        private void SetFooter(PdfWriter oWriter, Document oDocument)
        {
            int intPageNumber = oWriter.PageNumber;
            string strText = "Page " + intPageNumber + " of ";
            BaseFont objBaseFont = 
BaseFont.CreateFont("C:/windows/fonts/Arial.ttf", BaseFont.WINANSI, 
BaseFont.EMBEDDED);
            Font objFont = new Font(objBaseFont, 10f);
            float fltLen = objFont.BaseFont.GetWidthPoint(strText, 
objFont.Size);
            objCB.BeginText();
            objCB.SetFontAndSize(objFont.BaseFont, objFont.Size);
            objCB.SetTextMatrix((oDocument.PageSize.Width / 2) - (fltLen / 2), 
5f);
            objCB.ShowText(strText);
            objCB.EndText();
            objCB.AddTemplate(objTemplate, (oDocument.PageSize.Width / 2) - 
(fltLen / 2) + fltLen, 5f);
        }
    }

    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PdfPageEvent objMyEvent = new PdfPageEvent();

            #region Prepare Document for Writing

            // need to write to memory first due to IE wanting
            // to know the length of the pdf beforehand
            MemoryStream m = new MemoryStream();
            Document objDocument = new Document(PageSize.LETTER, 20f, 20f, 35f, 
33f);
            //we set the ContentType and create an instance of the Writer
            Response.ContentType = "application/pdf";
            PdfWriter objWriter = PdfWriter.GetInstance(objDocument, m);
            objWriter.PageEvent = objMyEvent;
            objDocument.Open();

            #endregion

            PdfPTable objTable = new PdfPTable(1);
            PdfPCell objCell = null;
            objTable.SplitLate = false;
            for (int i = 0; i < 4; i++)
            {
                objCell = new PdfPCell(new Phrase("Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. Phasellus non turpis eu nulla dignissim bibendum a 
vel libero. Maecenas feugiat justo in nibh gravida adipiscing. Cras ut augue 
nibh. Quisque posuere, tortor facilisis rutrum dignissim, nisi nunc tincidunt 
elit, id auctor quam libero eu odio. Vestibulum lacinia aliquet egestas. 
Phasellus sed enim nibh. Curabitur at sapien nisl. Fusce sed leo sed massa 
lacinia tempor. Suspendisse dictum urna sed elit blandit bibendum. Duis lacus 
neque, facilisis sit amet mattis nec, aliquam id velit. Vivamus elementum, 
risus non accumsan interdum, nulla arcu pretium nibh, ac iaculis ligula ipsum 
id nisi. Maecenas sodales, urna ac pharetra suscipit, est lacus euismod urna, 
ac tristique lorem risus eu augue. Pellentesque suscipit elit non ligula 
fermentum sollicitudin. Cras suscipit turpis sed sapien tincidunt in aliquet 
tellus tempus.  Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Phasellus non turpis eu nulla dignissim bibendum a vel libero. Maecenas feugiat 
justo in nibh gravida adipiscing. Cras ut augue nibh. Quisque posuere, tortor 
facilisis rutrum dignissim, nisi nunc tincidunt elit, id auctor quam libero eu 
odio. Vestibulum lacinia aliquet egestas. Phasellus sed enim nibh. Curabitur at 
sapien nisl. Fusce sed leo sed massa lacinia tempor. Suspendisse dictum urna 
sed elit blandit bibendum. Duis lacus neque, facilisis sit amet mattis nec, 
aliquam id velit. Vivamus elementum, risus non accumsan interdum, nulla arcu 
pretium nibh, ac iaculis ligula ipsum id nisi. Maecenas sodales, urna ac 
pharetra suscipit, est lacus euismod urna, ac tristique lorem risus eu augue. 
Pellentesque suscipit elit non ligula fermentum sollicitudin. Cras suscipit 
turpis sed sapien tincidunt in aliquet tellus tempus.", 
FontFactory.GetFont("Arial", 10f)));
                objTable.AddCell(objCell);
            }

            objDocument.Add(objTable);

            #region Clean Up Document

            // Close document
            objDocument.Close();

            // Write pdf bytes to outputstream
            Response.OutputStream.Write(m.GetBuffer(), 0, m.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();

            #endregion

        }
    }

Thanks, 
Brian

-----Original Message-----

Date: Thu, 12 Nov 2009 17:40:46 -0000
From: "Paulo Soares" <psoa...@glintt.com>
Subject: Re: [iText-questions] Small problem with PdfPCell spanning
        more    thanone page
To: "Post all your questions about iText here"
        <itext-questions@lists.sourceforge.net>
Message-ID: <009001ca63bf$466b9740$587ba...@psoaresw>
Content-Type: text/plain; format=flowed; charset="iso-8859-1";
        reply-type=original

It may be a rounding bug. Please post the complete code required to generate 
the PDF ([Shortened for brevity] is not very useful and the devil is always 
in the details).

Paulo

----- Original Message ----- 
From: "Brian Lovett" <brianlov...@gamestop.com>
To: <itext-questions@lists.sourceforge.net>
Sent: Thursday, November 12, 2009 5:24 PM
Subject: [iText-questions] Small problem with PdfPCell spanning more thanone 
page


Hey Guys,

I am using iTextSharp 4.1.6.0.  I noticed that when a PdfPCell contains text 
that forces it to span multiple pages, if the final line of text before the 
page break falls in just the right place, that line will be repeated on the 
next page (generic sample attached).  I worked around it by adjusting the 
top and bottom margin by a point or two.

The meat of the code I used to create the attachment is below.  I am unsure 
if the problem is with my code, iText, Reader, or some combination as I was 
unable to find anything in the documentation or searches.

            MemoryStream m = new MemoryStream();
            Document objDocument = new Document(PageSize.LETTER, 20f, 20f, 
35f, 33f);
            Response.ContentType = "application/pdf";
            PdfWriter objWriter = PdfWriter.GetInstance(objDocument, m);
            objWriter.PageEvent = objMyEvent;
            objDocument.Open();
            PdfPTable objTable = new PdfPTable(1);
            PdfPCell objCell = null;
            objTable.SplitLate = false;
            for (int i = 0; i < 4; i++)
            {
                objCell = new PdfPCell(new Phrase("Lorem ipsum dolor sit 
amet [Shortened for brevity]", FontFactory.GetFont("Arial", 10f)));
                objTable.AddCell(objCell);
            }
            objDocument.Add(objTable);
            objDocument.Close();
            Response.OutputStream.Write(m.GetBuffer(), 0, 
m.GetBuffer().Length);
            Response.OutputStream.Flush();
            Response.OutputStream.Close();
Thanks,
Brian







------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day 
trial. Simplify your report design, integration and deployment - and focus on 
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to