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

c# - Add PdfPCell to Paragraph

I'm trying to add a TextField (acrofield) in the middle of a Paragraph sentence using iTextSharp. An example would be "The Effective Date is [Day] day of [Month], [Year] that this will begin."

Things I have tried:

Paragraph para1 = new Paragraph();
para1.Add(New Phrase("The Effective Date is",fontBold));
    //The next line is where it breaks, "Insertion of illegal Element: 30"
para1.Add(CreateTextField("Day",1,0)); //this function returns a PdfPCell.

PdfPCell tempCell = new PdfPCell();
tempCell.AddElement(new Phrase("The Effective Date is",fontBold));
    //the next line breaks as well, "Element not allowed."
tempCell.AddElement(CreateTextField("Day",1,0));

Paragraph para1 = new Paragraph();
para1.Add(New Phrase("The Effective Date is",fontBold));
para1.AddSpecial(CreateTextField("Day",1,0));
    //This doesn't generate an error, but the TextField is not displayed on PDF

Paragraph para1 = new Paragraph();
PdfPTable tempTable = new PdfPTable(1);
para1.Add(New Phrase("Hello",fontBold));
tempTable.AddCell(CreateTextField("Day",1,0));
para1.Add(tempTable);
para1.Add(New Phrase("World",fontBold));
    //This doesn't generate an error, but the TextField is not displayed on PDF

I know the CreateTextField(...) works because I am using it in several other places on the page.

How can I add a TextField inline with other text without using tables and tediously trying to manipulate cell size to accommodate what I need?

Thanks for the help!

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Your question is wrong. You don't want to add a PdfPCell to a Paragraph. You want to create inline form fields. That's a totally different question.

Take a look at the GenericFields example. In this example, we create the Paragraph you need like this:

Paragraph p = new Paragraph();
p.add("The Effective Date is ");
Chunk day = new Chunk("     ");
day.setGenericTag("day");
p.add(day);
p.add(" day of ");
Chunk month = new Chunk("     ");
month.setGenericTag("month");
p.add(month);
p.add(", ");
Chunk year = new Chunk("            ");
year.setGenericTag("year");
p.add(year);
p.add(" that this will begin.");

Do you see how we add empty Chunks where you want to add a PdfPCell? We use the setGenericTag() method on these Chunk object to add a form field where ever the Chunks are rendered.

For this to work, we need to declare a page event:

writer.setPageEvent(new FieldChunk());

The FieldChunk class looks like this:

public class FieldChunk extends PdfPageEventHelper {
    @Override
    public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text) {
        TextField field = new TextField(writer, rect, text);
        try {
            writer.addAnnotation(field.getTextField());
        } catch (IOException ex) {
            throw new ExceptionConverter(ex);
        } catch (DocumentException ex) {
            throw new ExceptionConverter(ex);
        }
    }
}

Every time a "generic chunk" is rendered, the onGenericTag() method will be called passing the parameter we used in the setGenericTag() method as the text parameter. We use the writer, rect and text parameters to create and add a TextField. The result looks like this:

enter image description here

Feel free to adapt rect if you want to create a bigger text field.

Important: my example is written in Java. If you want to port the example to C#, just change the first letter of each method to upper case (e.g. change add() into Add()). If that doesn't work, try setting the parameter as a member variable (e.g. change writer.setPageEvent(event) into writer.PageEvent = event).

Update: If you want to make the field bigger, you should create a new Rectangle. For instance:

Rectangle rect2 = new Rectangle(rect.Left, rect.Bottom - 5, rect.Right, rect.Top + 2);
TextField field = new TextField(writer, rect2, text);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
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

...