Reputation: 39
my Method is
private void _fillText(string field, List<TextPart> parts, int alignment)
{
//fieldPosition: page, llx, lly, urx, ury
float[] fieldPosition = _pdfStamper.AcroFields.GetFieldPositions(field);
PdfContentByte pdfContentByte = _pdfStamper.GetOverContent(fieldPosition[0]);
foreach (TextPart tp in parts)
{
ColumnText myColumn = new ColumnText(pdfContentByte);
myColumn.RunDirection = _runDirection;
//if (_runDirection == PdfWriter.RUN_DIRECTION_RTL)
// tp.Font = _arialBlackFont12;
myColumn.SetSimpleColumn(Convert.ToInt32(fieldPosition[1]) + tp.LeftMargin
, fieldPosition[2]
, fieldPosition[3] - tp.RightMargin
, fieldPosition[4] - tp.TopMargin);
Phrase myPhrase = new Phrase(new Chunk(tp.Text));
//myPhrase.Font = font;
Paragraph myParagraph = new Paragraph();
myParagraph.Font = tp.Font;
myParagraph.Font.SetStyle(tp.FontStyle);
if (tp.Font == _lucidaSansGray60Font9)
myParagraph.Leading = 10;
if (tp.Leading != null)
myParagraph.Leading = tp.Leading.Value;
myParagraph.Alignment = alignment;
myParagraph.Add(myPhrase);
myColumn.AddElement(myParagraph);
myColumn.Go();
}
}
At line
float[] fieldPosition = _pdfStamper.AcroFields.GetFieldPositions(field);
I got error cannot implicitly convert type system.collections.generic.IList<Itextsharp.text.pdf.acrofields.fieldposition> to float array
Somebody tell me how to convert. I have all my logic based on the conversion.
Upvotes: 0
Views: 784
Reputation: 95928
You seem to upgrade from some ancient iTextSharp version in which AcroFields.GetFieldPositions
returned a list of 5 numbers per field visualization, page, llx, lly, urx, ury.
This meanwhile has been changed to a more structured design, you now get one FieldPosition instance which has an int
page member and a Rectangle
member.
You can of course convert this back to the old structure as proposed by Sepehr Farshid in his comments and attempted by LVBean in his new answer. For the sake of better source code quality, though, you might consider using the new FieldPosition
object.
This would amount to something like (converted only in editor, minor errors might persist):
FieldPosition fieldPosition = _pdfStamper.AcroFields.GetFieldPositions(field)[0];
PdfContentByte pdfContentByte = _pdfStamper.GetOverContent(fieldPosition.page);
foreach (TextPart tp in parts)
{
ColumnText myColumn = new ColumnText(pdfContentByte);
myColumn.RunDirection = _runDirection;
myColumn.SetSimpleColumn(Convert.ToInt32(fieldPosition.position.Left) + tp.LeftMargin
, fieldPosition.position.Bottom
, fieldPosition.position.Right - tp.RightMargin
, fieldPosition.position.Top - tp.TopMargin);
Upvotes: 2
Reputation: 2061
float[] fieldPosition = _pdfStamper.AcroFields.GetFieldPositions(field)
.SelectMany(fp => new[] { fp.position.X, fp.position.Y, fp.position.Width, fp.position.Height })
.ToList();
Upvotes: 0