itextsharp将Pdfptable置于所需位置

时间:2012-02-17 01:53:38

标签: itextsharp

标题内容和Pdftable重叠

如何在pdf页面的任何位置使用(x,y)定位(如100,200)或(15,100)将 PdfPTable放置在pdf页面的任何位置?< / p>

使用PdfEventHelper的标头表

public override void OnEndPage(PdfWriter writer, Document document)
{
    AddHeader(writer, document);
}

public void AddHeader(PdfWriter writer, Document document)
{
    // set no of rows
    PdfPTable headerTable = new PdfPTable(1);
    // set the width
    headerTable.TotalWidth = document.PageSize.Width;
    headerTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER;

    PdfPCell company = new PdfPCell(new Phrase(new Chunk("Name", fontArial)));
    company.HorizontalAlignment = Element.ALIGN_CENTER;
    company.BorderWidth = 0;
    headerTable.AddCell(company);

    PdfPCell report = new PdfPCell(new Phrase(new Chunk("PrintedDate", fontArial)));
    report.HorizontalAlignment = Element.ALIGN_CENTER;
    report.BorderWidth = 0;
    headerTable.AddCell(report);

    headerTable.TotalWidth = document.PageSize.Width - 20;

    // write rows to the pdf output stream
    Rectangle pageSize = document.PageSize;
    headerTable.WriteSelectedRows(0, -1, 0, (document.PageSize.Height - 10), writer.DirectContent);
}

在我的主要班级我这样做

PdfWriter pdfWriter = PdfWriter.GetInstance(document, new FileStream(pdfFile, FileMode.Create));

pdfWriter.PageEvent = page;

document.Open()

接下来我在这里添加一个pdftable

PdfPTable HeaderTable = new PdfPTable(2);
HeaderTable.DefaultCell.VerticalAlignment = Element.ALIGN_MIDDLE;
HeaderTable.TotalWidth = pageSize.Width - 80;
HeaderTable.SetWidthPercentage(new float[] {45, 45}, pageSize);

PdfPCell HeaderLeftCell = new PdfPCell(new Phrase(8, HeaderLeft, HeaderFont));
HeaderLeftCell.Padding = 5;
HeaderLeftCell.PaddingBottom = 8;
HeaderLeftCell.BorderWidthRight = 0;
HeaderTable.AddCell(HeaderLeftCell);

PdfPCell HeaderRightCell = new PdfPCell(new Phrase(8, HeaderRight, HeaderFont));
HeaderRightCell.HorizontalAlignment = Element.ALIGN_RIGHT;
HeaderRightCell.Padding = 5;
HeaderRightCell.PaddingBottom = 8;
HeaderRightCell.BorderWidthLeft = 0;
HeaderTable.AddCell(HeaderRightCell);

HeaderTable.WriteSelectedRows(0, -1, pageSize.GetLeft(40), pageSize.GetTop(50), cb);

结果是标题内容和pdftable重叠

2 个答案:

答案 0 :(得分:0)

正如itext docs -

中所述
  

为避免单元格边框和内容重叠,如果是的话   有厚单元格边框,调用setUserBorderPadding(true),就像   这样:

cell.setUserBorderPadding(true);

答案 1 :(得分:0)

您已在onload() document.PageSize.Height-10中计算了表头的y位置。

请在第二个表格中设置一些计算值,而不是pageSize.GetTop(50)