如何向使用iTextSharp创建的复选框添加文本? [英] How can I add text to a Checkbox I create using iTextSharp?

查看:177
本文介绍了如何向使用iTextSharp创建的复选框添加文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

源自Jeff S的方法,这里 ,我可以添加一个复选框到PDF页面如下:

  PdfPTable tblFirstRow = new PdfPTable(5); 
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.Horizo​​ntalAlignment = Element.ALIGN_LEFT;

。 。 。 //添加文本框的代码已被省略为了简洁

PdfPCell cell204Submitted = new PdfPCell()
{
CellEvent = new DynamicCheckbox(checkbox204Submitted,204已提交或已打开文件)
};
tblFirstRow.AddCell(cell204Submitted);

doc.Add(tblFirstRow);



基于Jeff S的CustomCellLayout类的DynamicCheckbox类是:

  public class DynamicCheckbox:IPdfPCellEvent 
{
private string fieldname;
private string cap;

public DynamicCheckbox(string name,String caption)
{
fieldname = name;
cap = caption;
}

public void CellLayout(PdfPCell单元格,矩形矩形,PdfContentByte [] Canvases)
{
PdfWriter writer = canvases [0] .PdfWriter;
RadioCheckField ckbx = new RadioCheckField(writer,rectangle,fieldname,Yes);
ckbx.CheckType = RadioCheckField.TYPE_CHECK;
ckbx.Text = cap;
PdfFormField field = ckbx.CheckField;
writer.AddAnnotation(field);
}
}

我的问题是复选框的文本到ckbx.Text)不显示。复选框(超大)占据表格行中的最后一个单元格,但没有(可见)伴随文本。



我的代码缺少什么?



注意:我试图通过这样做减小复选框的大小:

 矩形tangle = new Rectangle(20,20); 
// RadioCheckField ckbx = new RadioCheckField(writer,rectangle,fieldname,Yes);
RadioCheckField ckbx = new RadioCheckField(writer,tangle,fieldname,Yes);

...但该尝试失败 - 使用该代码,我甚至无法查找在生成的PDF文件中的复选框 - 第5列中的点击不会产生复选框...

解决方案

-32000-1,复选框是Button类型的字段。如果定义按钮的文本,则要定义按钮上显示的文本。但是:在一个复选框的情况下,没有这样的文本!相反,您有两个出现,一个用于值,一个用于 Yes 值。



注意的读者作出的有根据的猜测是,你不想添加文本(到按钮),但是你想添加标签(对于复选框)。再次,你应该参考ISO-32000-1,你会发现规格没有说明任何关于复选框的标签。这个概念在AcroForm的层面上不存在。



这并不意味着概念一般不存在。许多PDF工具允许您定义前面带有标签的复选框。当您查看PDF内部时,您会发现此标签只是内容的一部分,而复选框则由窗口小部件方向表示。



看看官方文档,而不是阻止自己在网上的每一个地方搜索,除了官方网站。具体来说:让我们来看看我的书第7章的按钮示例。你会看到一个可以为一个真正的按钮设置文本:

  PushbuttonField按钮= new PushbuttonField(writer,rect,Buttons ); 
button.setText(Push me);

这对于复选框是不可能的(因为显然的原因是复选框的外观是完全不同)。如果我们想添加一个标签,我们可以像这样添加:

  checkbox = new RadioCheckField(writer,rect, LANGUAGES [i],Yes); 
field = checkbox.getCheckField();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL,Off,onOff [0]);
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL,Yes,onOff [1]);
writer.addAnnotation(field);
ColumnText.showTextAligned(canvas,Element.ALIGN_LEFT,
new Phrase(LANGUAGES [i],font),210,790 - i * 40,0);

您可以在这里找到这些示例的C#版本: http://tinyurl.com/itextsharpIIA2C07


Derived from Jeff S's methodology found here, I can add a "Checkbox" to a PDF page like so:

PdfPTable tblFirstRow = new PdfPTable(5);
tblFirstRow.SpacingBefore = 4f;
tblFirstRow.HorizontalAlignment = Element.ALIGN_LEFT;

. . . // code where textboxes are added has been elided for brevity

PdfPCell cell204Submitted = new PdfPCell()
{
    CellEvent = new DynamicCheckbox("checkbox204Submitted", "204 Submitted or on file")
};
tblFirstRow.AddCell(cell204Submitted);

doc.Add(tblFirstRow);

The DynamicCheckbox class, based on Jeff S's CustomCellLayout class, is:

public class DynamicCheckbox : IPdfPCellEvent
{
    private string fieldname;
    private string cap;

    public DynamicCheckbox(string name, String caption)
    {
        fieldname = name;
        cap = caption;
    }

    public void CellLayout(PdfPCell cell, Rectangle rectangle, PdfContentByte[] canvases)
    {
        PdfWriter writer = canvases[0].PdfWriter;
        RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
        ckbx.CheckType = RadioCheckField.TYPE_CHECK;
        ckbx.Text = cap;
        PdfFormField field = ckbx.CheckField;
        writer.AddAnnotation(field);
    }
}

My problem is that the checkbox's text (the string assigned to ckbx.Text) is not displaying. The checkbox (outsized) occupies the last cell in the table row, but there is no (visible) accompanying text.

What's missing from my code?

Note: I tried to reduce the size of the checkbox by doing this:

Rectangle tangle = new Rectangle(20, 20);
//RadioCheckField ckbx = new RadioCheckField(writer, rectangle, fieldname, "Yes");
RadioCheckField ckbx = new RadioCheckField(writer, tangle, fieldname, "Yes");

...but that attempt failed - with that code, I can't even "find" the checkbox in the generated PDF file - clicking willy-nilly in column 5 conjures up no checkbox...

解决方案

As described in ISO-32000-1, a check box is a field of type Button. If you define text for a button, you want to define the text that is displayed on the button. However: in the case of a check box, there is no such text! Instead, you have two appearances, one for the Off value and one for the Yes value.

An educated guess made by an attentive reader would be that you don't want to add text (to the button), but that you want to add a label (for a checkbox). Again you should consult ISO-32000-1 and you'll discover that the spec doesn't say anything about labels for check boxes. The concept just doesn't exist at the level of an AcroForm.

This doesn't mean the concept doesn't exist in general. Many PDF tools allow you to define check boxes that are preceded by a label. When you look inside the PDF, you'll discover that this label is just part of the content, whereas the check box is represented by a widget orientation.

Let's take a look at the official documentation instead of frustrating ourselves searching on every place of the web except on the official web site. More specifically: let's take a look at the Buttons example from Chapter 7 of my book. You'll see that one can set text for a real button:

PushbuttonField button = new PushbuttonField(writer, rect, "Buttons");
button.setText("Push me");

This isn't possible with check boxes (for the obvious reason that the appearance of a check box is completely different). If we want to add a label, we can add it for instance like this:

checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "Yes");
field = checkbox.getCheckField();
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
field.setAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", onOff[1]);
writer.addAnnotation(field);
ColumnText.showTextAligned(canvas, Element.ALIGN_LEFT,
    new Phrase(LANGUAGES[i], font), 210, 790 - i * 40, 0);

You can find the C# version of these examples here: http://tinyurl.com/itextsharpIIA2C07

这篇关于如何向使用iTextSharp创建的复选框添加文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆