在iText 7中编写文档时如何获得垂直光标位置? [英] How to get vertical cursor position when writing document in iText 7?

查看:648
本文介绍了在iText 7中编写文档时如何获得垂直光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在iText 5中有一个名为 getVerticalPosition()的方法,该方法在页面上为下一个写入的对象提供位置。作为回答这个问题如何找出当前的问题页面上的光标位置?和记录的文件这里

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

iText 7在写入文档的页面上获取当前垂直位置的等价物是什么?

What is the equivalent for iText 7 to get the current vertical position on the page for writing the document?

更新日期:08-11-2018:根据评论中的回复,我更新了添加页面preak或新页面的逻辑,但两者仍在打印同一页

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


推荐答案

链接Jon Reilly在评论中发布了这里对于解决这个问题非常有用:

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

然后您可以按如下方式获取元素的IRenderer,这样您就可以获得LayoutResult,它有很多关于PDFDocument中元素渲染的有用信息,包括边界框,有完整的坐标,宽度,高度等,它们应该比旧的iText5 getVerticalPosition <更强大/ code>。

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

这篇关于在iText 7中编写文档时如何获得垂直光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆