How to get vertical cursor position when writing d

2020-02-15 07:35发布

问题:

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();
}
}

回答1:

The link Jon Reilly posted in the comment, which goes here, is very useful to figuring this out:

You will need a reference to the the element whose vertical position you need and add it:

Paragraph p = new Paragraph("Hello World");
doc.add(p);

You can then get the IRenderer of the element as follows, this then allows you to get the LayoutResult, which has lots of useful information about the rendering of the element in the PDFDocument, including the bounding box, with the full coordinates, width, height, etc. which should be strictly more powerful than the old iText5 getVerticalPosition.

IRenderer pRenderer = p.createRendererSubTree().setParent(doc.getRenderer());
LayoutResult pLayoutResult = pRenderer.layout(new LayoutContext(new LayoutArea(0, new Rectangle(595-72, 842-72))));

float y = pLayoutResult.getOccupiedArea().getBBox().getY();
float x = pLayoutResult.getOccupiedArea().getBBox().getX();


回答2:

I think asking document renderer about the top of its current layout area is a simpler way to obtain this information.

Paragraph p = new Paragraph("Hello World");
doc.add(p);
float position = doc.getRenderer().getCurrentArea().getBBox().getTop();

I know too little about the API to be aware of the downsides of this method. It looks like it's working, and for me it's enough.