如何找出页面上当前的光标位置? [英] How to find out the current cursor position on a page?

查看:175
本文介绍了如何找出页面上当前的光标位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在页面中添加了一堆东西,例如 document.add(p); ,然后我添加了一个使用文档填充两列的表这里。在这个过程中,我使用:

I add a bunch of stuff to a page, such as document.add(p);, then I add a table that fills two columns using the documentation here. In this process, I use:

column.setSimpleColumn(
   x[count][0], document.bottom(),
   x[count][1], document.top() - height - 10);

其中height设置为0.这会将表放在从顶部开始的列中文献。当然,我想要做的是在写入文档的最后一个内容的正下方添加列(例如 document.add(p); )。如果我知道页面上到目前为止消耗的高度,我可以手动输入正确的 height 值,并且工作正常。

where height is set to 0. This places the table in a column that starts at the top of the document. Of course, what I'd like to do is to add the column directly below the last thing written to the document (e.g. document.add(p);). If I knew the height consumed thus far on the page, I could manually enter the correct height value and things work fine.

问题是,文档中写的内容取决于很多条件,所以我想简单地执行命令告诉我已经有多少高度到目前为止在文档中消耗(对于当前页面)所以我可以适当地设置 height 。有没有办法在iText中执行此操作(这样我就不需要跟踪添加到页面中的每个元素的高度)?

The problem is, what gets written in the document depends on a lot of conditions, so I'd like to simply execute a command to tell me how much height has been consumed thus far in the document (for the current page) so I can set height appropriately. Is there a way to do this in iText (so that I don't need to keep track of the heights of each individual element added to the page)?

推荐答案

有三种方法可以向PDF添加内容:

There are three ways to add content to a PDF:

使用document.add();

在这种情况下,iText根据您定义的页面大小和边距决定添加内容的位置。

In this case, iText decides where the content is added, based on the page size and the margins you defined.

有一个名为 getVerticalPosition()的非常古老的方法,它将 boolean 作为参数。我并不为这种方法感到自豪,因为它是一个 getter 方法,它不仅会在使用一系列 document.add()之后在页面上给你Y位置。 / code>操作,它也可以改变当前的Y位置(这不是 getter 方法应该做的)。

There is a very old method called getVerticalPosition() that takes a boolean as parameter. I'm not proud of that method as it is a getter method that not only will give you the Y position on the page after using a series of document.add() operations, it can also change the current Y position (and that is not what a getter method should do).

如果您将 false 作为参数传递,它将为您提供当前的Y,这可能是最后一个块的基线位置你已添加。

If you pass false as a parameter, it will give you the current Y, which could be the position of the baseline of the last Chunk you've added.

如果你传递 true 作为参数,该方法将首先添加换行符并为您提供要添加的下一行文本基线的位置。

If you pass true as a parameter, the method will first add a newline and give you the position of the baseline of the "next" line of text you'll be adding.

使用ColumnText

ColumnText是一个可以添加 Elements 的对象。然后,您可以使用绝对坐标定义列,并调用 go()方法。 go()方法将呈现内容,您可以使用返回值来查看内容是否符合矩形。您可以使用 getYLine()方法查找已添加的最后一条内容的Y坐标。

ColumnText is an object to which you can add Elements. You can then define a column using absolute coordinates and invoke the go() method. The go() method will render the content and you can use the return value to see if the content fitted the rectangle. You can use the getYLine() method to find the Y coordinate of the last piece of content that was added.

使用writeSelectedRows()

此方法仅在使用 PdfPTable 时有效对象。
writeSelectedRows()方法可以向页面添加一系列行和列,从特定的 x 和 y 坐标。
它返回一个 float 值,它给出了添加的最后一行的下边框的Y位置。
如果你想知道在使用 writeSelectedRows()
添加表之前表需要多少空间,你需要计算表的高度要首先添加的行。

This method will only work when you work with PdfPTable objects. The writeSelectedRows() method can add a selection of rows and columns to a page starting at a specific x and y coordinate. It returns a float value that gives you the Y-position of the bottom border of the final row that was added. If you want to know how much space the table needs vertically before adding the table with writeSelectedRows(), you need to calculate the height of the rows you want to add first.

这篇关于如何找出页面上当前的光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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