本文介绍了itextsharp将Pdfptable放置在所需位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I place PdfPTable at any position in the pdf page using (x, y) positioning like (100, 200) or (15, 100) at any place on the pdf page?

使用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重叠

推荐答案

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

you have calculated the y position for your header table in onload() like document.PageSize.Height-10.

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

please set some calculated value instead of pageSize.GetTop(50) in the second table also.

这篇关于itextsharp将Pdfptable放置在所需位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-08 01:54