2015-10-20 1091 views
1

我正在尝试在使用iTextSharp的段落句子中添加一个TextField(acrofield)。一个例子是“生效日期是[Month],[Year]的[Day]日,这将开始。”将PPCPCell添加到段落

事情我已经尝试:

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 

我知道,通过createTextField(...)的作品,因为我在使用它其他几个地方在页面上。

如何在不使用表格的情况下将TextField与其他文本内嵌添加,并且繁琐地尝试操纵单元格大小以适应我需要的内容?

感谢您的帮助!

+0

这是个多么奇怪的问题。 “PdfPCell”是“PdfPRow”的一部分,而“PdfPRow”又是“PdfPTable”的一部分。我想不出为什么有人想为'Paragraph'添加一个'PdfPCell'。你想要达到的目标可以很容易完成,但不能用“PdfPCell”。你的问题听起来像“我用叉子吃汤,这是行不通的。”这个问题的答案是:“不要使用叉子(也不要责怪叉子),使用勺子。” –

+0

取决于你喜欢你的汤。 :P –

回答

3

你的问题是错误的。您不希望将PdfPCell添加到Paragraph。你想创建内联表单域。这是完全不同的问题。请参考GenericFields示例。在这个例子中,我们创建了Paragraph,你需要这样的:

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

你看我们如何添加要在其中添加PdfPCellChunk S'我们在这些Chunk对象上使用setGenericTag()方法来添加一个表单域,其中的任何一个表单域都会呈现。

对于这个工作,我们需要声明一个页面事件:

writer.setPageEvent(new FieldChunk()); 

FieldChunk类看起来是这样的:

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

每次“通用块”呈现时,onGenericTag()方法将被称为传递我们在setGenericTag()方法中使用的参数作为text参数。我们使用writer,recttext参数来创建并添加TextField。结果是这样的:如果你想创建一个更大的文本字段

enter image description here

随意适应rect

重要提示:我的例子是用Java编写的。如果您想将示例移植到C#,只需将每种方法的第一个字母更改为大写(例如,将add()更改为Add())。如果这不起作用,请尝试将参数设置为成员变量(例如将writer.setPageEvent(event)更改为writer.PageEvent = event)。

更新:如果您想使该字段更大,您应该创建一个新的Rectangle。例如:

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

感谢您的详细解答!我不知道这是应该如何完成的。无痛地转换为C#。 setPageEvent不再是itextSharp的选项。必须使用writer.PageEvent = new FieldChunk(); –

+0

什么是使字段更大或至少更高的好方法,以便与其他字体大小更接近?我尝试手动设置大块上的字体与其余行相同,也尝试在OnGenericTag()中设置矩形,但是然后它下降到左下边缘,或者我必须弄清楚我的llx和lly是为我需要使用这个的每个地方。这是唯一的方法吗?再次感谢! –

+1

我会更新我的答案。 –