如何使用iTextSharp的创建pdfformfields? [英] How to create pdfformfields using iTextSharp?

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

问题描述

我使用iTextSharp的CSHARP和用于创建PDF文件。我需要添加像复选框,单选按钮和下拉不能被编辑formfields。

我用这个..

 的FileStream pdffile =新的FileStream(路径+/Pdf/tes.pdf\",FileMode.Create);
PdfWriter作家= PdfWriter.GetInstance(DOC,pdffile);
doc.Open();
矩形RECT =新的Rectangle(100,100,100,100);
RadioCheckField复选框=新RadioCheckField(作家,矩形,bhjabsdf,关于);
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField场= checkbox.CheckField;
writer.AddAnnotation(场);
doc.Close();

但它不工作。我也看了关于 PdfStamper 。但我创建一个新的PDF,不改变现有的one.So我不知道我是否可以使用 PdfStamper

谢谢..

编辑:

 私人无效CreateRadioButton(PdfWriter作家,PdfContentByte CB,字体字型)
    {
        矩形RECT;
        PdfFormField场;
        PdfFormField RadioGroup中= PdfFormField.CreateRadioButton(作家,真正的);
        radiogroup.FieldName =语文;
        RadioCheckField收音机;
        INT X = 20;
        的for(int i = 0; I< Petrol.Length;我++)
        {
            RECT =新的Rectangle(440 + I * X,692,450 + I * X,682);
            无线电=新RadioCheckField(作家,矩形,空,语言文字[I]);
            radio.BorderColor = GrayColor.GRAYBLACK;
            radio.BackgroundColor = BaseColor.WHITE;
            radio.CheckType = RadioCheckField.TYPE_CIRCLE;            如果(汽油由[i] ==F)
                radio.Checked = TRUE;
            字段= radio.RadioField;
            //我在这里设置只读..            field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
            radiogroup.AddKid(场);
            ColumnText.ShowTextAligned(CB,Element.ALIGN_LEFT,
              新词(汽油[I],字体),451 + I * X,684,0);
            如果(ⅰ> = 1)×= 25;
        }
        writer.AddAnnotation(RadioGroup中);
    }


解决方案

您正在创建一个字段'艰辛的道路。有一个名为 RadioCheckField A类,使得它更容易为你打造一个字段。

请看看来自第8章书中的例子。你可以找到的例子这里 C#的版本,例如一个例子命名的按钮

 复选框=新RadioCheckField(作家,矩形,语言文字的[我]);
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField场= checkbox.CheckField;

I am using iTextSharp and CSharp for creating the pdf. I need to add formfields like checkbox, radiobutton and dropdown which can not be edited.

I used this..

FileStream pdffile = new FileStream(path + "/Pdf/tes.pdf",FileMode.Create);
PdfWriter writer = PdfWriter.GetInstance(doc, pdffile);
doc.Open(); 
Rectangle rect = new Rectangle(100, 100, 100, 100);
RadioCheckField checkbox = new RadioCheckField(writer, rect, "bhjabsdf", "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;
writer.AddAnnotation(field);
doc.Close();

But it's not working. I also read about PdfStamper. But I am creating a new pdf, not changing the existing one.So I don't know whether I can use PdfStamper?

Thanks..

Edit:

private void CreateRadioButton(PdfWriter writer, PdfContentByte cb,Font font)
    {
        Rectangle rect;
        PdfFormField field;
        PdfFormField radiogroup = PdfFormField.CreateRadioButton(writer, true);
        radiogroup.FieldName = "language";
        RadioCheckField radio;
        int x = 20;
        for (int i = 0; i < Petrol.Length; i++)
        {
            rect = new Rectangle(440 + i * x, 692, 450 + i * x, 682);
            radio = new RadioCheckField(writer, rect, null, LANGUAGES[i]);
            radio.BorderColor = GrayColor.GRAYBLACK;
            radio.BackgroundColor = BaseColor.WHITE;
            radio.CheckType = RadioCheckField.TYPE_CIRCLE; 

            if (Petrol[i] == "F")
                radio.Checked = true;
            field = radio.RadioField;
            //Here i am setting readonly..

            field.SetFieldFlags(PdfFormField.FF_READ_ONLY);
            radiogroup.AddKid(field);                
            ColumnText.ShowTextAligned(cb, Element.ALIGN_LEFT,
              new Phrase(Petrol[i], font), 451 + i * x, 684, 0);
            if (i >= 1) x = 25;
        }
        writer.AddAnnotation(radiogroup);
    }

解决方案

You're creating a field 'the hard way'. There's a class named RadioCheckField that makes it much easier for you to create a field.

Please take a look at the book examples from Chapter 8. You can find C# versions of the examples here, for instance an example named Buttons.

checkbox = new RadioCheckField(writer, rect, LANGUAGES[i], "on");
checkbox.CheckType = RadioCheckField.TYPE_CHECK;
PdfFormField field = checkbox.CheckField;

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

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