gpt4 book ai didi

itextsharp 将 Pdfptable 放置在所需位置

转载 作者:行者123 更新时间:2023-12-03 15:02:41 26 4
gpt4 key购买 nike

标题内容和 Pdftable 重叠

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

使用 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);
}

在我的主要类(class)中,我这样做
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);

结果是 Header Content 和 pdftable 重叠

最佳答案

itext docs 中所述——

To avoid having the cell border and the content overlap, if you are having thick cell borders, call the setUserBorderPadding(true), like this:


cell.setUserBorderPadding(true);

关于itextsharp 将 Pdfptable 放置在所需位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9321867/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com