iTextSharp GetFieldPositions to SetSimpleColumn

前端 未结 1 1329
梦如初夏
梦如初夏 2020-12-20 00:28

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

I am trying to use ColumnText.SetSimpleColumn after getting the p

相关标签:
1条回答
  • 2020-12-20 01:31

    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.

    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];
    }
    

    Should be converted to:

    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;
    }
    
    0 讨论(0)
提交回复
热议问题