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
384 views
in Technique[技术] by (71.8m points)

c# - iTextSharp: fields generated in a table not shown in adobe reader

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.Can't see in adobe 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! 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.

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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);

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

2.1m questions

2.1m answers

60 comments

57.0k users

...