【问题标题】:Main content is overlapping on footer section PDF document iTextSharp主要内容在页脚部分重叠 PDF 文档 iTextSharp
【发布时间】:2014-01-08 12:37:34
【问题描述】:

我正在使用 itextsharp 创建文档。我在文档中创建了一个页脚部分,但页脚顶部的内容重叠。

我该如何解决这个问题?

我想在每个页面上添加页码,我该怎么做?

请检查此链接 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);

        }
    }
}

【问题讨论】:

    标签: c# pdf-generation itextsharp


    【解决方案1】:

    简而言之:

    在

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

    您告诉 iText 几乎不留任何边距(例如,用于页脚行)。因此,您的页脚并不奇怪

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

    每隔一段时间就会与内容重叠。

    所以只需调整页边距(尤其是底部页边距)和页脚位置,使其不相互重叠。

    更详细:

    您使用的 Document 构造函数记录为

    /// <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) {
    

    因此,您的new Document(PageSize.A4, 10, 10, 10, 10) 指示 iText 创建 A4 大小的文档,并在填充内容时在每个方向仅留下 10 磅的可用边距空间。 10分不算多。

    你使用的PdfPTable的写表方法记录为

    /**
     * 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)
    

    因此,您的代码

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

    在坐标 (15, 110) 处开始绘制表格 --- 这里的原点 (0, 0) 是文档的左下角。

    因此,显然,页面内容区域和页脚区域在条带 10 Document 构造函数中的最后一个参数)并减少表格的 y 位置绘图调用不再重叠。

    【讨论】:

    • 对不起,我正在使用这个库,所以我无法理解你的回复。请解释。谢谢
    • 我添加了一些细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2018-06-05
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2018-09-10
    相关资源
    最近更新 更多