iTextSharp: fields generated in a table not shown in adobe reader

南楼画角 提交于 2019-12-11 14:13:00

问题


I'm trying to generate a pdf and fill in some fields at the same time. I do this two ways. The first by creating a text field and setting the text.

var _text = new TextField(writer,
                new Rectangle(sectionX + textLength + 2, fieldIncrementer - 3,
                    sectionX + Convert.ToSingle(xElement.Parent.Attribute("width").Value),
                    fieldIncrementer + fieldFontSize - 2), xElement.Attribute("name").Value.Trim());
if (xElement.Attribute("autoFill") != null)
 {
   _text.Text = FormAutoFill.Instance.GetValue(xElement.Attribute("autoFill").Value);
 }

This works well. The other way is for creating a text field that sits on top of a cell in a table.

//Create the text field that will be attached to the cell
var tf = new TextField(writer, new Rectangle(67, 585, 140, 800),
                tableDescendant.Attribute("name").Value); //The rectangle will be ignored by the cell event
tf.Alignment = alignment;
tf.Font = FONT;
if (tableDescendant.Attribute("autoFill") != null)
 {
   tf.Text = tableDescendant.Attribute("autoFill").Value;
 }
//Create an empty phrase since the cell won't take an empty string
var phrase = new Phrase(" ",
     new Font(FONT, FontSize, Font.NORMAL, BaseColor.BLACK));
//Create the empty cell
var tbCell = new PdfPCell(phrase);
var events = new FieldPositioningEvents(writer, tf.GetTextField());
events.AddField("djdjdj", tf.GetTextField());
tbCell.CellEvent = events;

This method works great when viewing the pdf through BlueBeam Revu, but in Adobe Reader, the fields that are in a table are not visible.

Even stranger, if I click the button on the top of reader that says "Highlight Existing Fields" I see the text that supposed to be there, but it's squashed!

I hope someone can help me out here, I see no difference between the first method and the second method.

UPDATE #1: It appears that the same behavior is exhibited in bluebeam after I print. That is, the text is squashed.


回答1:


Ok, I looked at your sample file, and was surprised about the dimensions of the field's appearance's bounding box:

/BBox   [0, 0, 73, 215] 

It has the width and height of your original TextField, the values which as per your comment will be ignored by the cell event.

var tf = new TextField(writer, new Rectangle(67, 585, 140, 800),
            tableDescendant.Attribute("name").Value); //The rectangle will be ignored by the cell event

Indeed, the cell event does not use those values, but before the cell event is triggered, you set th value of the field:

tf.Text = tableDescendant.Attribute("autoFill").Value;

Here an appearance is generated for that field value, and that appearance uses those initial field dimensions you hoped to be ignored.

To solve this, you should try setting the value after the cell event is triggered, i.e. after the table is layout'ed.

Alternatively you can switch off the generation of appearances altogether, e.g. as proposed by coderRed by creating a pdf stamper as follows:

PdfReader reader = new PdfReader(xmlFormDoc + ".pdf"); 
PdfStamper stamper = new PdfStamper(reader,new FileStream("tempdoc.pdf",FileMode.Create));
stamper.AcroFields.GenerateAppearances = false; 
stamper.Close();
reader.Close();        
File.Replace("tempdoc.pdf",xmlFormDoc+".pdf",null);


来源:https://stackoverflow.com/questions/25169342/itextsharp-fields-generated-in-a-table-not-shown-in-adobe-reader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!