In iText 5 there is a method named getVerticalPosition()
which gives the position on the page for the next object written. As answers this question How to find out the current cursor position on a page? and which is documented here
What is the equivalent for iText 7 to get the current vertical position on the page for writing the document?
UPDATE DATE: 08-11-2018: As per the response in the comment I have updated the logic of adding a page preak or a new page but both are still printing on the same page
foreach (var element in RenderHtmlAndCss(document, css, html))
{
AddElement(document, null, (IBlockElement)element);
IRenderer pRenderer = element.CreateRendererSubTree().SetParent(document.GetRenderer());
LayoutResult pLayoutResult = pRenderer.Layout(new LayoutContext(new LayoutArea(0, new Rectangle(pdf.GetDefaultPageSize().GetHeight() - 72, pdf.GetDefaultPageSize().GetWidth() - 72))));
// writer.GetCurrentPos();
float y = pLayoutResult.GetOccupiedArea().GetBBox().GetY();
//20 is height of the content.
if(y<20 && !string.IsNullOrEmpty(LastPageStaticContent))
{
AreaBreak newpage = new AreaBreak(AreaBreakType.NEXT_PAGE);
//pdf.AddNewPage();
}
}
// Add Preface
if (Preface != null && Preface.Count > 0)
{
foreach (ReportSection section in Preface)
{
for (int i = 1; i <= pdf.GetNumberOfPages(); i++)
{
if (i == pdf.GetPageNumber(pdf.GetLastPage()))
{
foreach (var element in RenderHtmlAndCss(document, css, LastPageStaticContent))
{
//float x = pLayoutResult.getOccupiedArea().getBBox().getX();
IBlockElement glueToBottom = new Paragraph().Add((IBlockElement)element)
.SetFontSize(12)
.SetWidth(UnitValue.CreatePercentValue(100))
// .SetBackgroundColor(ColorConstants.RED)
.SetTextAlignment(TextAlignment.JUSTIFIED);
glueToBottom.SetProperty(Property.POSITION, iText.Layout.Layout.LayoutPosition.ABSOLUTE);
glueToBottom.SetProperty(Property.BOTTOM, 0);
// glueToBottom.Add(element);
document.Add(glueToBottom);
}
}
}
// document.Close();
}
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…