Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

pdf generation - How to get vertical cursor position when writing document in iText 7?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...