Shoaib Ijaz
Shoaib Ijaz

Reputation: 5587

Main content is overlapping on footer section PDF document iTextSharp

i am using itextsharp for creating document. I have created a footer section in document but content that over the top of footer is overlapping.

How can i fix this?

I want to add page number on each page how can i do this?

Please check this link Pdf document Image

 public class PdfNote
 {
    public int Create(int id, string path)
    {
        try
        {
            var file = path;

            if (System.IO.File.Exists(file))
                System.IO.File.Delete(file);

            Document document = new Document(PageSize.A4, 10, 10, 10, 10);

            var writer = iTextSharp.text.pdf.PdfWriter.GetInstance(document, new System.IO.FileStream(file, FileMode.Create));
            document.Open();

            writer.PageEvent = new Footer();

            // main content

            document.Close();

            return 1;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    public partial class Footer : PdfPageEventHelper
    {
        public override void OnEndPage(PdfWriter writer, Document doc)
        {
            PdfPTable fTbl = new PdfPTable(1);
            fTbl.TotalWidth = 550;
            fTbl.DefaultCell.Border = 0;

            PdfPTable line = new PdfPTable(1);
            line.DefaultCell.Border = 0;
            line.DefaultCell.BorderWidthBottom = 0.2f;
            line.DefaultCell.Padding = 5;
            line.AddCell("");

            fTbl.AddCell(line);

            PdfPTable footerTbl = new PdfPTable(3);
            footerTbl.TotalWidth = 550;
            footerTbl.DefaultCell.Column.Alignment = 1;
            footerTbl.HorizontalAlignment = Element.ALIGN_CENTER;
            footerTbl.DefaultCell.Border = 0;

            footerTbl.AddCell("Print Name:");
            footerTbl.AddCell("Signature:");
            footerTbl.AddCell("Date:");


            PdfPCell cell = new PdfPCell();
            cell.Padding = 20;
            cell.Border = 0;
            cell.BorderWidthBottom = 1;

            footerTbl.AddCell(cell);
            footerTbl.AddCell(cell);
            footerTbl.AddCell(cell);

            fTbl.AddCell(footerTbl);
            fTbl.AddCell(line);

            fTbl.WriteSelectedRows(0, -1, 15, 110, writer.DirectContent);

        }
    }
}

Upvotes: 1

Views: 5129

Answers (1)

mkl
mkl

Reputation: 95908

In a nutshell:

In

Document document = new Document(PageSize.A4, 10, 10, 10, 10);

you tell iText to hardly leave any margin (e.g. for use for footer lines) at all. Thus, it is not surprising that your footer

fTbl.WriteSelectedRows(0, -1, 15, 110, writer.DirectContent);

every so often overlaps the content.

So simply adjust the margins (especially the bottom one) and the footer position to not overlap each other.

In more detail:

The Document constructor you use is documented as

/// <summary>
/// Constructs a new Document-object.
/// </summary>
/// <param name="pageSize">the pageSize</param>
/// <param name="marginLeft">the margin on the left</param>
/// <param name="marginRight">the margin on the right</param>
/// <param name="marginTop">the margin on the top</param>
/// <param name="marginBottom">the margin on the bottom</param>
public Document(Rectangle pageSize, float marginLeft, float marginRight, float marginTop, float marginBottom) {

Thus your new Document(PageSize.A4, 10, 10, 10, 10) instructs iText to create a document in A4 size and to merely leave 10 points free margin space in every direction when filling it with content. 10 points ain't much.

The table writing method of PdfPTable you use is documented as

/**
 * Writes the selected rows to the document.
 *
 * @param rowStart the first row to be written, zero index
 * @param rowEnd   the last row to be written + 1. If it is -1 all the
 *                 rows to the end are written
 * @param xPos     the x write coordinate
 * @param yPos     the y write coordinate
 * @param canvas   the <CODE>PdfContentByte</CODE> where the rows will
 *                 be written to
 * @return the y coordinate position of the bottom of the last row
 */
public float WriteSelectedRows(int rowStart, int rowEnd, float xPos, float yPos, PdfContentByte canvas)

Thus, your code

fTbl.WriteSelectedRows(0, -1, 15, 110, writer.DirectContent);

starts drawing the table at the coordinate (15, 110) --- the origin (0, 0) here is the bottom left corner of your document.

Thus, obviously, page content area and footer area overlap in the stripe 10 <= y <= 110. You should increase the bottom margin (the last parameter in your Document constructor) and decrease the y position of the table drawing call to not overlap anymore.

Upvotes: 8

Related Questions