iTextSharp的GetFieldPositions到SetSimpleColumniTextSharp、GetFieldPositions、SetSimpleColumn

2023-09-03 04:17:45 作者:权者为王

我使用iTextSharp的最新版本在这里找到: http://sourceforge.net/projects/itextsharp /

I'm using the latest version of iTextSharp found here: http://sourceforge.net/projects/itextsharp/

我试图让使用GetFieldPositions一些AcroFields的(字段名)的位置后使用ColumnText.SetSimpleColumn。

I am trying to use ColumnText.SetSimpleColumn after getting the position of some AcroFields using GetFieldPositions( fieldName ).

所有的例子我能找到演出GetFieldPositions返回一个float []然而,这似乎并没有这样的情况了。现在看来要返回的IList这不(根据到Visual Studio)隐式转换为float []。

All the examples I can find show GetFieldPositions returning a float[] however this doesn't appear to be the case anymore. It now appears to be returning IList which doesn't (according to Visual Studio) implicitly convert to a float[].

在0指数在返回值是一个定位构件是一个长方形,但由于例子我已经看到了返回的浮动进行数学运算[]我不知道从GetFieldPostions的返回值是什么值使用SetSimpleColumn时使用。这里有一个是我引用的文章: HTTP ://blog.dmbcllc.com/2009/07/08/itextsharp-html-to-pdf-positioning-text/

Inside the return value at the 0 index is a position member that is a Rectangle, but since the examples I've seen perform math operations on the returned float[] I'm not sure what values from the return value in GetFieldPostions to use when using SetSimpleColumn. Here's one article that I'm referencing: http://blog.dmbcllc.com/2009/07/08/itextsharp-html-to-pdf-positioning-text/

最简单的接受的答案将是如何从GetFieldPositions价值转化为SetSimpleColumn。

Simplest accepted answer will be how to translate the value from GetFieldPositions to SetSimpleColumn.

谢谢!

推荐答案

我觉得这是做有两个原因。 1), GetFieldPositions()实际上可能返回多个项目,因为你可以在技术上有多个字段具有相同的名称和2),原数组方法需要知道幻阵数字找什么东西。所有code,你看到了pretty的很多假设 GetFieldPositions()只返回一个单一的项目,这是真正的99%的时间。相反,与索引工作,你现在可以正常的工作性能。

I think this was done for two reasons. 1), GetFieldPositions() could actually return multiple items because you can technically have more than one field with the same name and 2), the original array method required knowing "magic array numbers" to find what was what. All of the code that you saw pretty much assumed that GetFieldPositions() only returned a single item, which is true 99% of the time. Instead of working with indexes you can now work with normal properties.

所以从您发布的链接code:

So the code from the link that you posted:

float[] fieldPosition = null;
fieldPosition = fields.GetFieldPositions("fieldNameInThePDF");
left = fieldPosition[1];
right = fieldPosition[3];
top = fieldPosition[4];
bottom = fieldPosition[2];
if (rotation == 90)
{
    left = fieldPosition[2];
    right = fieldPosition[4];
    top = pageSize.Right - fieldPosition[1];
    bottom = pageSize.Right - fieldPosition[3];
}

应转换为:

IList<AcroFields.FieldPosition> fieldPositions = fields.GetFieldPositions("fieldNameInThePDF");
if (fieldPositions == null || fieldPositions.Count <= 0) throw new ApplicationException("Error locating field");
AcroFields.FieldPosition fieldPosition = fieldPositions[0];
left = fieldPosition.position.Left;
right = fieldPosition.position.Right;
top = fieldPosition.position.Top;
bottom = fieldPosition.position.Bottom;
if (rotation == 90)
{
    left = fieldPosition.position.Bottom;
    right = fieldPosition.position.Top;
    top = pageSize.Right - fieldPosition.position.Left;
    bottom = pageSize.Right - fieldPosition.position.Right;
}