如何使用iTextSharp 5.5.13添加带刻度标记操作的复选框

时间:2018-03-29 10:35:51

标签: c# itext

复选框与十字标记一起正常工作。 但是,在复选框选中时,我想用刻度线替换十字标记。

我在C#中使用iTextSharp 5.5.13尝试了这段代码:

document.Open();
PdfContentByte cb = writer.DirectContent;
iTextSharp.text.Rectangle rect;
PdfFormField Field1;

PdfAppearance[] onOff = new PdfAppearance[2];
onOff[0] = cb.CreateAppearance(20, 20);
onOff[0].Rectangle(1, 1, 18, 18);
onOff[0].Stroke();

onOff[1] = cb.CreateAppearance(20, 20);
onOff[1].SetRGBColorFill(255, 128, 128);
onOff[1].Rectangle(1, 1, 18, 18);
onOff[1].FillStroke();
//onOff[1].MoveTo(1, 1);
//onOff[1].LineTo(19, 19);
//onOff[1].MoveTo(1, 19);
//onOff[1].LineTo(19, 1);
//onOff[1].Stroke();
RadioCheckField checkbox1;

for (int i = 0; i < LANGUAGES_gc.Length; i++)
{
    rect = new iTextSharp.text.Rectangle(180, 806 - i * 40, 200, 788 - i * 40);
    checkbox1 = new RadioCheckField(writer, rect, LANGUAGES_gc[i], "on");

    checkbox1.CheckType = RadioCheckField.TYPE_CHECK;

    Field1 = checkbox1.CheckField;
    Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", onOff[0]);
    Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);

    writer.AddAnnotation(Field1);

    cb.BeginText();
    cb.SetRGBColorFill(255, 0, 0);
    cb.SetFontAndSize(BaseFont.CreateFont(@"C:\Windows\Fonts\arial.ttf", BaseFont.CP1252, false), 8);
    cb.ShowTextAligned(PdfContentByte.ALIGN_LEFT, LANGUAGES_gc[i], 210, 790 - i * 40, 0);
    cb.EndText();

}

cb = writer.DirectContent;
document.Close();

我在代码中尝试了这个标记checkbox1.CheckType = RadioCheckField.TYPE_CHECK; 但输出没有任何变化

1 个答案:

答案 0 :(得分:1)

您的代码通过将外观流分别设置为onOff[0]onOff[1]来明确设置复选框的外观。至少在复选框具有焦点时使用这些外观。

因此,要始终显示勾号,您不仅要设置CheckType,还要调整自定义外观或完全删除它们。

你可以,例如适应你的外表,特别是 On 外观,如下所示:

onOff[1] = cb.CreateAppearance(20, 20);
onOff[1].SetRGBColorFill(255, 128, 128);
onOff[1].Rectangle(1, 1, 18, 18);
onOff[1].FillStroke();
onOff[1].BeginText();
onOff[1].SetGrayFill(0);
onOff[1].SetFontAndSize(BaseFont.CreateFont(BaseFont.ZAPFDINGBATS, BaseFont.WINANSI, false), 15);
onOff[1].MoveText(4, 5);
onOff[1].ShowText("4");
onOff[1].EndText();

此外,您必须对所需活动状态的名称保持一致。目前你称之为&#34; on&#34;在一个地方

checkbox1 = new RadioCheckField(writer, rect, LANGUAGES_gc[i], "on");

然后&#34; On&#34;在另一个

Field1.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "On", onOff[1]);

您必须决定是否要打开打开