将PdfPCell添加到段落中 [英] Add PdfPCell to Paragraph
问题描述
我正在尝试使用iTextSharp在段落句子的中间添加一个TextField(acrofield)。一个例子是生效日期是[月] [日],[年]这将开始。
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."
我尝试的事情:
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(...)有效,因为我在页面上的其他几个地方使用它。
I know the CreateTextField(...) works because I am using it in several other places on the page.
如何在不添加其他文本的情况下添加TextField使用表格并繁琐地试图操纵单元格大小以适应我的需要?
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?
感谢您的帮助!
推荐答案
你的问题是对的。您不希望将 PdfPCell
添加到段落
。您想要创建内联表单字段。这是一个完全不同的问题。
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.
看一下 GenericFields 示例。在这个例子中,我们创建你需要的段落
:
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.");
你看到我们如何添加空块
你想在哪里添加 PdfPCell
?我们在这些 Chunk
对象上使用 setGenericTag()
方法添加一个表单字段,其中 Chunk
s被渲染。
Do you see how we add empty Chunk
s where you want to add a PdfPCell
? We use the setGenericTag()
method on these Chunk
object to add a form field where ever the Chunk
s are rendered.
为此,我们需要声明一个页面事件:
For this to work, we need to declare a page event:
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
, rect
和 text
参数来创建并添加 TextField
。结果如下所示:
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:
< img src =https://i.stack.imgur.com/Dg9Et.pngalt =在此输入图像说明>
随意如果你想创建一个更大的文本字段,以适应 rect
。
Feel free to adapt rect
if you want to create a bigger text field.
重要:我的例子是用Java编写的。如果要将示例移植到C#,只需将每个方法的第一个字母更改为大写(例如,将 add()
更改为 Add()
)。如果这不起作用,请尝试将参数设置为成员变量(例如,将 writer.setPageEvent(event)
更改为 writer.PageEvent = event
)。
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);
这篇关于将PdfPCell添加到段落中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!