添加一个复选框与iTextSharp [英] Add one checkbox with iTextSharp

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

问题描述

我想使用iTextSharp库为C#添加一个复选框。我遵循了一些例子,并试图修改它们,因为它们显示了如何创建一组复选框。如何创建一个?

  private static void WriteFooter(Document doc,int page,int maxPages,bool lastPage,PdfWriter writer ,PdfAppearance [] checkBoxAppearance)
{
if(!lastPage)
{
doc.Add(新段落(\\\
MA-2028.1-F2 page +of+ maxPages,Fonts.ExtraSmall));
doc.NewPage();
} else
{
PdfContentByte cb = writer.DirectContent;
PdfFormField _checkGroup = PdfFormField.CreateRadioButton(writer,true);
RadioCheckField _radioG;
PdfFormField _radioField1;
_radioG = new RadioCheckField(writer,new Rectangle(20,20),null,Yes);
_radioG.CheckType = RadioCheckField.TYPE_CHECK;
_radioField1 = _radioG.RadioField;
_checkGroup.AddKid(_radioField1);
ColumnText.ShowTextAligned(cb,Element.ALIGN_RIGHT,new Phrase(No Users require Deletion),20,20,0);
writer.AddAnnotation(_checkGroup);
cb = writer.DirectContent;
doc.Add(
new Paragraph(
Operations Management:\\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER});
doc.Add(
new Paragraph(
Automation Manager或被指派者:\\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER});
}
}

writer和checkBoxAppearance从另一个函数传入。以前在我的代码中,我创建一个表格与复选框,但我需要添加一个外面的表。我按照 http://simpledotnetsolutions.wordpress.com的代码/ 2012/11/01 / itextsharp-creating-form-fields /



checkBoxAppearance是一个定义复选框行为的数组。

并尝试修改它只有一个复选框。我也试图让文字显示在它旁边。



编辑:
我也试试这个:

  private static void WriteFooter(document doc,int page,int maxPages,bool lastPage,PdfWriter writer,PdfFormField checkboxField,PdfAppearance [] checkbox,PdfContentByte cb)
{
if(!lastPage)
{
doc.Add(new Paragraph(\\\
MA-2028.1-F2(Portrait)Page+ page +of+ maxPages,Fonts 。超小));
doc.NewPage();
} else
{
PdfFormField field;
RadioCheckField复选框;

for(int i = 0; i <1; i ++)
{
checkBox = new RadioCheckField(writer,new Rectangle(20,20),noDelete 是);
field = checkBox.CheckField;
field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL,Off,checkbox [0]);
field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL,Yes,checkbox [1]);
writer.AddAnnotation(field);
//ColumnText.ShowTextAligned(cb,Element.ALIGN_RIGHT,new Phrase(No Users Need Deletion),210,790,0);
}
doc.Add(
new Paragraph(
Operations Management:\\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER});
doc.Add(
new Paragraph(
Automation Manager或被指派者:\\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER});
}
}


解决方案

心灵,我找到了一个更好的方法。我的解决方案是将复选框放在一个表中,并将其添加到PDF的方式。它看起来比我的老板最初想要的更干净。

  private static void WriteFooter(Document doc,int page,int maxPages,bool lastPage,PdfWriter writer,PdfFormField checkboxField,PdfAppearance []复选框,PdfContentByte cb)
{
if(!lastPage)
{
doc.Add(新段落(\\\
MA-2028.1-F2 +of+ maxPages,Fonts.ExtraSmall));
doc.NewPage();
} else
{
PdfFormField footerField = PdfFormField.CreateEmpty(writer);
PdfPTable footerTbl = new PdfPTable(2);
float [] footerWidths = new float [] {1f,4f};
PdfPCell noDeleteCell = new PdfPCell();
PdfPCell noDeleteText = new PdfPCell(new Paragraph(No Users requires Deletion,Fonts.Small));
RadioCheckField fCell = new RadioCheckField(writer,new Rectangle(20,20),NoDeletion,Yes);
fCell.CheckType = RadioCheckField.TYPE_CROSS;
PdfFormField footerCheck = null;
footerCheck = fCell.CheckField;
footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL,Off,checkbox [0]);
footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL,Yes,checkbox [1]);
noDeleteCell.CellEvent = new ChildFieldEvent(footerField,footerCheck,1,20,20);
footerField.FieldName =no_delete_table;
footerTbl.SetWidths(footersWidths);
footerTbl.AddCell(noDeleteCell);
footerTbl.AddCell(noDeleteText);
doc.Add(footerTbl);
writer.AddAnnotation(footerField);
doc.Add(
new Paragraph(
\\\
\\\
Operations Management:_______________________________________________日期:___________ \\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER });
doc.Add(
new Paragraph(
Automation Manager或指派者:_______________________________________________日期:___________ \\\
\\\
,Fonts.Small){Alignment = Element.ALIGN_CENTER});
doc.Add(new Paragraph(\\\
MA-2028.1-F2(Portrait)Page+ page +of+ maxPages,Fonts.ExtraSmall));
}
}


I'm trying to add just one checkbox using the iTextSharp library for C#. I've followed some examples and tried to modify them because they show how to create a group of checkboxes. How do I create just one?

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfAppearance[] checkBoxAppearance)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfContentByte cb = writer.DirectContent;
            PdfFormField _checkGroup = PdfFormField.CreateRadioButton(writer, true);
            RadioCheckField _radioG;
            PdfFormField _radioField1;
            _radioG = new RadioCheckField(writer, new Rectangle(20, 20), null, "Yes");
            _radioG.CheckType = RadioCheckField.TYPE_CHECK;
            _radioField1 = _radioG.RadioField;
            _checkGroup.AddKid(_radioField1);
            ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Require Deletion"), 20, 20, 0);
            writer.AddAnnotation(_checkGroup);
            cb = writer.DirectContent;
            doc.Add(
                new Paragraph(
                    "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee: \n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
        }
    }

writer, and checkBoxAppearance are passed in from another function. Previously in my code, I am creating a table with checkboxes but I need to add one outside of this table. I followed the code from http://simpledotnetsolutions.wordpress.com/2012/11/01/itextsharp-creating-form-fields/

checkBoxAppearance is an array that defines the behavior of the checkbox.

And tried to modify it to have just one checkbox. I'm also trying to get text to show up next to it.

EDIT: I am also trying this:

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfFormField field;
            RadioCheckField checkBox;

            for (int i = 0; i < 1; i++ )
            {
                checkBox = new RadioCheckField(writer, new Rectangle(20, 20), "noDelete", "Yes");
                field = checkBox.CheckField;
                field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]);
                field.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]);
                writer.AddAnnotation(field);
                //ColumnText.ShowTextAligned(cb, Element.ALIGN_RIGHT, new Phrase("No Users Need Deletion"), 210, 790, 0);
            }
            doc.Add(
                new Paragraph(
                    "Operations Management:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee:\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
        }
    }

解决方案

Never mind, I found a better way. My solution was to put the check box in a table and add it to the PDF that way. It looks much cleaner than the way my boss originally wanted it.

private static void WriteFooter(Document doc, int page, int maxPages, bool lastPage, PdfWriter writer, PdfFormField checkboxField, PdfAppearance[] checkbox, PdfContentByte cb)
    {
        if(!lastPage)
        {
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
            doc.NewPage();
        } else
        {
            PdfFormField footerField = PdfFormField.CreateEmpty(writer);
            PdfPTable footerTbl = new PdfPTable(2);
            float[] footerWidths = new float[] { 1f, 4f };
            PdfPCell noDeleteCell = new PdfPCell();
            PdfPCell noDeleteText = new PdfPCell(new Paragraph("No Users Require Deletion", Fonts.Small));
            RadioCheckField fCell = new RadioCheckField(writer, new Rectangle(20, 20), "NoDeletion", "Yes");
            fCell.CheckType = RadioCheckField.TYPE_CROSS;
            PdfFormField footerCheck = null;
            footerCheck = fCell.CheckField;
            footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Off", checkbox[0]);
            footerCheck.SetAppearance(PdfAnnotation.APPEARANCE_NORMAL, "Yes", checkbox[1]);
            noDeleteCell.CellEvent = new ChildFieldEvent(footerField, footerCheck, 1, 20, 20);
            footerField.FieldName = "no_delete_table";
            footerTbl.SetWidths(footerWidths);
            footerTbl.AddCell(noDeleteCell);
            footerTbl.AddCell(noDeleteText);
            doc.Add(footerTbl);
            writer.AddAnnotation(footerField);
            doc.Add(
                new Paragraph(
                    "\n\nOperations Management: _______________________________________________   Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(
                new Paragraph(
                    "Automation Manager or Designee: _______________________________________________   Date: ___________\n\n", Fonts.Small) { Alignment = Element.ALIGN_CENTER });
            doc.Add(new Paragraph("\nMA-2028.1-F2(Portrait) Page " + page + " of " + maxPages, Fonts.ExtraSmall));
        }
    }

这篇关于添加一个复选框与iTextSharp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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