Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
717 views
in Technique[技术] by (71.8m points)

itext - Why is my iTextSharp PDF textbox so large?

I am using the following code to add a textbox to my PDF file:

Dim stamper As PdfStamper = New PdfStamper(New PdfReader(sInputFile), File.Create(sOutputFile))

Dim iPageNumer As Integer = 1

Dim tf As TextField
tf = New TextField(stamper.Writer, New iTextSharp.text.Rectangle(33, 780, 30, 28), "SomeName") 

Dim bf = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, False)
With tf
    .Alignment = Element.ALIGN_CENTER And Element.ALIGN_MIDDLE
    .BackgroundColor = GrayColor.GRAYBLACK
    .BorderColor = Color.GREEN
    .BorderStyle = PdfBorderDictionary.STYLE_SOLID
    .DefaultText = "This is a new text field."
    .Font = bf
    .FontSize = 7
    .MaxCharacterLength = 25
    .Options = TextField.REQUIRED Or TextField.MULTILINE
    .Text = "This is the assigned value."
End With
stamper.AddAnnotation(tf.GetTextField(), iPageNumer)
stamper.Close()

This creates a textbox which is almost as high as the document. However, I expect the textbox to be 30, 28 only.

I guess Y = 780 is correct because iTextSharp starts at bottom left.

What am I not seeing here?

Thank you!

This is what it looks like: Its width is 5 pixels only, and its height is 806 pixels.

enter image description here

question from:https://stackoverflow.com/questions/65851383/why-is-my-itextsharp-pdf-textbox-so-large

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Stupid coordinate system requires the following arguments:

Dim iNewY As Integer = uDocumentSize.Y - iY

Dim iLowerLeftX As Integer = iX
Dim iLowerLeftY As Integer = iNewY
Dim iUpperRightX As Integer = iX + yourtextboxwidth
Dim iUpperRightY As Integer = iNewY + yourtextboxheight

New iTextSharp.text.Rectangle(iLowerLeftX, iLowerLeftY, iUpperRightX, iUpperRightY)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...