我是IText7的新手,并且标题重叠,并查看了他们的示例。我创建了一个页脚,现在页脚重叠了。
class Program
{
static string dest = @"HeaderSample.pdf";
public static void Main(string[] args)
{
CreatePdf(dest);
Console.Read();
}
public static void CreatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc, PageSize.LETTER);
TableHeaderEventHandler handler = new TableHeaderEventHandler(doc);
pdfDoc.AddEventHandler(PdfDocumentEvent.START_PAGE, handler);
doc.SetMargins(20 + handler.GetTableHeight(), 36, 36, 36);
TableFooterEventHandler FooterHandler = new TableFooterEventHandler(doc);
pdfDoc.AddEventHandler(PdfDocumentEvent.END_PAGE, FooterHandler);
for (int i = 0; i<150; i++)
{
doc.Add(new Paragraph("Hello World"));
}
doc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
Table table = new Table(1);
Cell contractor = new Cell().Add(new Paragraph("ANOTHER PARA."));
contractor.SetBorder(iText.Layout.Borders.Border.NO_BORDER);
table.AddCell(contractor);
doc.Add(table);
doc.Add(new AreaBreak(iText.Layout.Properties.AreaBreakType.NEXT_PAGE));
doc.Close();
}
}
public class TableHeaderEventHandler : IEventHandler
{
protected Table table;
protected float tableHeight;
protected Document doc;
public TableHeaderEventHandler(Document doc)
{
this.doc = doc;
table = new Table(1);
table.SetWidth(doc.GetPdfDocument().GetDefaultPageSize().GetRight() - doc.GetPdfDocument().GetDefaultPageSize().GetLeft() - doc.GetLeftMargin() - doc.GetRightMargin());
table.AddCell("Header row 1");
table.AddCell("Header row 2");
table.AddCell("Header row 3");
TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
renderer.SetParent(new Document(new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))).GetRenderer());
tableHeight = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.LETTER))).GetOccupiedArea().GetBBox().GetHeight();
}
public void HandleEvent(Event eventi)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent) eventi;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
Rectangle rect = new Rectangle(pdfDoc.GetDefaultPageSize().GetX() + doc.GetLeftMargin(),
pdfDoc.GetDefaultPageSize().GetTop() - doc.GetTopMargin(), 100, GetTableHeight());
new Canvas(canvas, pdfDoc, rect)
.Add(table);
}
public float GetTableHeight()
{
return tableHeight;
}
}
public class TableFooterEventHandler : IEventHandler
{
protected Table table;
protected float tableHeight;
protected Document doc;
public TableFooterEventHandler(Document doc)
{
this.doc = doc;
table = new Table(1);
table.SetWidth(doc.GetPdfDocument().GetDefaultPageSize().GetRight() - doc.GetPdfDocument().GetDefaultPageSize().GetLeft() - doc.GetLeftMargin() - doc.GetRightMargin());
table.AddCell("Footer row 1");
table.AddCell("Footer row 2");
table.AddCell("Footer row 3");
TableRenderer renderer = (TableRenderer)table.CreateRendererSubTree();
renderer.SetParent(new Document(new PdfDocument(new PdfWriter(new ByteArrayOutputStream()))).GetRenderer());
tableHeight = renderer.Layout(new LayoutContext(new LayoutArea(0, PageSize.LETTER))).GetOccupiedArea().GetBBox().GetHeight();
}
public void HandleEvent(Event eventi)
{
PdfDocumentEvent docEvent = (PdfDocumentEvent)eventi;
PdfDocument pdfDoc = docEvent.GetDocument();
PdfPage page = docEvent.GetPage();
PdfCanvas canvas = new PdfCanvas(page.NewContentStreamBefore(), page.GetResources(), pdfDoc);
Rectangle rect = new Rectangle(pdfDoc.GetDefaultPageSize().GetX() + doc.GetLeftMargin(),
pdfDoc.GetDefaultPageSize().GetBottom() - doc.GetBottomMargin(), 100, GetTableHeight() + 50);
new Canvas(canvas, pdfDoc, rect)
.Add(table);
}
public float GetTableHeight()
{
return tableHeight;
}
}
}