iTextSharp - 在添加到列之前计算短语/段落高度 [英] iTextSharp - calculating phrase/paragraph height before adding to column
问题描述
我正在创建具有不同数量的项目和备注的PDF工作订单。我使用 ColumnText.SetSimpleColumn(短语,x,y,x2,y2,高度,对齐)
将文本添加到单个列。
I am creating PDF work orders that have a varying amount of items and notes. I am using ColumnText.SetSimpleColumn(phrase, x, y, x2, y2, height, alignment)
to add text to a single column.
我可以获取ColumnText.YLine值,但只有 将应用于 Go后的ColumnText对象( )
方法。
I can get the ColumnText.YLine value, but only after applying it to the ColumnText object with the Go()
method.
我想做的是预先计算最终的YLine值,或者如果YLine则撤消添加短语超过mjy底部阈值,所以我可以手动添加另一个页面并继续。这可能吗?
What I want to do is to either pre-calculate the final YLine value or undo the adding of a phrase if the YLine is past mjy bottom threshold so I can manually add another page and continue. Is this possible?
推荐答案
正如我之前发表的评论所示,你的问题的答案是肯定的。 MovieColumns3 向您展示了如何操作:
As indicated in the comment I posted earlier, the answer to your question is yes. MovieColumns3 shows you how to do it:
您需要创建 ColumnText
对象而不是使用静态 ColumnText
方法,并使用该对象上的 setSimpleColumn()
方法。您可以使用文本模式使用 addText()
方法添加 Phrase
对象,或者在复合模式中使用 addElement()
方法添加任何类型的元素
。
Instead of using a static ColumnText
method, you need to create a ColumnText
object and use the setSimpleColumn()
method on that object. You can work in text mode adding Phrase
objects with the addText()
method, or in composite mode adding any type of Element
using the addElement()
method.
通常,你会调用 go()
方法来渲染这些元素,但这就是你的全部观点。问题:在渲染任何元素之前,您想知道元素拟合。可以在模拟模式下使用 go()
方法: go(true)
。这将假装添加内容,但不会呈现任何内容。您可以使用 getYLine()
方法获取Y位置,并使用 hasMoreText()
方法。
Normally, you'd then invoke the go()
method to render those elements, but that's the whole point of your question: before you render any element, you want to know of the element fits. That can be done to use the go()
method in simulation mode: go(true)
. This will pretend to add the content, but won't render anything. You can get the Y position using the getYLine()
method and check if all the content was added using the hasMoreText()
method.
重要的是要理解 go()
方法(部分)消耗 ColumnText
对象的内容。一旦您决定添加真实内容(即:不在模拟模式下),无论是在当前页面还是下一页,您需要丢弃 ColumnText $ c $中的所有剩余内容c>对象并重新添加。
It's important to understand that the go()
method (partially) consumes the content of the ColumnText
object. Once you decide to add the content for real (that is: not in simulation mode) be it on the current page or the next page, you need to discard all remaining content from the ColumnText
object and add it anew.
您可以在我的书籍如果您不认识示例中的方法,因为它是用Java编写的,请与 C#端口示例。
You'll find a more detailed explanation in my book and if you don't recognize the methods in the example because it's written in Java, please compare with the C# port of the example.
这篇关于iTextSharp - 在添加到列之前计算短语/段落高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!