Setting AcroField Position using iTextSharp


The <strong>iTextSharp</strong> AcroFields class has a GetFieldPosition method.

I am attempting to modify a field's position programmatically. I'm wondering why there is no corresponding SetFieldPosition, or is there another way of changing a field's size or position?


<a href="https://stackoverflow.com/a/7099922/231316" rel="nofollow">See this</a> for part of the reason. There isn't actually a singular GetFieldPosition but instead a plural <a href="http://sourceforge.net/p/itextsharp/code/HEAD/tree/trunk/src/core/iTextSharp/text/pdf/AcroFields.cs#l1566" rel="nofollow">GetFieldPositions</a> since you can technically have multiple fields with the same name. So a theoretical SetFieldPositions would need to take this into account or it might erase some fields.

The other reason is that a field's defining areas aren't always that simple. Take this very simple bit of code:

var tf = new TextField(writer, new iTextSharp.text.Rectangle(100, 400, 500, 500), "name"); writer.AddAnnotation(tf.GetTextField());

It creates a 400x100 text field with corners at 100,400 and 500,500. It <em>also</em> creates an appearance entry /AP with a bounding box of 0,0 and 400,100 giving the normal appearance area that you see before clicking into the field. If you literally just changed the "position" by moving the two corners accordingly you'd be okay but since the position is really just a Rectangle there's nothing stopping you from making it wider or taller. In that case this BBox entry would need to get updated, too. This is a simple case and iText could probably work through it. Check boxes, however, usually have at least two appearance states that need to be altered. There could also be other cases that it might not make sense to propagate these changes through.

If you want to reposition a field, however, it can still be done, but the burden is on you to do it sanely. Imagine a simple PDF with just this single text field:

var tf = new TextField(writer, new iTextSharp.text.Rectangle(100, 400, 500, 500), "name"); tf.Text = "Hi"; tf.BorderColor = BaseColor.BLACK; tf.BorderWidth = 1; writer.AddAnnotation(tf.GetTextField());

You can "move" the text field with a PdfStamper by getting the actual field and adjusting its RECT array. The code below.

using (var fs = new FileStream(testFile, FileMode.Create,FileAccess.Write,FileShare.None)) { using (var reader = new PdfReader(bytes)) { using (var stamper = new PdfStamper(reader, fs)) { //Get our name field var nameField = reader.AcroFields.GetFieldItem("name"); //Grab the first widget inside of it (there could be more) var w = nameField.GetWidget(0); //Grab the bounding array var r = w.GetAsArray(PdfName.RECT); //Check both of the Y values r[1] = new PdfNumber(r.GetAsNumber(1).IntValue - 300); r[3] = new PdfNumber(r.GetAsNumber(3).IntValue - 300); } } }


  • How to get Date from my JSpinner?
  • iTextsharp 7: Rotating a Stamp or Annotation
  • Multiple annotation color
  • jfree chart draw image on a category plot
  • MKMapView overlapping MKAnnotations only allow showing two of the callouts
  • Scalable images in Java
  • How do I get a TextField from AcroFields using iText/Sharp?
  • php imagemagick create a Tiled Pyramid TIFF
  • how to change the div background image in mouse over [duplicate]
  • Error after installing aws-sdk
  • Complicated COUNT query in MySQL
  • Getting coordinates of a component in java
  • Circle movement upon rectangle Collision
  • c# winform DrawToBitmap offscreen
  • crash in __tcf_0
  • How to add closing tag for canvas in three js rendered Canvas?
  • Why people use prototype in javascript when it is easy to inherit using apply () and call () methods
  • Examples of how to a STS in .Net 4.5 using WCF
  • What Makes These Two Array Adds Different?
  • there is no graph with tensorboard
  • CSS bleed-through with cfinput type=“datefield”
  • R Split data.frame using a column that represents and on/off switch
  • Bigquery event streaming and table creation
  • Can I programmatically choose the Android layout folder?
  • How to make JSON.NET deserialize to Microsoft Date Time?
  • Cannot upload to OneDrive using the new SDK
  • Not able to aggregate on nested fields in elasticsearch
  • Unable to install Git-core+svn by MacPorts
  • Django simple Captcha “No module named fields” error
  • Could not find rake using whenever rails
  • Change Inet root folder for iis 7
  • MySQL WHERE-condition in procedure ignored
  • How do you troubleshoot character encoding problems?
  • Web-crawler for facebook in python
  • trying to dynamically update Highchart column chart but series undefined
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • How can I remove ASP.NET Designer.cs files?
  • Django query for large number of relationships
  • java string with new operator and a literal